MADARA  3.1.8
LStack.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _MADARA_LSTACK_H_
3 #define _MADARA_LSTACK_H_
4 
5 // This header defines "size_t"
6 #include <stdlib.h>
7 #include <stdexcept>
8 
9 namespace madara
10 {
11  namespace utility
12  {
13  // Solve circular include problem via forward decls.
14  template <typename T>
15  class LStackNode;
16 
17  template <typename T>
19 
20  template <typename T>
22 
28  template <class T>
29  class LStack
30  {
31  friend class LStackIterator<T>;
32  friend class LStackConstIterator<T>;
33  public:
34  // Define a "trait"
35  typedef T value_type;
36 
42  class Underflow {};
43 
49  class Overflow {};
50 
52  LStack (size_t size_hint = 0);
53 
55  LStack (const LStack<T> &rhs);
56 
58  LStack<T> &operator = (const LStack<T> &rhs);
59 
61  ~LStack (void);
62 
66  void push (const T &new_item);
67 
70  T pop (void);
71 
74  T top (void) const;
75 
77  bool is_empty (void) const;
78 
80  bool is_full (void) const;
81 
83  size_t size (void) const;
84 
88  bool operator== (const LStack<T> &rhs) const;
89 
90  // Compare this stack with @a rhs for inequality such that @a
91  // *this!=s is always the complement of the boolean return value of
92  // @a *this==s.
93  bool operator!= (const LStack<T> &s) const;
94 
96  void erase (void);
97 
100 
102  iterator begin (void);
103 
105  const_iterator begin (void) const;
106 
108  iterator end (void);
109 
111  const_iterator end (void) const;
112 
113  protected:
115  void pop_i (void);
116 
119  void copy_list (const LStack<T> &rhs);
120 
122  void delete_list (void);
123 
124  private:
129 
131  size_t count_;
132  };
133 
142  template <typename T>
143  class LStackIterator
144  {
145  public:
147  LStackIterator (LStack<T> &stack,
148  size_t pos = 0);
149 
151  LStackIterator (LStack<T> &stack,
152  LStackNode<T> *pos = 0);
153 
156  T& operator* (void);
157 
159  const T& operator* (void) const;
160 
163 
166 
168  bool operator== (const LStackIterator<T> &rhs) const;
169 
171  bool operator!= (const LStackIterator<T> &lhs) const;
172 
173  // = Necessary traits
174  typedef ::std::forward_iterator_tag iterator_category;
175  typedef T value_type;
176  typedef T *pointer;
177  typedef T &reference;
178  typedef int difference_type;
179 
180  private:
183 
184  // the position in the linked list
185  mutable LStackNode<T> *pos_;
186  };
187 
196  template <typename T>
197  class LStackConstIterator
198  {
199  public:
201  LStackConstIterator (const LStack<T> &stack,
202  size_t pos = 0);
203 
205  LStackConstIterator (const LStack<T> &stack,
206  LStackNode<T> *pos);
207 
210  const T& operator* (void) const;
211 
213  const LStackConstIterator<T> &operator++ (void) const;
214 
217 
219  bool operator== (const LStackConstIterator<T> &rhs) const;
220 
222  bool operator!= (const LStackConstIterator<T> &lhs) const;
223 
224  // = Necessary traits
225  typedef ::std::forward_iterator_tag iterator_category;
226  typedef T value_type;
227  typedef T *pointer;
228  typedef T &reference;
229  typedef int difference_type;
230 
231  private:
234 
235  // the position in the linked list
236  mutable LStackNode<T> *pos_;
237  };
238  }
239 }
240 
241 #include "LStack.cpp"
242 
243 #endif /* _MADARA_LSTACK_H_ */
size_t count_
Number of items that are currently in the stack.
Definition: LStack.h:131
T top(void) const
Returns the front stack item without removing it.
Definition: LStack.cpp:405
LStack< T > & operator=(const LStack< T > &rhs)
Assignment operator.
Definition: LStack.cpp:290
bool is_empty(void) const
Returns 1 if the stack is empty, otherwise returns 0.
Definition: LStack.cpp:418
T pop(void)
Remove and return the front item on the stack.
Definition: LStack.cpp:366
size_t size(void) const
Returns the current number of elements in the stack.
Definition: LStack.cpp:182
bool is_full(void) const
Returns 1 if the stack is full, otherwise returns 0.
Definition: LStack.cpp:426
bool operator==(const LStack< T > &rhs) const
Compare this stack with rhs for equality.
Definition: LStack.cpp:318
~LStack(void)
Perform actions needed when stack goes out of scope.
Definition: LStack.cpp:308
iterator begin(void)
Get an iterator that points to the beginning of the stack.
Definition: LStack.cpp:434
void pop_i(void)
Remove the front item on the stack. does not throw exceptions.
Definition: LStack.cpp:386
auto operator*(const Tracked< T > &lhs, const Tracked< U > &rhs) -> decltype(lhs.get()*rhs.get())
Definition: Tracked.h:276
LStack< T > & stack_
the stack we are dealing with
Definition: LStack.h:182
void erase(void)
Delete all the nodes in the LStack.
Definition: LStack.cpp:283
bool operator!=(const LStack< T > &s) const
Definition: LStack.cpp:330
::std::forward_iterator_tag iterator_category
Definition: LStack.h:174
LStackNode< T > * pos_
Definition: LStack.h:185
::std::forward_iterator_tag iterator_category
Definition: LStack.h:225
Implements a forward iterator for LStack type classes.
Definition: LStack.h:18
void copy_list(const LStack< T > &rhs)
Copy a linked list of nodes.
Definition: LStack.cpp:224
iterator end(void)
Get an iterator that points to the end of the stack.
Definition: LStack.cpp:442
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
LStackIterator< T > iterator
Definition: LStack.h:98
Exception thrown by methods in this class when an underflow condition occurs.
Definition: LStack.h:42
LStack(size_t size_hint=0)
Constructor.
Definition: LStack.cpp:190
Defines a generic "last-in/first-out" (LIFO) Abstract Data Type (ADT) using a stack that&#39;s implemente...
Definition: LStack.h:29
Exception thrown by methods in this class when an overflow condition occurs.
Definition: LStack.h:49
Copyright (c) 2015 Carnegie Mellon University.
Implements a forward iterator for LStack type classes.
Definition: LStack.h:21
const LStack< T > & stack_
the stack we are dealing with
Definition: LStack.h:233
Defines a node in the LStack that&#39;s implemented as a linked list.
Definition: LStack.cpp:21
LStackConstIterator< T > const_iterator
Definition: LStack.h:99
void push(const T &new_item)
Place a new_item at the tail of the stack.
Definition: LStack.cpp:340
LStackNode< T > * head_
We only need to keep a single pointer for the circular linked list.
Definition: LStack.h:128
void delete_list(void)
Delete a linked list of nodes.
Definition: LStack.cpp:271
auto operator++(Tracked< T > &lhs) -> decltype(++lhs.get_mut())
Definition: Tracked.h:301