1#ifndef CIRCULAR_DEQUE_H
2#define CIRCULAR_DEQUE_H
12 template<
class T,
size_t N>
15 static_assert(N > 0,
"CircularDeque capacity N must be greater than 0");
17 using StorageType =
typename std::aligned_storage<
sizeof(T),
alignof(T)>::type;
18 std::array<StorageType, N> arr;
20 size_t front_index = 0;
21 size_t rear_index = 0;
25 T *element_ptr(
size_t i) {
26 return reinterpret_cast<T *
>(&arr[i]);
29 const T *element_ptr(
size_t i)
const {
30 return reinterpret_cast<const T *
>(&arr[i]);
44 throw std::overflow_error(
"CircularDeque is full");
46 new(&arr[rear_index]) T(value);
47 rear_index = (rear_index + 1) % N;
53 throw std::overflow_error(
"CircularDeque is full");
55 new(&arr[rear_index]) T(std::move(value));
56 rear_index = (rear_index + 1) % N;
62 throw std::overflow_error(
"CircularDeque is full");
64 front_index = (front_index + N - 1) % N;
65 new(&arr[front_index]) T(value);
71 throw std::overflow_error(
"CircularDeque is full");
73 front_index = (front_index + N - 1) % N;
74 new(&arr[front_index]) T(std::move(value));
80 throw std::underflow_error(
"CircularDeque is empty");
82 element_ptr(front_index)->~T();
83 front_index = (front_index + 1) % N;
89 throw std::underflow_error(
"CircularDeque is empty");
91 rear_index = (rear_index + N - 1) % N;
92 element_ptr(rear_index)->~T();
98 throw std::underflow_error(
"CircularDeque is empty");
100 return *element_ptr(front_index);
105 throw std::underflow_error(
"CircularDeque is empty");
107 return *element_ptr(front_index);
112 throw std::underflow_error(
"CircularDeque is empty");
114 size_t index = (rear_index + N - 1) % N;
115 return *element_ptr(index);
120 throw std::underflow_error(
"CircularDeque is empty");
122 size_t index = (rear_index + N - 1) % N;
123 return *element_ptr(index);
Definition circular_deque.hpp:13
void push_back(const T &value)
Definition circular_deque.hpp:42
void push_front(const T &value)
Definition circular_deque.hpp:60
const T & back() const
Definition circular_deque.hpp:118
T & front()
Definition circular_deque.hpp:96
~CircularDeque()
Definition circular_deque.hpp:36
bool full() const
Definition circular_deque.hpp:138
void push_back(T &&value)
Definition circular_deque.hpp:51
T & back()
Definition circular_deque.hpp:110
void pop_back()
Definition circular_deque.hpp:87
bool empty() const
Definition circular_deque.hpp:134
void push_front(T &&value)
Definition circular_deque.hpp:69
void pop_front()
Definition circular_deque.hpp:78
const T & front() const
Definition circular_deque.hpp:103
size_t size() const
Definition circular_deque.hpp:142
void clear()
Definition circular_deque.hpp:126
Definition big_integer.hpp:14