1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_STACK
|
---|
30 | #define LIBCPP_STACK
|
---|
31 |
|
---|
32 | #include <deque>
|
---|
33 | #include <initializer_list>
|
---|
34 | #include <utility>
|
---|
35 |
|
---|
36 | namespace std
|
---|
37 | {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * 23.5.6.2, stack:
|
---|
41 | */
|
---|
42 |
|
---|
43 | template<class T, class Container = deque<T>>
|
---|
44 | class stack
|
---|
45 | {
|
---|
46 | public:
|
---|
47 | using container_type = Container;
|
---|
48 | using value_type = typename container_type::value_type;
|
---|
49 | using reference = typename container_type::reference;
|
---|
50 | using const_reference = typename container_type::const_reference;
|
---|
51 | using size_type = typename container_type::size_type;
|
---|
52 |
|
---|
53 | explicit stack(container_type& cont)
|
---|
54 | : c{cont}
|
---|
55 | { /* DUMMY BODY */ }
|
---|
56 |
|
---|
57 | explicit stack(container_type&& cont = container_type{})
|
---|
58 | : c{move(cont)}
|
---|
59 | { /* DUMMY BODY */ }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * TODO: The allocator constructor should use enable_if
|
---|
63 | * as a last parameter that checks if uses_allocator
|
---|
64 | * from <memory> holds.
|
---|
65 | */
|
---|
66 | template<class Alloc>
|
---|
67 | explicit stack(Alloc& alloc)
|
---|
68 | : c{alloc}
|
---|
69 | { /* DUMMY BODY */ }
|
---|
70 |
|
---|
71 | template<class Alloc>
|
---|
72 | stack(const container_type& cont, const Alloc& alloc)
|
---|
73 | : c{cont, alloc}
|
---|
74 | { /* DUMMY BODY */ }
|
---|
75 |
|
---|
76 | template<class Alloc>
|
---|
77 | stack(container_type&& cont, const Alloc& alloc)
|
---|
78 | : c{move(cont), alloc}
|
---|
79 | { /* DUMMY BODY */ }
|
---|
80 |
|
---|
81 | template<class Alloc>
|
---|
82 | stack(const stack& other, const Alloc& alloc)
|
---|
83 | : c{other.c, alloc}
|
---|
84 | { /* DUMMY BODY */ }
|
---|
85 |
|
---|
86 | template<class Alloc>
|
---|
87 | stack(stack&& other, const Alloc& alloc)
|
---|
88 | : c{move(other.c), alloc}
|
---|
89 | { /* DUMMY BODY */ }
|
---|
90 |
|
---|
91 | bool empty()
|
---|
92 | {
|
---|
93 | return c.empty();
|
---|
94 | }
|
---|
95 |
|
---|
96 | size_type size()
|
---|
97 | {
|
---|
98 | return c.size();
|
---|
99 | }
|
---|
100 |
|
---|
101 | reference top()
|
---|
102 | {
|
---|
103 | return c.back();
|
---|
104 | }
|
---|
105 |
|
---|
106 | const_reference top() const
|
---|
107 | {
|
---|
108 | return c.back();
|
---|
109 | }
|
---|
110 |
|
---|
111 | void push(const value_type& val)
|
---|
112 | {
|
---|
113 | c.push_back(val);
|
---|
114 | }
|
---|
115 |
|
---|
116 | void push(value_type&& val)
|
---|
117 | {
|
---|
118 | c.push_back(move(val));
|
---|
119 | }
|
---|
120 |
|
---|
121 | template<class... Args>
|
---|
122 | void emplace(Args&&... args)
|
---|
123 | {
|
---|
124 | c.emplace_back(forward<Args>(args)...);
|
---|
125 | }
|
---|
126 |
|
---|
127 | void pop()
|
---|
128 | {
|
---|
129 | c.pop_back();
|
---|
130 | }
|
---|
131 |
|
---|
132 | void swap(stack& other) // We cannot use c in the noexcept :/
|
---|
133 | noexcept(noexcept(declval<container_type>().swap(declval<container_type&>())))
|
---|
134 | {
|
---|
135 | std::swap(c, other.c);
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected:
|
---|
139 | container_type c;
|
---|
140 | };
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * 23.6.5.5, stack operators:
|
---|
144 | */
|
---|
145 |
|
---|
146 | template<class T, class Container>
|
---|
147 | bool operator==(const stack<T, Container>& lhs,
|
---|
148 | const stack<T, Container>& rhs)
|
---|
149 | {
|
---|
150 | return lhs.c == rhs.c;
|
---|
151 | }
|
---|
152 |
|
---|
153 | template<class T, class Container>
|
---|
154 | bool operator!=(const stack<T, Container>& lhs,
|
---|
155 | const stack<T, Container>& rhs)
|
---|
156 | {
|
---|
157 | return lhs.c != rhs.c;
|
---|
158 | }
|
---|
159 |
|
---|
160 | template<class T, class Container>
|
---|
161 | bool operator<(const stack<T, Container>& lhs,
|
---|
162 | const stack<T, Container>& rhs)
|
---|
163 | {
|
---|
164 | return lhs.c < rhs.c;
|
---|
165 | }
|
---|
166 |
|
---|
167 | template<class T, class Container>
|
---|
168 | bool operator<=(const stack<T, Container>& lhs,
|
---|
169 | const stack<T, Container>& rhs)
|
---|
170 | {
|
---|
171 | return lhs.c <= rhs.c;
|
---|
172 | }
|
---|
173 |
|
---|
174 | template<class T, class Container>
|
---|
175 | bool operator>(const stack<T, Container>& lhs,
|
---|
176 | const stack<T, Container>& rhs)
|
---|
177 | {
|
---|
178 | return lhs.c > rhs.c;
|
---|
179 | }
|
---|
180 |
|
---|
181 | template<class T, class Container>
|
---|
182 | bool operator>=(const stack<T, Container>& lhs,
|
---|
183 | const stack<T, Container>& rhs)
|
---|
184 | {
|
---|
185 | return lhs.c >= rhs.c;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * 23.6.5.6, stack specialized algorithms:
|
---|
190 | */
|
---|
191 |
|
---|
192 | template<class T, class Container>
|
---|
193 | void swap(stack<T, Container>& lhs, stack<T, Container>& rhs)
|
---|
194 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
195 | {
|
---|
196 | lhs.swap(rhs);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | #endif
|
---|