18 #ifndef ION_BASE_CIRCULARBUFFER_H_
19 #define ION_BASE_CIRCULARBUFFER_H_
43 if (buffer_.size() < capacity_) {
44 buffer_.push_back(item);
46 buffer_[next_pos_] = item;
48 next_pos_ = (next_pos_ + 1) % capacity_;
54 DCHECK(i < buffer_.size());
56 if (buffer_.size() < capacity_) {
59 size_t wrap_threshold = capacity_ - next_pos_;
60 if (i < wrap_threshold) {
61 return buffer_[i + next_pos_];
63 return buffer_[i - wrap_threshold];
69 size_t GetSize()
const {
return std::min(buffer_.size(), capacity_); }
76 const size_t capacity_;
88 : capacity_(capacity), next_pos_(0), buffer_(alloc) {
91 buffer_.reserve(capacity_);
98 #endif // ION_BASE_CIRCULARBUFFER_H_
size_t GetSize() const
Get the current number of items in the buffer.
Simple circular buffer class that has fixed capacity and does not grow automatically.
Allocatable is an abstract base class for classes whose memory is managed by an Allocator.
void AddItem(const T &item)
Add an item to the buffer.
const T & GetItem(size_t i) const
Return the item at position i.
size_t GetCapacity() const
Get the total capacity of the buffer.
CircularBuffer(size_t capacity, const ion::base::AllocatorPtr &alloc, bool do_reserve)
Create CircularBuffer with maximum size capacity allocated from alloc.
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...