-
Notifications
You must be signed in to change notification settings - Fork 37
/
stack.hpp
169 lines (128 loc) · 3.19 KB
/
stack.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
/***
栈
1. 使用 vector 作为底层容器,而不使用 deque
3. 引入异常,对于不合法的操作会抛出异常
版本 1.0
作者:詹春畅
博客:senlinzhan.github.io
***/
#ifndef _STACK_H_
#define _STACK_H_
#include "vector.hpp"
#include <exception>
#include <string>
namespace mystl {
class stack_exception : public std::exception
{
public:
explicit stack_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::vector<T>>
class stack
{
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 stack( const Container &container )
: container_( container )
{
}
explicit stack( Container &&container = Container() )
: container_( std::move( container ) )
{
}
bool empty() const
{
return container_.empty();
}
size_type size() const
{
return container_.size();
}
reference top()
{
if( container_.empty() )
{
throw stack_exception( "stack::top(): stack is empty()" );
}
return container_.back();
}
const_reference top() const
{
return const_cast<stack *>( this )->top();
}
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( container_.empty() )
{
throw stack_exception( "stack::pop(): stack is empty()" );
}
container_.pop_back();
}
void swap( stack &other ) noexcept( noexcept( swap( container_, other.container_ ) ) )
{
using std::swap;
swap( container_, other.container );
}
bool operator==( const stack &other )
{
return container_ == other.container_;
}
bool operator!=( const stack &other )
{
return !( *this == other );
}
bool operator<( const stack &other )
{
return container_ < other.container_;
}
bool operator>=( const stack &other )
{
return !( *this < other );
}
bool operator>( const stack &other )
{
return other < *this;
}
bool operator<=( const stack &other )
{
return !( *this > other );
}
};
template <typename T, typename Container>
inline void swap( stack<T, Container> &left, stack<T, Container> &right )
noexcept( noexcept( left.swap( right ) ) )
{
left.swap( right );
}
};
#endif /* _STACK_H_ */