16 : mCapacity(capacity), mSize(0), mHead(0), mTail(0) {
17 size_t n = MAX(1, mCapacity);
18 mBuffer.reset(
new T[n]);
26 bool push_back(
const T& value) {
30 mBuffer[mHead] = value;
31 if (mSize < mCapacity) {
34 mHead = increment(mHead);
35 if (mSize == mCapacity) {
42 bool pop_front(T* dst =
nullptr) {
47 T value = mBuffer[mTail];
48 mTail = increment(mTail);
60 bool pop_back(T* dst =
nullptr) {
64 mHead = decrement(mHead);
65 T value = mBuffer[mHead];
77 bool push_front(
const T& value) {
81 mTail = decrement(mTail);
82 mBuffer[mTail] = value;
83 if (mSize < mCapacity) {
93 return mBuffer[mTail];
96 const T& front()
const {
97 return mBuffer[mTail];
102 return mBuffer[(mHead + mCapacity - 1) % mCapacity];
105 const T& back()
const {
106 return mBuffer[(mHead + mCapacity - 1) % mCapacity];
110 T& operator[](
size_t index) {
112 return mBuffer[(mTail + index) % mCapacity];
115 const T& operator[](
size_t index)
const {
116 return mBuffer[(mTail + index) % mCapacity];
120 size_t size()
const {
125 size_t capacity()
const {
136 return mSize == mCapacity;
142 size_t n = MAX(1, mCapacity);
143 mBuffer.reset(
new T[n]);
151 size_t increment(
size_t index)
const {
152 return (index + 1) % mCapacity;
156 size_t decrement(
size_t index)
const {
157 return (index + mCapacity - 1) % mCapacity;