ORIGINAL
Loading...
Searching...
No Matches
prique.h
1#ifndef PRIQUE_H
2#define PRIQUE_H
3#include "algorithms.h"
4#include "blocksList.h"
5#include "comparator.h"
6#include "containerAdapter.h"
7#include "types.h"
8
9namespace original
10{
11 template<typename TYPE,
12 template <typename> typename Callback = increaseComparator,
13 template <typename> typename SERIAL = blocksList>
14 requires Compare<Callback<TYPE>, TYPE>
15 class prique final : public containerAdapter<TYPE, SERIAL>
16 {
17 Callback<TYPE> compare_;
18 public:
19 explicit prique(const SERIAL<TYPE>& serial = SERIAL<TYPE>{}, const Callback<TYPE>& compare = Callback<TYPE>{});
20 prique(const std::initializer_list<TYPE>& lst, const Callback<TYPE>& compare = Callback<TYPE>{});
21 prique(const prique& other);
22 prique& operator=(const prique& other);
23 bool operator==(const prique& other) const;
24 prique(prique&& other) noexcept;
25 prique& operator=(prique&& other) noexcept;
26 void push(const TYPE& e);
27 TYPE pop();
28 TYPE top() const;
29 [[nodiscard]] std::string className() const override;
30 };
31}
32
33 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
35 original::prique<TYPE, Callback, SERIAL>::prique(const SERIAL<TYPE>& serial, const Callback<TYPE>& compare)
36 : containerAdapter<TYPE, SERIAL>(serial), compare_(compare)
37 {
38 algorithms::heapInit(this->serial_.begin(), this->serial_.last(), this->compare_);
39 }
40
41 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
42 requires original::Compare<Callback<TYPE>, TYPE>
43 original::prique<TYPE, Callback, SERIAL>::prique(const std::initializer_list<TYPE>& lst, const Callback<TYPE>& compare)
44 : prique(SERIAL<TYPE>(lst), compare) {}
45
46 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
47 requires original::Compare<Callback<TYPE>, TYPE>
48 original::prique<TYPE, Callback, SERIAL>::prique(const prique& other)
49 : containerAdapter<TYPE, SERIAL>(other.serial_), compare_(other.compare_) {}
50
51 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
52 requires original::Compare<Callback<TYPE>, TYPE>
53 auto original::prique<TYPE, Callback, SERIAL>::operator=(const prique& other) -> prique&
54 {
55 if (this == &other) return *this;
56 this->serial_ = other.serial_;
57 compare_ = other.compare_;
58 return *this;
59 }
60
61 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
62 requires original::Compare<Callback<TYPE>, TYPE>
63 auto original::prique<TYPE, Callback, SERIAL>::operator==(const prique& other) const -> bool
64 {
65 return this->serial_ == other.serial_;
66 }
67
68 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
69 requires original::Compare<Callback<TYPE>, TYPE>
70 original::prique<TYPE, Callback, SERIAL>::prique(prique&& other) noexcept : prique()
71 {
72 this->operator=(std::move(other));
73 }
74
75 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
76 requires original::Compare<Callback<TYPE>, TYPE>
77 auto original::prique<TYPE, Callback, SERIAL>::operator=(prique&& other) noexcept -> prique&
78 {
79 if (this == &other)
80 return *this;
81
82 this->serial_ = std::move(other.serial_);
83 this->compare_ = std::move(other.compare_);
84 other.serial_ = SERIAL<TYPE>{};
85 other.compare_ = Callback<TYPE>{};
86 return *this;
87 }
88
89 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
90 requires original::Compare<Callback<TYPE>, TYPE>
91 auto original::prique<TYPE, Callback, SERIAL>::push(const TYPE& e) -> void
92 {
93 this->serial_.pushEnd(e);
94 algorithms::heapAdjustUp(this->serial_.begin(), this->serial_.last(), this->compare_);
95 }
96
97 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
98 requires original::Compare<Callback<TYPE>, TYPE>
99 auto original::prique<TYPE, Callback, SERIAL>::pop() -> TYPE
100 {
101 if (this->empty()) throw noElementError();
102
103 algorithms::swap(this->serial_.begin(), this->serial_.last());
104 TYPE res = this->serial_.popEnd();
105 algorithms::heapAdjustDown(this->serial_.begin(), this->serial_.last(), this->serial_.begin(), compare_);
106 return res;
107 }
108
109 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
110 requires original::Compare<Callback<TYPE>, TYPE>
111 auto original::prique<TYPE, Callback, SERIAL>::top() const -> TYPE
112 {
113 return this->serial_.getBegin();
114 }
115
116 template <typename TYPE, template <typename> class Callback, template <typename> typename SERIAL>
117 requires original::Compare<Callback<TYPE>, TYPE>
118 auto original::prique<TYPE, Callback, SERIAL>::className() const -> std::string
119 {
120 return "prique";
121 }
122
123#endif //PRIQUE_H
Definition blocksList.h:10
Definition containerAdapter.h:10
Definition comparator.h:15
Definition error.h:44
Definition prique.h:16
Definition serial.h:8
Definition types.h:24