/
queue_with_min_v1.hpp
181 lines (142 loc) · 4.08 KB
/
queue_with_min_v1.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#ifndef EXAMPLES_QUEUE_WITH_MIN_V1_HPP
#define EXAMPLES_QUEUE_WITH_MIN_V1_HPP
#include <boost/config.hpp>
#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif
#include <list>
#include <algorithm>
namespace examples_v1 {
template <class T>
class queue_with_min {
typedef typename std::list<T>::const_iterator data_ptr_t;
std::list<T> data_;
data_ptr_t min_;
data_ptr_t pointer_to_last() const noexcept {
return std::prev(data_.end());
}
data_ptr_t pointer_to_first() const noexcept {
return data_.cbegin();
}
public:
typedef T value_type;
/// \b Complexity: O(1)
queue_with_min() noexcept
: data_()
, min_(data_.cend())
{}
/**
\b Complexity: O(1)
*/
queue_with_min(queue_with_min&& q) = default;
queue_with_min(const queue_with_min& q)
: data_(q.data_)
, min_( std::min_element(data_.cbegin(), data_.cend()) )
{}
queue_with_min(std::initializer_list<value_type> il)
: data_(il)
, min_( std::min_element(data_.cbegin(), data_.cend()) )
{}
queue_with_min& operator=(queue_with_min&& q) = default;
queue_with_min& operator=(const queue_with_min& q) {
if (this == &q) {
return *this;
}
data_ = q.data_;
min_ = std::min_element(data_.cbegin(), data_.cend());
return *this;
}
queue_with_min& operator=(std::initializer_list<value_type> il) {
data_ = il;
min_ = std::min_element(data_.cbegin(), data_.cend());
return *this;
}
// back
/// \b Complexity: O(1)
void push_back(value_type&& v) {
emplace_back(std::move(v));
}
/// \b Complexity: O(1)
void push_back(const value_type& v) {
emplace_back(v);
}
/// \b Complexity: O(1)
template <class... Args>
void emplace_back(Args&&... args) {
const bool need_reinit = (min_ == data_.cend());
data_.emplace_back(std::forward<Args>(args)...);
if (need_reinit || data_.back() < *min_) {
min_ = pointer_to_last();
}
}
/// \b Complexity: amort O(1)
void pop_back() {
const bool need_reinit = (min_ == pointer_to_last());
data_.pop_back();
if (need_reinit) {
min_ = std::min_element(data_.cbegin(), data_.cend());
}
}
/// \b Complexity: O(1)
const value_type& back() const {
return data_.back();
}
// front
/// \b Complexity: O(1)
void push_front(value_type&& v) {
emplace_front(std::move(v));
}
/// \b Complexity: O(1)
void push_front(const value_type& v) {
emplace_front(v);
}
/// \b Complexity: O(1)
template <class... Args>
void emplace_front(Args&&... args) {
const bool need_reinit = (min_ == data_.cend());
data_.emplace_front(std::forward<Args>(args)...);
if (need_reinit || data_.front() < *min_) {
min_ = pointer_to_first();
}
}
/// \b Complexity: amort O(1)
void pop_front() {
const bool need_reinit = (min_ == pointer_to_first());
data_.pop_front();
if (need_reinit) {
min_ = std::min_element(data_.cbegin(), data_.cend());
}
}
/// \b Complexity: O(1)
const value_type& front() const {
return data_.front();
}
// misc
/// \b Complexity: O(1)
std::size_t size() const noexcept {
return data_.size();
}
/// \b Complexity: O(1)
bool empty() const noexcept {
return data_.empty();
}
/// \b Complexity: O(1)
const value_type& min() const {
return *min_;
}
/// \b Complexity: O(N)
bool equal(const queue_with_min<T>& q) const noexcept {
return data_ == q.data_;
}
/// \b Complexity: O(N), in case of POD type up to O(1)
void clear() noexcept {
data_.clear();
min_ = data_.cend();
}
};
template <class T>
inline bool operator==(const queue_with_min<T>& lhs, const queue_with_min<T>& rhs) noexcept {
return lhs.equal(rhs);
}
} // namespace examples_v1
#endif // EXAMPLES_QUEUE_WITH_MIN_V1_HPP