ls1-MarDyn
ls1-MarDyn molecular dynamics code
FixedSizeQueue.h
1#pragma once
2
3#include <deque>
4
14template <typename T>
16public:
21 explicit FixedSizeQueue(size_t capacity = 1ul) : _capacity{capacity} {};
22
27 void setCapacity(size_t new_capacity){
28 _capacity = new_capacity;
29 // shrink to fit:
30 while(_storage.size() > _capacity){
31 _storage.pop_front();
32 }
33 }
34
40 void insert(const T& t) {
41 if (_storage.size() == _capacity) {
42 _storage.push_back(t);
43 _storage.pop_front();
44 } else {
45 _storage.push_back(t);
46 }
47 }
48
54 auto begin() { return _storage.begin(); }
55
60 auto begin() const { return _storage.cbegin(); }
61
66 auto end() { return _storage.end(); }
67
72 auto end() const { return _storage.cend(); }
73
74 [[nodiscard]] size_t size() const {
75 return _storage.size();
76 }
77private:
81 size_t _capacity{0ul};
82
86 std::deque<T> _storage;
87};
Definition: FixedSizeQueue.h:15
auto end()
Definition: FixedSizeQueue.h:66
void insert(const T &t)
Definition: FixedSizeQueue.h:40
auto end() const
Definition: FixedSizeQueue.h:72
auto begin() const
Definition: FixedSizeQueue.h:60
auto begin()
Definition: FixedSizeQueue.h:54
FixedSizeQueue(size_t capacity=1ul)
Definition: FixedSizeQueue.h:21
void setCapacity(size_t new_capacity)
Definition: FixedSizeQueue.h:27