-
Notifications
You must be signed in to change notification settings - Fork 0
/
myqueue.h
181 lines (148 loc) · 3.42 KB
/
myqueue.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
#ifndef MYQUEUE_H
#define MYQUEUE_H
#include "myiterator.h"
#include "mydeque.h"
#include "myvector.h"
#include "myfunction.h"
#include "myheap.h"
namespace my
{
template <class T, class Sequence = deque<T> >
class queue
{
template <class T1, class S1>
friend bool operator==(const queue<T1, S1>& x, const queue<T1, S1>& y);
template <class T1, class S1>
friend bool operator<(const queue<T1, S1>& x, const queue<T1, S1>& y);
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::reference reference;
typedef typename Sequence::const_reference const_reference;
typedef typename Sequence::size_type size_type;
public:
bool empty() const
{
return s.empty();
}
size_type size() const
{
return s.size();
}
void push(const T& x)
{
s.push_back(x);
}
void pop()
{
s.pop_front();
}
reference front()
{
return s.front();
}
const_reference front() const
{
return s.front();
}
reference back()
{
return s.back();
}
const_reference back() const
{
return s.back();
}
private:
Sequence s;
};
template <class T1, class S1>
bool operator==(const queue<T1, S1>& x, const queue<T1, S1>& y)
{
return x.s == y.s;
}
template <class T1, class S1>
bool operator<(const queue<T1, S1>& x, const queue<T1, S1>& y)
{
return x.s < y.s;
}
template <class T1, class S1>
bool operator!=(const queue<T1, S1>& x, const queue<T1, S1>& y)
{
return !(x == y);
}
template <class T1, class S1>
bool operator>(const queue<T1, S1>& x, const queue<T1, S1>& y)
{
return y < x;
}
template <class T1, class S1>
bool operator<=(const queue<T1, S1>& x, const queue<T1, S1>& y)
{
return !(x > y);
}
template <class T1, class S1>
bool operator>=(const queue<T1, S1>& x, const queue<T1, S1>& y)
{
return !(x < y);
}
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::reference reference;
typedef typename Sequence::const_reference const_reference;
typedef typename Sequence::size_type size_type;
public:
priority_queue() {}
priority_queue(const Compare& com): c(com) {}
priority_queue(const Sequence& t, const Compare& com): s(t), c(com)
{
make_heap(s.begin(), s.end(), c);
}
template <class RandomIterator>
priority_queue(RandomIterator first, RandomIterator last): s(first, last)
{
make_heap(s.begin(), s.end(), c);
}
template <class RandomIterator>
priority_queue(RandomIterator first, RandomIterator last, const Compare& com): s(first, last), c(com)
{
make_heap(s.begin(), s.end(), c);
}
priority_queue(const priority_queue& pq): s(pq.s), c(pq.c)
{
make_heap(s.begin(), s.end(), c);
}
bool empty() const
{
return s.empty();
}
size_type size() const
{
return s.size();
}
reference top()
{
return s.front();
}
const_reference top() const
{
return s.front();
}
void push(const T& x)
{
s.push_back(x);
push_heap(s.begin(), s.end());
}
void pop()
{
pop_heap(s.begin(), s.end());
s.pop_back();
}
private:
Sequence s;
Compare c;
};
} // namesapce my
#endif /* end of include guard: MYQUEUE_H */