ROSE  0.9.9.139
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 <vector>
13 
14 namespace Sawyer {
15 namespace Container {
16 
21 template<typename T>
22 class Stack {
23 public:
24  typedef T Value;
25 private:
26  std::vector<T> vector_;
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  vector_.push_back(*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 vector_.size();
43  }
44 
48  bool isEmpty() const {
49  return vector_.empty();
50  }
51 
57  Value& top() {
58  ASSERT_forbid(isEmpty());
59  return vector_.back();
60  }
61  const Value& top() const {
62  ASSERT_forbid(isEmpty());
63  return vector_.back();
64  }
73  Value& get(size_t idx) {
74  ASSERT_require(idx < size());
75  return vector_[vector_.size() - (idx+1)];
76  }
77  const Value& get(size_t idx) const {
78  ASSERT_require(idx < size());
79  return vector_[vector_.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  vector_.push_back(value);
91  return *this;
92  }
93 
98  Value pop() {
99  Value v = top();
100  vector_.pop_back();
101  return v;
102  }
103 };
104 
105 } // namespace
106 } // namespace
107 
108 #endif
Stack(const boost::iterator_range< Iterator > &range)
Construct a stack from an iterator range.
Definition: Stack.h:33
Name space for the entire library.
Definition: Access.h:11
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