ORIGINAL
Loading...
Searching...
No Matches
prique.h
Go to the documentation of this file.
1#ifndef PRIQUE_H
2#define PRIQUE_H
3
11
12#include "algorithms.h"
13#include "blocksList.h"
14#include "comparator.h"
15#include "containerAdapter.h"
16#include "types.h"
17
18namespace original
19{
42 template<typename TYPE,
43 template <typename> typename Callback = increaseComparator,
44 template <typename, typename> typename SERIAL = blocksList,
45 template <typename> typename ALLOC = allocator>
46 requires Compare<Callback<TYPE>, TYPE>
47 class prique final : public containerAdapter<TYPE, SERIAL, ALLOC>
48 {
49 Callback<TYPE> compare_;
50
51 public:
59 explicit prique(const SERIAL<TYPE, ALLOC<TYPE>>& serial = SERIAL<TYPE, ALLOC<TYPE>>{},
60 const Callback<TYPE>& compare = Callback<TYPE>{});
61
69 prique(const std::initializer_list<TYPE>& lst,
70 const Callback<TYPE>& compare = Callback<TYPE>{});
71
77 prique(const prique& other);
78
85 prique& operator=(const prique& other);
86
93 prique(prique&& other) noexcept;
94
102 prique& operator=(prique&& other) noexcept;
103
110 void push(const TYPE& e);
111
119 TYPE pop();
120
126 TYPE top() const;
127
132 [[nodiscard]] std::string className() const override;
133 };
134}
135
136
137 template<typename TYPE,
138 template <typename> typename Callback,
139 template <typename, typename> typename SERIAL,
140 template <typename> typename ALLOC>
142 original::prique<TYPE, Callback, SERIAL, ALLOC>::prique(const SERIAL<TYPE, ALLOC<TYPE>>& serial, const Callback<TYPE>& compare)
143 : containerAdapter<TYPE, SERIAL, ALLOC>(serial), compare_(compare)
144 {
145 algorithms::heapInit(this->serial_.begin(), this->serial_.last(), this->compare_);
146 }
147
148 template<typename TYPE,
149 template <typename> typename Callback,
150 template <typename, typename> typename SERIAL,
151 template <typename> typename ALLOC>
153 original::prique<TYPE, Callback, SERIAL, ALLOC>::prique(const std::initializer_list<TYPE>& lst, const Callback<TYPE>& compare)
154 : prique(SERIAL<TYPE, ALLOC<TYPE>>(lst), compare) {}
155
156 template<typename TYPE,
157 template <typename> typename Callback,
158 template <typename, typename> typename SERIAL,
159 template <typename> typename ALLOC>
162 : containerAdapter<TYPE, SERIAL, ALLOC>(other.serial_), compare_(other.compare_) {}
163
164 template<typename TYPE,
165 template <typename> typename Callback,
166 template <typename, typename> typename SERIAL,
167 template <typename> typename ALLOC>
170 {
171 if (this == &other) return *this;
172 this->serial_ = other.serial_;
173 compare_ = other.compare_;
174 return *this;
175 }
176
177 template<typename TYPE,
178 template <typename> typename Callback,
179 template <typename, typename> typename SERIAL,
180 template <typename> typename ALLOC>
183 {
184 this->operator=(std::move(other));
185 }
186
187 template<typename TYPE,
188 template <typename> typename Callback,
189 template <typename, typename> typename SERIAL,
190 template <typename> typename ALLOC>
193 {
194 if (this == &other)
195 return *this;
196
197 this->serial_ = std::move(other.serial_);
198 this->compare_ = std::move(other.compare_);
199 return *this;
200 }
201
202 template<typename TYPE,
203 template <typename> typename Callback,
204 template <typename, typename> typename SERIAL,
205 template <typename> typename ALLOC>
208 {
209 this->serial_.pushEnd(e);
210 algorithms::heapAdjustUp(this->serial_.begin(), this->serial_.last(), this->compare_);
211 }
212
213 template<typename TYPE,
214 template <typename> typename Callback,
215 template <typename, typename> typename SERIAL,
216 template <typename> typename ALLOC>
219 {
220 if (this->empty()) throw noElementError();
221
222 algorithms::swap(this->serial_.begin(), this->serial_.last());
223 TYPE res = this->serial_.popEnd();
224 algorithms::heapAdjustDown(this->serial_.begin(), this->serial_.last(), this->serial_.begin(), this->compare_);
225 return res;
226 }
227
228 template<typename TYPE,
229 template <typename> typename Callback,
230 template <typename, typename> typename SERIAL,
231 template <typename> typename ALLOC>
234 {
235 return this->serial_.getBegin();
236 }
237
238 template<typename TYPE,
239 template <typename> typename Callback,
240 template <typename, typename> typename SERIAL,
241 template <typename> typename ALLOC>
244 {
245 return "prique";
246 }
247
248#endif //PRIQUE_H
Standard algorithm implementations for iterator-based containers.
A block-based list implementation.
static void heapInit(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &compares)
Initializes a heap structure from a range of iterators.
static void swap(const iterator< TYPE > &it1, const iterator< TYPE > &it2) noexcept
Swaps the values of two elements.
static void heapAdjustUp(const iterator< TYPE > &begin, const iterator< TYPE > &current, const Callback &compares)
Adjusts a heap structure upwards from a given iterator.
static void heapAdjustDown(const iterator< TYPE > &begin, const iterator< TYPE > &range, const iterator< TYPE > &current, const Callback &compares)
Adjusts a heap structure downwards starting from a given iterator.
Default memory allocator using allocators utilities.
Definition allocator.h:154
A block-based list implementation.
Definition blocksList.h:30
blocksList< TYPE, allocator< TYPE > > serial_
Definition containerAdapter.h:60
containerAdapter(const blocksList< TYPE, allocator< TYPE > > &serial)
Definition containerAdapter.h:139
bool empty() const
Definition container.h:155
Comparator for increasing comparison (less than).
Definition comparator.h:65
Exception for missing element requests.
Definition error.h:136
TYPE top() const
Accesses highest priority element.
Definition prique.h:233
prique(const SERIAL< TYPE, ALLOC< TYPE > > &serial=SERIAL< TYPE, ALLOC< TYPE > >{}, const Callback< TYPE > &compare=Callback< TYPE >{})
Constructs priority queue with container, comparator and allocator.
Definition prique.h:142
void push(const TYPE &e)
Inserts element maintaining heap property.
Definition prique.h:207
prique & operator=(const prique &other)
Copy assignment operator.
Definition prique.h:169
std::string className() const override
Gets class name identifier.
Definition prique.h:243
TYPE pop()
Extracts highest priority element.
Definition prique.h:218
Abstract base class for sequential containers with index-based access.
Definition serial.h:34
Comparator base class and concrete comparator classes.
Combines Comparable and CallbackOf for comparison callbacks.
Definition types.h:76
Base class for container adapters with common interfaces.
Main namespace for the project Original.
Definition algorithms.h:21
Type system foundations and concept definitions.