|
| 1 | +#ifndef guard_slidingBuffer_h_srhphd54ujz5u4i6dftrz5d |
| 2 | +#define guard_slidingBuffer_h_srhphd54ujz5u4i6dftrz5d |
| 3 | + |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include <cage-core/core.h> |
| 7 | + |
| 8 | +namespace cage |
| 9 | +{ |
| 10 | + template<class T> |
| 11 | + class SlidingBuffer : private Noncopyable |
| 12 | + { |
| 13 | + public: |
| 14 | + CAGE_FORCE_INLINE const T *begin() const { return items.data() + offset_; } |
| 15 | + |
| 16 | + CAGE_FORCE_INLINE T *begin() { return items.data() + offset_; } |
| 17 | + |
| 18 | + CAGE_FORCE_INLINE const T *end() const { return items.data() + offset_ + size_; } |
| 19 | + |
| 20 | + CAGE_FORCE_INLINE T *end() { return items.data() + offset_ + size_; } |
| 21 | + |
| 22 | + CAGE_FORCE_INLINE bool empty() const noexcept { return size_ == 0; } |
| 23 | + |
| 24 | + CAGE_FORCE_INLINE uintPtr size() const noexcept { return size_; } |
| 25 | + |
| 26 | + CAGE_FORCE_INLINE const T &front() const |
| 27 | + { |
| 28 | + CAGE_ASSERT(!empty()); |
| 29 | + return items[offset_]; |
| 30 | + } |
| 31 | + |
| 32 | + CAGE_FORCE_INLINE T &front() |
| 33 | + { |
| 34 | + CAGE_ASSERT(!empty()); |
| 35 | + return items[offset_]; |
| 36 | + } |
| 37 | + |
| 38 | + void push_back(const T &val) { push_back(T(val)); } |
| 39 | + |
| 40 | + void push_back(T &&val) |
| 41 | + { |
| 42 | + if (offset_ + size_ < items.capacity()) |
| 43 | + { // we have enough space for directly inserting at the end |
| 44 | + if (offset_ + size_ >= items.size()) |
| 45 | + items.push_back(std::move(val)); |
| 46 | + else |
| 47 | + items[offset_ + size_] = std::move(val); |
| 48 | + size_++; |
| 49 | + return; |
| 50 | + } |
| 51 | + CAGE_ASSERT(offset_ + size_ == items.size()); |
| 52 | + |
| 53 | + if (offset_ * 2 > items.size() + 10) |
| 54 | + { // we prefer to move existing items to front |
| 55 | + for (uintPtr i = 0; i < size_; i++) |
| 56 | + items[i] = std::move(items[offset_ + i]); |
| 57 | + offset_ = 0; |
| 58 | + items[size_] = std::move(val); |
| 59 | + size_++; |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // we must reallocate |
| 64 | + items.push_back(std::move(val)); |
| 65 | + size_++; |
| 66 | + } |
| 67 | + |
| 68 | + void pop_front() |
| 69 | + { |
| 70 | + CAGE_ASSERT(!empty()); |
| 71 | + items[offset_] = {}; |
| 72 | + size_--; |
| 73 | + if (size_ == 0) |
| 74 | + offset_ = 0; |
| 75 | + else |
| 76 | + offset_++; |
| 77 | + } |
| 78 | + |
| 79 | + void erase(const T *it) |
| 80 | + { |
| 81 | + CAGE_ASSERT(!empty()); |
| 82 | + CAGE_ASSERT(it >= items.data() && it < items.data() + items.size()); |
| 83 | + const uintPtr index = it - items.data(); |
| 84 | + CAGE_ASSERT(index >= offset_ && index < offset_ + size_); |
| 85 | + items[index] = {}; |
| 86 | + |
| 87 | + const uintPtr a = index - offset_; |
| 88 | + const uintPtr b = offset_ + size_ - index - 1; |
| 89 | + CAGE_ASSERT(a + b + 1 == size_); |
| 90 | + if (b <= a) |
| 91 | + { // shift following items left |
| 92 | + for (uintPtr i = 0; i < b; i++) |
| 93 | + items[index + i] = std::move(items[index + i + 1]); |
| 94 | + } |
| 95 | + else |
| 96 | + { // shift preceding items right |
| 97 | + for (uintPtr i = a; i; i--) |
| 98 | + items[offset_ + i] = std::move(items[offset_ + i - 1]); |
| 99 | + offset_++; |
| 100 | + } |
| 101 | + size_--; |
| 102 | + if (size_ == 0) |
| 103 | + offset_ = 0; |
| 104 | + } |
| 105 | + |
| 106 | + void clear() |
| 107 | + { |
| 108 | + offset_ = size_ = 0; |
| 109 | + items.clear(); |
| 110 | + } |
| 111 | + |
| 112 | + protected: |
| 113 | + std::vector<T> items; |
| 114 | + uintPtr offset_ = 0; |
| 115 | + uintPtr size_ = 0; |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +#endif // guard_slidingBuffer_h_srhphd54ujz5u4i6dftrz5d |
0 commit comments