ROSE  0.11.145.0
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://github.com/matzke1/sawyer.
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 
14 namespace Sawyer {
15 namespace Container {
16 
21 template<typename T>
22 class Stack {
23 public:
24  typedef T Value;
25 private:
26  IndexedList<T> items_;
27 public:
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
size_t size() const
Number of elements in list.
Definition: IndexedList.h:478
Stack(const boost::iterator_range< Iterator > &range)
Construct a stack from an iterator range.
Definition: Stack.h:33
bool isEmpty() const
Determines if this list is empty.
Definition: IndexedList.h:471
Value & backValue()
Last element of list.
Definition: IndexedList.h:561
NodeIterator erase(size_t id)
Erase one element.
Definition: IndexedList.h:690
Name space for the entire library.
Definition: FeasiblePath.h:767
IndexedList & pushBack(const Value &value)
Insert at the back of the list.
Definition: IndexedList.h:633
Stack & push(const Value &value)
Push new item onto stack.
Definition: Stack.h:89
Stack()
Construct an empty stack.
Definition: Stack.h:29
size_t size() const
Returns the number of items on the stack.
Definition: Stack.h:41
Value & operator[](size_t idx)
Access an item not at the top of the stack.
Definition: Stack.h:81
Value pop()
Pop existing item from stack.
Definition: Stack.h:98
const Value & top() const
Returns the top item.
Definition: Stack.h:61
const Value & operator[](size_t idx) const
Access an item not at the top of the stack.
Definition: Stack.h:82
Stack-based container.
Definition: Stack.h:22
bool isEmpty() const
Determines if the stack is empty.
Definition: Stack.h:48
Value & top()
Returns the top item.
Definition: Stack.h:57