ROSE  0.11.96.11
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
Sawyer::Container::Stack::pop
Value pop()
Pop existing item from stack.
Definition: Stack.h:98
Sawyer::Container::IndexedList::pushBack
IndexedList & pushBack(const Value &value)
Insert at the back of the list.
Definition: IndexedList.h:628
Sawyer::Container::Stack::Stack
Stack()
Construct an empty stack.
Definition: Stack.h:29
Sawyer::Container::Stack::push
Stack & push(const Value &value)
Push new item onto stack.
Definition: Stack.h:89
Sawyer::Container::IndexedList::erase
NodeIterator erase(size_t id)
Erase one element.
Definition: IndexedList.h:685
Sawyer::Container::Stack::isEmpty
bool isEmpty() const
Determines if the stack is empty.
Definition: Stack.h:48
Sawyer::Container::Stack::operator[]
Value & operator[](size_t idx)
Access an item not at the top of the stack.
Definition: Stack.h:81
Sawyer::Container::IndexedList::isEmpty
bool isEmpty() const
Determines if this list is empty.
Definition: IndexedList.h:466
Sawyer::Container::IndexedList::backValue
Value & backValue()
Last element of list.
Definition: IndexedList.h:556
Sawyer::Container::Stack
Stack-based container.
Definition: Stack.h:22
Sawyer::Container::Stack::get
const Value & get(size_t idx) const
Access an item not at the top of the stack.
Definition: Stack.h:77
Sawyer::Container::Stack::top
Value & top()
Returns the top item.
Definition: Stack.h:57
Sawyer::Container::IndexedList::size
size_t size() const
Number of elements in list.
Definition: IndexedList.h:473
Sawyer::Container::Stack::Stack
Stack(const boost::iterator_range< Iterator > &range)
Construct a stack from an iterator range.
Definition: Stack.h:33
Sawyer::Container::Stack::get
Value & get(size_t idx)
Access an item not at the top of the stack.
Definition: Stack.h:73
Sawyer::Container::Stack::size
size_t size() const
Returns the number of items on the stack.
Definition: Stack.h:41
Sawyer::Container::Stack::operator[]
const Value & operator[](size_t idx) const
Access an item not at the top of the stack.
Definition: Stack.h:82
Sawyer
Name space for the entire library.
Definition: Access.h:13
Sawyer::Container::Stack::top
const Value & top() const
Returns the top item.
Definition: Stack.h:61
Sawyer::Container::IndexedList< T >