gtsam_points
Loading...
Searching...
No Matches
indexed_sliding_window.hpp
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2024 Kenji Koide (k.koide@aist.go.jp)
3#pragma once
4
5#include <deque>
6#include <cstdint>
7#include <iostream>
8
9namespace gtsam_points {
10
15template <typename T>
17public:
24 IndexedSlidingWindow(bool auto_shrink = true) : auto_shrink(auto_shrink), total_size(0) {}
25
26 bool empty() const { return total_size == 0; }
27 size_t size() const { return total_size; }
28 size_t inner_size() const { return data.size(); }
29
30 void clear() {
31 data.clear();
32 total_size = 0;
33 }
34
35 void push_back(const T& value) {
36 if (auto_shrink) {
37 shrink();
38 }
39
40 data.push_back(value);
41 total_size++;
42 }
43
44 void emplace_back(T&& value) {
45 if (auto_shrink) {
46 shrink();
47 }
48
49 data.emplace_back(std::move(value));
50 total_size++;
51 }
52
54 void shrink() {
55 while (!data.empty() && !data.front()) {
56 data.pop_front();
57 }
58 }
59
60 T& operator[](size_t index) {
61 if (auto_shrink) {
62 shrink();
63 }
64
65 const std::int64_t local_index = static_cast<std::int64_t>(index) - static_cast<std::int64_t>(total_size - data.size());
66 if (local_index < 0 || local_index >= static_cast<std::int64_t>(data.size())) {
67 std::cerr << "local_index: " << local_index << ", data.size(): " << data.size() << ", total_size: " << total_size << std::endl;
68 throw std::out_of_range("IndexedSlidingWindow: index out of range");
69 }
70 return data[local_index];
71 }
72
73 const T& operator[](size_t index) const {
74 const std::int64_t local_index = static_cast<std::int64_t>(index) - static_cast<std::int64_t>(total_size - data.size());
75 if (local_index < 0 || local_index >= static_cast<std::int64_t>(data.size())) {
76 std::cerr << "local_index: " << local_index << ", data.size(): " << data.size() << ", total_size: " << total_size << std::endl;
77 throw std::out_of_range("IndexedSlidingWindow: index out of range");
78 }
79 return data[local_index];
80 }
81
82 typename std::deque<T>::iterator inner_begin() { return data.begin(); }
83 typename std::deque<T>::iterator inner_end() { return data.end(); }
84 typename std::deque<T>::const_iterator inner_begin() const { return data.begin(); }
85 typename std::deque<T>::const_iterator inner_end() const { return data.end(); }
86
87 T& inner_front() { return data.front(); }
88 const T& inner_front() const { return data.front(); }
89
90 T& inner_back() { return data.back(); }
91 const T& inner_back() const { return data.back(); }
92
93 T& back() { return data.back(); }
94 const T& back() const { return data.back(); }
95
96private:
97 bool auto_shrink;
98 size_t total_size;
99 std::deque<T> data;
100};
101
102} // namespace gtsam_points
A sliding window container that allows absolute indexed access. It automatically shrinks by removing ...
Definition indexed_sliding_window.hpp:16
void shrink()
Remove leading null elements from the container.
Definition indexed_sliding_window.hpp:54
IndexedSlidingWindow(bool auto_shrink=true)
Constructor.
Definition indexed_sliding_window.hpp:24