CPP-TOOLBOX
Loading...
Searching...
No Matches
expiring_temporal_vector.hpp
Go to the documentation of this file.
1#ifndef EXPIRING_TEMPORAL_VECTOR_HPP
2#define EXPIRING_TEMPORAL_VECTOR_HPP
3
4#include <vector>
5#include <chrono>
6#include <unordered_map>
7
12template <typename T>
14public:
15 using Clock = std::chrono::steady_clock;
16 using TimePoint = Clock::time_point;
17
22 explicit ExpiringTemporalVector(int time_limit_seconds);
23
28 void push_back(const T& element);
29
34 std::vector<T> get_elements();
35
41 std::vector<T> get_elements_since(TimePoint time);
42
43 // Iterator methods for non-const access
44 typename std::vector<T>::iterator begin();
45 typename std::vector<T>::iterator end();
46
47private:
51 void clean_expired_elements();
52
53 std::vector<T> elements_; // Vector of elements
54 std::unordered_map<int, TimePoint> timestamps_; // Map from index to timestamp
55 std::chrono::seconds time_limit_;
56 int current_index_; // Current index to track the next element
57};
58
59#include "expiring_temporal_vector.tpp"
60
61#endif // EXPIRING_TEMPORAL_VECTOR_HPP
std::vector< T > get_elements_since(TimePoint time)
Get all elements with timestamps greater than or equal to the specified time.
std::vector< T >::iterator end()
std::vector< T > get_elements()
Get all elements currently in the vector.
Clock::time_point TimePoint
Definition expiring_temporal_vector.hpp:16
void push_back(const T &element)
Add an element to the vector with the current timestamp.
ExpiringTemporalVector(int time_limit_seconds)
Constructor to initialize the expiring temporal vector with a time limit.
std::chrono::steady_clock Clock
Definition expiring_temporal_vector.hpp:15
std::vector< T >::iterator begin()
@ T
Definition input_state.hpp:73