-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathqueue.hpp
182 lines (146 loc) · 3.68 KB
/
queue.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
182
/***
队列
1. 引入异常,对于不合法的操作会抛出异常
2. 使用 list 作为底层容器,而不使用 deque
版本 1.0
作者:詹春畅
博客:senlinzhan.github.io
***/
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include "list.hpp"
#include <string>
#include <exception>
namespace mystl {
class queue_exception : public std::exception
{
public:
explicit queue_exception( const std::string &message )
: message_( message )
{
}
virtual const char * what() const noexcept override
{
return message_.c_str();
}
private:
std::string message_;
};
template <typename T, typename Container = mystl::list<T>>
class queue
{
public:
using container_type = Container;
using value_type = typename Container::value_type;
using reference = typename Container::reference;
using const_reference = typename Container::const_reference;
using size_type = typename Container::size_type;
protected:
Container container_;
public:
explicit queue( const Container &container )
: container_( container )
{
}
explicit queue( Container &&container = Container() )
: container_( std::move( container ) )
{
}
~queue() = default;
bool empty() const
{
return container_.empty();
}
size_type size() const
{
return container_.size();
}
reference front()
{
if( empty() )
{
throw queue_exception( "queue::front(): the queue is empty!" );
}
return container_.front();
}
const_reference front() const
{
if( empty() )
{
throw queue_exception( "queue::front(): the queue is empty!" );
}
return container_.front();
}
reference back()
{
if( empty() )
{
throw queue_exception( "queue::front(): the queue is empty!" );
}
return container_.back();
}
const_reference back() const
{
if( empty() )
{
throw queue_exception( "queue::front(): the queue is empty!" );
}
return container_.back();
}
void push( const value_type &value ) {
auto copy = value;
push( std::move( copy ) );
}
void push( value_type &&value ) {
emplace( std::move( value ) );
}
template<typename... Args>
void emplace( Args&&... args ) {
container_.emplace_back( std::forward<Args>( args )... );
}
void pop()
{
if( empty() )
{
throw queue_exception( "queue::pop(): the queue is empty!" );
}
container_.pop_front();
}
void swap( queue &other ) noexcept( noexcept( swap( container_, other.container_ ) ) )
{
using std::swap;
swap( container_, container_ );
}
bool operator==( const queue &other )
{
return container_ == other.container_;
}
bool operator!=( const queue &other )
{
return !( *this == other );
}
bool operator<( const queue &other )
{
return container_ < other.container_;
}
bool operator>=( const queue &other )
{
return !( *this < other );
}
bool operator>( const queue &other )
{
return other < *this;
}
bool operator<=( const queue &other )
{
return !( *this > other );
}
};
template <typename T, typename Container>
inline void swap( queue<T, Container> &left, queue<T, Container> &right )
noexcept( noexcept( left.swap( right ) ) )
{
left.swap( right );
}
}; // namespace mystl
#endif /* _QUEUE_H_ */