-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstack.h
376 lines (271 loc) · 10.4 KB
/
stack.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements a stack that is just like the C++ std::stack adapter class.
// The only significant difference is that the stack here provides a get_container
// function to provide access to the underlying container.
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_STACK_H
#define EASTL_STACK_H
#include <eastl/internal/config.h>
#include <eastl/vector.h>
#include <eastl/initializer_list.h>
#include <stddef.h>
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// EASTL_STACK_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_STACK_DEFAULT_NAME
#define EASTL_STACK_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " stack" // Unless the user overrides something, this is "EASTL stack".
#endif
/// EASTL_STACK_DEFAULT_ALLOCATOR
///
#ifndef EASTL_STACK_DEFAULT_ALLOCATOR
#define EASTL_STACK_DEFAULT_ALLOCATOR allocator_type(EASTL_STACK_DEFAULT_NAME)
#endif
/// stack
///
/// stack is an adapter class provides a LIFO (last-in, first-out) interface
/// via wrapping a sequence container (https://en.cppreference.com/w/cpp/named_req/SequenceContainer)
/// that additionally provides the following operations:
/// pushBack
/// popBack
/// back
///
/// In practice this means vector, deque, list, intrusive_list and (the pseudo-container) string.
///
/// Note: the default underlying container is vector, rather than the standard's deque.
///
template <typename T, typename Container = eastl::vector<T> >
class stack
{
public:
typedef stack<T, Container> this_type;
typedef Container container_type;
//typedef typename Container::allocator_type allocator_type; // We can't currently declare this because the container may be a type that doesn't have an allocator.
typedef typename Container::value_type value_type;
typedef typename Container::reference reference;
typedef typename Container::const_reference const_reference;
typedef typename Container::size_type size_type;
public: // We declare public so that global comparison operators can be implemented without adding an inline level and without tripping up GCC 2.x friend declaration failures. GCC (through at least v4.0) is poor at inlining and performance wins over correctness.
container_type c; // The C++ standard specifies that you declare a protected member variable of type Container called 'c'.
public:
stack();
// Allocator is templated here because we aren't allowed to infer the allocator_type from the Container, as some containers (e.g. array) don't
// have allocators. For containers that don't have allocator types, you could use void or char as the Allocator template type.
template <class Allocator>
explicit stack(const Allocator& allocator, typename eastl::enable_if<eastl::uses_allocator<container_type, Allocator>::value>::type* = NULL)
: c(allocator)
{
}
template <class Allocator>
stack(const this_type& x, const Allocator& allocator, typename eastl::enable_if<eastl::uses_allocator<container_type, Allocator>::value>::type* = NULL)
: c(x.c, allocator)
{
}
template <class Allocator>
stack(this_type&& x, const Allocator& allocator, typename eastl::enable_if<eastl::uses_allocator<container_type, Allocator>::value>::type* = NULL)
: c(eastl::move(x.c), allocator)
{
}
explicit stack(const container_type& x);
explicit stack(container_type&& x);
// Additional C++11 support to consider:
//
// template <class Allocator>
// stack(const container_type& x, const Allocator& allocator);
//
// template <class Allocator>
// stack(container_type&& x, const Allocator& allocator);
//
// template <class InputIt>
// stack(InputIt first, InputIt last);
//
// template <class InputIt, class Allocator>
// stack(InputIt first, InputIt last, const Allocator& allocator);
stack(std::initializer_list<value_type> ilist); // The first item in the initializer list is pushed first. C++11 doesn't specify that std::stack has initializer list support.
bool empty() const;
size_type size() const;
reference top();
const_reference top() const;
void push(const value_type& value);
void push(value_type&& x);
template <class... Args> EASTL_REMOVE_AT_2024_SEPT void emplace_back(Args&&... args); // use emplace() instead. they are equivalent.
template <class... Args> decltype(auto) emplace(Args&&... args);
void pop();
container_type& get_container();
const container_type& get_container() const;
void swap(this_type& x) EASTL_NOEXCEPT_IF(eastl::is_nothrow_swappable<this_type::container_type>::value);
bool validate() const;
}; // class stack
///////////////////////////////////////////////////////////////////////
// stack
///////////////////////////////////////////////////////////////////////
template <typename T, typename Container>
inline stack<T, Container>::stack()
: c() // To consider: use c(EASTL_STACK_DEFAULT_ALLOCATOR) here, though that would add the requirement that the user supplied container support this.
{
// Empty
}
template <typename T, typename Container>
inline stack<T, Container>::stack(const Container& x)
: c(x)
{
// Empty
}
template <typename T, typename Container>
inline stack<T, Container>::stack(Container&& x)
: c(eastl::move(x))
{
// Empty
}
template <typename T, typename Container>
inline stack<T, Container>::stack(std::initializer_list<value_type> ilist)
: c() // We could alternatively use c(ilist) here, but that would require c to have an ilist constructor.
{
// Better solution but requires an insert function.
// c.insert(ilist.begin(), ilist.end());
// Possibly slower solution but doesn't require an insert function.
for(const auto& value : ilist)
{
c.pushBack(value);
}
}
template <typename T, typename Container>
inline bool stack<T, Container>::empty() const
{
return c.empty();
}
template <typename T, typename Container>
inline typename stack<T, Container>::size_type
stack<T, Container>::size() const
{
return c.size();
}
template <typename T, typename Container>
inline typename stack<T, Container>::reference
stack<T, Container>::top()
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY(c.empty()))
EASTL_FAIL_MSG("stack::top -- empty container");
#endif
return c.back();
}
template <typename T, typename Container>
inline typename stack<T, Container>::const_reference
stack<T, Container>::top() const
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY(c.empty()))
EASTL_FAIL_MSG("stack::top -- empty container");
#endif
return c.back();
}
template <typename T, typename Container>
inline void stack<T, Container>::push(const value_type& value)
{
c.pushBack(const_cast<value_type&>(value)); // const_cast so that intrusive_list can work. We may revisit this.
}
template <typename T, typename Container>
inline void stack<T, Container>::push(value_type&& x)
{
c.pushBack(eastl::move(x));
}
template <typename T, typename Container>
template <class... Args>
inline void stack<T, Container>::emplace_back(Args&&... args)
{
emplace(eastl::forward<Args>(args)...);
}
template <typename T, typename Container>
template <class... Args>
inline decltype(auto) stack<T, Container>::emplace(Args&&... args)
{
return c.emplace_back(eastl::forward<Args>(args)...);
}
template <typename T, typename Container>
inline void stack<T, Container>::pop()
{
#if EASTL_ASSERT_ENABLED
if (EASTL_UNLIKELY(c.empty()))
EASTL_FAIL_MSG("stack::pop -- empty container");
#endif
c.popBack();
}
template <typename T, typename Container>
inline typename stack<T, Container>::container_type&
stack<T, Container>::get_container()
{
return c;
}
template <typename T, typename Container>
inline const typename stack<T, Container>::container_type&
stack<T, Container>::get_container() const
{
return c;
}
template <typename T, typename Container>
void stack<T, Container>::swap(this_type& x) EASTL_NOEXCEPT_IF(eastl::is_nothrow_swappable<this_type::container_type>::value)
{
using eastl::swap;
swap(c, x.c);
}
template <typename T, typename Container>
bool stack<T, Container>::validate() const
{
return c.validate();
}
///////////////////////////////////////////////////////////////////////
// global operators
///////////////////////////////////////////////////////////////////////
template <typename T, typename Container>
inline bool operator==(const stack<T, Container>& a, const stack<T, Container>& b)
{
return (a.c == b.c);
}
#if defined(EA_COMPILER_HAS_THREE_WAY_COMPARISON)
template <typename T, typename Container> requires std::three_way_comparable<Container>
inline synth_three_way_result<T> operator<=>(const stack<T, Container>& a, const stack<T, Container>& b)
{
return a.c <=> b.c;
}
#endif
template <typename T, typename Container>
inline bool operator!=(const stack<T, Container>& a, const stack<T, Container>& b)
{
return !(a.c == b.c);
}
template <typename T, typename Container>
inline bool operator<(const stack<T, Container>& a, const stack<T, Container>& b)
{
return (a.c < b.c);
}
template <typename T, typename Container>
inline bool operator>(const stack<T, Container>& a, const stack<T, Container>& b)
{
return (b.c < a.c);
}
template <typename T, typename Container>
inline bool operator<=(const stack<T, Container>& a, const stack<T, Container>& b)
{
return !(b.c < a.c);
}
template <typename T, typename Container>
inline bool operator>=(const stack<T, Container>& a, const stack<T, Container>& b)
{
return !(a.c < b.c);
}
template <typename T, typename Container>
inline void swap(stack<T, Container>& a, stack<T, Container>& b) EASTL_NOEXCEPT_IF((eastl::is_nothrow_swappable<typename stack<T, Container>::container_type>::value))
{
a.swap(b);
}
} // namespace eastl
#endif // Header include guard