ROSE 0.11.145.192
Stack.h
1// WARNING: Changes to this file must be contributed back to Sawyer or else they will
2// be clobbered by the next update from Sawyer. The Sawyer repository is at
3// https://gitlab.com/charger7534/sawyer.git.
4
5
6
7
8#ifndef Sawyer_Stack_H
9#define Sawyer_Stack_H
10
11#include <Sawyer/Assert.h>
12#include <Sawyer/IndexedList.h>
13
14namespace Sawyer {
15namespace Container {
16
21template<typename T>
22class Stack {
23public:
24 typedef T Value;
25private:
26 IndexedList<T> items_;
27public:
29 Stack() {}
30
32 template<class Iterator>
33 Stack(const boost::iterator_range<Iterator> &range) {
34 for (Iterator iter = range.begin(); iter != range.end(); ++iter)
35 items_.pushBack(*iter);
36 }
37
38 // FIXME[Robb P. Matzke 2014-08-06]: we need iterators, values(), begin(), end(), etc.
39
41 size_t size() const {
42 return items_.size();
43 }
44
48 bool isEmpty() const {
49 return items_.isEmpty();
50 }
51
57 Value& top() {
58 ASSERT_forbid(isEmpty());
59 return items_.backValue();
60 }
61 const Value& top() const {
62 ASSERT_forbid(isEmpty());
63 return items_.backValue();
64 }
73 Value& get(size_t idx) {
74 ASSERT_require(idx < size());
75 return items_[items_.size() - (idx+1)];
76 }
77 const Value& get(size_t idx) const {
78 ASSERT_require(idx < size());
79 return items_[items_.size() - (idx+1)];
80 }
81 Value& operator[](size_t idx) { return get(idx); }
82 const Value& operator[](size_t idx) const { return get(idx); }
89 Stack& push(const Value &value) {
90 items_.pushBack(value);
91 return *this;
92 }
93
98 Value pop() {
99 Value v = top();
100 items_.erase(items_.size()-1);
101 return v;
102 }
103};
104
105} // namespace
106} // namespace
107
108#endif
Doubly-linked list with constant-time indexing.
Definition IndexedList.h:75
bool isEmpty() const
Determines if this list is empty.
Value & backValue()
Last element of list.
NodeIterator erase(size_t id)
Erase one element.
IndexedList & pushBack(const Value &value)
Insert at the back of the list.
size_t size() const
Number of elements in list.
Stack-based container.
Definition Stack.h:22
const Value & operator[](size_t idx) const
Access an item not at the top of the stack.
Definition Stack.h:82
const Value & get(size_t idx) const
Access an item not at the top of the stack.
Definition Stack.h:77
Stack(const boost::iterator_range< Iterator > &range)
Construct a stack from an iterator range.
Definition Stack.h:33
size_t size() const
Returns the number of items on the stack.
Definition Stack.h:41
Value & top()
Returns the top item.
Definition Stack.h:57
Value pop()
Pop existing item from stack.
Definition Stack.h:98
Stack()
Construct an empty stack.
Definition Stack.h:29
const Value & top() const
Returns the top item.
Definition Stack.h:61
Value & operator[](size_t idx)
Access an item not at the top of the stack.
Definition Stack.h:81
Stack & push(const Value &value)
Push new item onto stack.
Definition Stack.h:89
Value & get(size_t idx)
Access an item not at the top of the stack.
Definition Stack.h:73
bool isEmpty() const
Determines if the stack is empty.
Definition Stack.h:48
Sawyer support library.