15 : mCapacity(capacity + 1), mHead(0), mTail(0) {
16 mBuffer.reset(
new T[mCapacity]);
22 bool push_back(
const T& value) {
24 mTail = increment(mTail);
26 mBuffer[mHead] = value;
27 mHead = increment(mHead);
31 bool pop_front(T* dst =
nullptr) {
36 *dst = mBuffer[mTail];
38 mTail = increment(mTail);
42 bool push_front(
const T& value) {
44 mHead = decrement(mHead);
46 mTail = decrement(mTail);
47 mBuffer[mTail] = value;
51 bool pop_back(T* dst =
nullptr) {
55 mHead = decrement(mHead);
57 *dst = mBuffer[mHead];
63 return mBuffer[mTail];
66 const T& front()
const {
67 return mBuffer[mTail];
71 return mBuffer[(mHead + mCapacity - 1) % mCapacity];
74 const T& back()
const {
75 return mBuffer[(mHead + mCapacity - 1) % mCapacity];
78 T& operator[](
size_t index) {
79 return mBuffer[(mTail + index) % mCapacity];
82 const T& operator[](
size_t index)
const {
83 return mBuffer[(mTail + index) % mCapacity];
87 return (mHead + mCapacity - mTail) % mCapacity;
90 size_t capacity()
const {
95 return mHead == mTail;
99 return increment(mHead) == mTail;
107 size_t increment(
size_t index)
const {
108 return (index + 1) % mCapacity;
111 size_t decrement(
size_t index)
const {
112 return (index + mCapacity - 1) % mCapacity;