-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.h
279 lines (235 loc) · 6.38 KB
/
queue.h
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* queue.h
*
* This file defines the STL-compatible container adapter queue.
* Note that stack is not an actual container, it provides a subset of functionality for an underlying container
*/
#ifndef THOR_QUEUE_H
#define THOR_QUEUE_H
#pragma once
#ifndef THOR_DEQUE_H
#include "deque.h"
#endif
#ifndef THOR_VECTOR_H
#include "vector.h"
#endif
#ifndef THOR_HEAP_H
#include "heap.h"
#endif
namespace thor
{
// thor::queue
template <class T, class Sequence = deque<T> > class queue
{
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::size_type size_type;
queue()
{}
queue(const queue& rhs) :
m_container(rhs.m_container)
{}
queue& operator = (const queue& rhs)
{
m_container = rhs.m_container;
return *this;
}
bool empty() const
{
return m_container.empty();
}
size_type size() const
{
return m_container.size();
}
T& front()
{
THOR_DEBUG_ASSERT(!empty());
return m_container.front();
}
const T& front() const
{
THOR_DEBUG_ASSERT(!empty());
return m_container.front();
}
T& back()
{
THOR_DEBUG_ASSERT(!empty());
return m_container.back();
}
const T& back() const
{
THOR_DEBUG_ASSERT(!empty());
return m_container.back();
}
void push(const T& x)
{
m_container.push_back(x);
}
void pop()
{
THOR_DEBUG_ASSERT(!empty());
m_container.pop_front();
}
// Extensions
// Default-construct
void push() { m_container.push_back(); }
const Sequence& get_container() const { return m_container; }
private:
Sequence m_container;
};
// thor::priority_queue
template <class T, class Sequence = vector<T>, class Compare = less<T> > class priority_queue
{
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::size_type size_type;
priority_queue() {}
priority_queue(const priority_queue& rhs) : m_container(rhs.m_container), m_root(rhs.m_root) {}
priority_queue(const Compare& c) : m_root(c) {}
priority_queue(const value_type* first, const value_type* last) : m_container(first, last)
{
m_root.m_pushcount = last - first;
}
priority_queue(const value_type* first, const value_type* last, const Compare& c) : m_container(first, last), m_root(c)
{
m_root.m_pushcount = last - first;
}
priority_queue& operator=(const priority_queue& rhs)
{
m_container = rhs.m_container;
m_root = rhs.m_root;
return *this;
}
bool empty() const { return m_container.empty(); }
size_type size() const { return m_container.size(); }
const value_type& top() const
{
THOR_DEBUG_ASSERT(!empty());
if (m_root.m_pushcount != 0)
{
heapify();
}
return m_container.front();
}
void push(const value_type& x)
{
m_container.push_back(x);
if (m_root.m_pushcount == 0)
{
push_heap(m_container.begin(), m_container.end(), comp());
}
else
{
++m_root.m_pushcount;
}
}
void pop()
{
THOR_DEBUG_ASSERT(!empty());
if (m_root.m_pushcount != 0)
{
heapify();
}
pop_heap(m_container.begin(), m_container.end(), comp());
m_container.pop_back();
THOR_DEBUG_ASSERT(is_heap(m_container.begin(), m_container.end(), comp()));
}
// Extensions
// Default-construct
void push()
{
m_container.push_back();
if (m_root.m_pushcount == 0)
{
push_heap(m_container.begin(), m_container.end(), comp());
}
else
{
++m_root.m_pushcount;
}
}
const Sequence& get_container() const
{
if (m_root.m_pushcount != 0)
{
heapify();
}
return m_container;
}
private:
void heapify() const
{
THOR_DEBUG_ASSERT(m_root.m_pushcount != 0);
if (m_root.m_pushcount >= size() || m_root.m_pushcount > 2)
{
THOR_DEBUG_ASSERT(m_root.m_pushcount == size() || is_heap(m_container.begin(), m_container.end() - m_root.m_pushcount, comp()));
// Just rebuild the heap
make_heap(m_container.begin(), m_container.end(), c);
}
else
{
// Just push the queued up values
THOR_DEBUG_ASSERT(is_heap(m_container.begin(), m_container.end() - m_root.m_pushcount, comp()));
Sequence::iterator iter(m_container.end());
iter -= m_root.m_pushcount;
do
{
push_heap(m_container.begin(), ++iter, c);
} while (iter != m_container.end());
}
m_root.m_pushcount = 0;
THOR_DEBUG_ASSERT(is_heap(m_container.begin(), m_container.end(), comp()));
}
Compare& comp()
{
return static_cast<Compare&>(m_root);
}
const Compare& comp() const
{
return static_cast<const Compare&>(m_root);
}
struct empty_member_opt : public Compare
{
size_type m_pushcount;
empty_member_opt() : m_pushcount(0) {}
empty_member_opt(const Compare& c) : Compare(c), m_pushcount(0) {}
};
mutable Sequence m_container;
mutable empty_member_opt m_root;
};
} // namespace thor
// Global operators
template <class T, class Sequence>
bool operator == (const queue<T,Sequence>& lhs, const queue<T,Sequence>& rhs)
{
return lhs.get_container() == rhs.get_container();
}
template <class T, class Sequence>
bool operator != (const queue<T,Sequence>& lhs, const queue<T,Sequence>& rhs)
{
return !(lhs == rhs);
}
template <class T, class Sequence>
bool operator < (const queue<T,Sequence>& lhs, const queue<T,Sequence>& rhs)
{
return lhs.get_container() < rhs.get_container();
}
template <class T, class Sequence>
bool operator > (const queue<T,Sequence>& lhs, const queue<T,Sequence>& rhs)
{
return rhs.get_container() < lhs.get_container();
}
template <class T, class Sequence>
bool operator <= (const queue<T,Sequence>& lhs, const queue<T,Sequence>& rhs)
{
return !(lhs > rhs);
}
template <class T, class Sequence>
bool operator >= (const queue<T,Sequence>& lhs, const queue<T,Sequence>& rhs)
{
return !(lhs < rhs);
}
#endif