snippets 0.1.0
Loading...
Searching...
No Matches
circular_deque.hpp
Go to the documentation of this file.
1#ifndef CIRCULAR_DEQUE_H
2#define CIRCULAR_DEQUE_H
3
4#include <array>
5#include <cstddef>
6#include <new>
7#include <stdexcept>
8#include <type_traits>
9
10
11namespace hsc_snippets {
12 template<class T, size_t N>
14 private:
15 static_assert(N > 0, "CircularDeque capacity N must be greater than 0");
16
17 using StorageType = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
18 std::array<StorageType, N> arr;
19
20 size_t front_index = 0;
21 size_t rear_index = 0;
22 size_t count = 0;
23
24 // Helper to get a pointer to the element at index i
25 T *element_ptr(size_t i) {
26 return reinterpret_cast<T *>(&arr[i]);
27 }
28
29 const T *element_ptr(size_t i) const {
30 return reinterpret_cast<const T *>(&arr[i]);
31 }
32
33 public:
34 CircularDeque() = default;
35
37 clear();
38 }
39
40 // Rest of the implementation remains similar, but with placement new and explicit destructor calls
41
42 void push_back(const T &value) {
43 if (full()) {
44 throw std::overflow_error("CircularDeque is full");
45 }
46 new(&arr[rear_index]) T(value);
47 rear_index = (rear_index + 1) % N;
48 count++;
49 }
50
51 void push_back(T &&value) {
52 if (full()) {
53 throw std::overflow_error("CircularDeque is full");
54 }
55 new(&arr[rear_index]) T(std::move(value));
56 rear_index = (rear_index + 1) % N;
57 count++;
58 }
59
60 void push_front(const T &value) {
61 if (full()) {
62 throw std::overflow_error("CircularDeque is full");
63 }
64 front_index = (front_index + N - 1) % N;
65 new(&arr[front_index]) T(value);
66 count++;
67 }
68
69 void push_front(T &&value) {
70 if (full()) {
71 throw std::overflow_error("CircularDeque is full");
72 }
73 front_index = (front_index + N - 1) % N;
74 new(&arr[front_index]) T(std::move(value));
75 count++;
76 }
77
78 void pop_front() {
79 if (empty()) {
80 throw std::underflow_error("CircularDeque is empty");
81 }
82 element_ptr(front_index)->~T();
83 front_index = (front_index + 1) % N;
84 count--;
85 }
86
87 void pop_back() {
88 if (empty()) {
89 throw std::underflow_error("CircularDeque is empty");
90 }
91 rear_index = (rear_index + N - 1) % N;
92 element_ptr(rear_index)->~T();
93 count--;
94 }
95
96 T &front() {
97 if (empty()) {
98 throw std::underflow_error("CircularDeque is empty");
99 }
100 return *element_ptr(front_index);
101 }
102
103 const T &front() const {
104 if (empty()) {
105 throw std::underflow_error("CircularDeque is empty");
106 }
107 return *element_ptr(front_index);
108 }
109
110 T &back() {
111 if (empty()) {
112 throw std::underflow_error("CircularDeque is empty");
113 }
114 size_t index = (rear_index + N - 1) % N;
115 return *element_ptr(index);
116 }
117
118 const T &back() const {
119 if (empty()) {
120 throw std::underflow_error("CircularDeque is empty");
121 }
122 size_t index = (rear_index + N - 1) % N;
123 return *element_ptr(index);
124 }
125
126 void clear() {
127 while (!empty()) {
128 pop_back();
129 }
130 front_index = 0;
131 rear_index = 0;
132 }
133
134 bool empty() const {
135 return count == 0;
136 }
137
138 bool full() const {
139 return count == N;
140 }
141
142 size_t size() const {
143 return count;
144 }
145 };
146}
147
148#endif // CIRCULAR_DEQUE_H
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