MADARA  3.4.1
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>
18 class LStackIterator;
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 
34 public:
35  // Define a "trait"
36  typedef T value_type;
37 
43  class Underflow
44  {
45  };
46 
52  class Overflow
53  {
54  };
55 
57  LStack(size_t size_hint = 0);
58 
60  LStack(const LStack<T>& rhs);
61 
63  LStack<T>& operator=(const LStack<T>& rhs);
64 
66  ~LStack(void);
67 
71  void push(const T& new_item);
72 
75  T pop(void);
76 
79  T top(void) const;
80 
82  bool is_empty(void) const;
83 
85  bool is_full(void) const;
86 
88  size_t size(void) const;
89 
93  bool operator==(const LStack<T>& rhs) const;
94 
95  // Compare this stack with @a rhs for inequality such that @a
96  // *this!=s is always the complement of the boolean return value of
97  // @a *this==s.
98  bool operator!=(const LStack<T>& s) const;
99 
101  void erase(void);
102 
105 
107  iterator begin(void);
108 
110  const_iterator begin(void) const;
111 
113  iterator end(void);
114 
116  const_iterator end(void) const;
117 
118 protected:
120  void pop_i(void);
121 
124  void copy_list(const LStack<T>& rhs);
125 
127  void delete_list(void);
128 
129 private:
134 
136  size_t count_;
137 };
138 
147 template<typename T>
149 {
150 public:
152  LStackIterator(LStack<T>& stack, size_t pos = 0);
153 
155  LStackIterator(LStack<T>& stack, LStackNode<T>* pos = 0);
156 
159  T& operator*(void);
160 
162  const T& operator*(void)const;
163 
166 
169 
171  bool operator==(const LStackIterator<T>& rhs) const;
172 
174  bool operator!=(const LStackIterator<T>& lhs) const;
175 
176  // = Necessary traits
177  typedef ::std::forward_iterator_tag iterator_category;
178  typedef T value_type;
179  typedef T* pointer;
180  typedef T& reference;
181  typedef int difference_type;
182 
183 private:
186 
187  // the position in the linked list
188  mutable LStackNode<T>* pos_;
189 };
190 
199 template<typename T>
201 {
202 public:
204  LStackConstIterator(const LStack<T>& stack, size_t pos = 0);
205 
207  LStackConstIterator(const LStack<T>& stack, LStackNode<T>* pos);
208 
211  const T& operator*(void)const;
212 
214  const LStackConstIterator<T>& operator++(void)const;
215 
218 
220  bool operator==(const LStackConstIterator<T>& rhs) const;
221 
223  bool operator!=(const LStackConstIterator<T>& lhs) const;
224 
225  // = Necessary traits
226  typedef ::std::forward_iterator_tag iterator_category;
227  typedef T value_type;
228  typedef T* pointer;
229  typedef T& reference;
230  typedef int difference_type;
231 
232 private:
235 
236  // the position in the linked list
237  mutable LStackNode<T>* pos_;
238 };
239 }
240 }
241 
242 #include "LStack.cpp"
243 
244 #endif /* _MADARA_LSTACK_H_ */
Implements a forward iterator for LStack type classes.
Definition: LStack.h:201
LStackConstIterator(const LStack< T > &stack, size_t pos=0)
Construct an LStackIterator at position pos.
Definition: LStack.cpp:582
const LStackConstIterator< T > & operator++(void) const
Preincrement operator.
Definition: LStack.cpp:544
const T & operator*(void) const
Dereference operator returns a const reference to the item contained at the current position.
Definition: LStack.cpp:537
const LStack< T > & stack_
the stack we are dealing with
Definition: LStack.h:234
bool operator==(const LStackConstIterator< T > &rhs) const
Equality operator.
Definition: LStack.cpp:567
bool operator!=(const LStackConstIterator< T > &lhs) const
Nonequality operator.
Definition: LStack.cpp:575
::std::forward_iterator_tag iterator_category
Definition: LStack.h:226
Implements a forward iterator for LStack type classes.
Definition: LStack.h:149
bool operator!=(const LStackIterator< T > &lhs) const
Nonequality operator.
Definition: LStack.cpp:508
::std::forward_iterator_tag iterator_category
Definition: LStack.h:177
LStackIterator(LStack< T > &stack, size_t pos=0)
Construct an LStackIterator at position pos.
Definition: LStack.cpp:516
LStackIterator< T > & operator++(void)
Preincrement operator.
Definition: LStack.cpp:474
LStackNode< T > * pos_
Definition: LStack.h:188
bool operator==(const LStackIterator< T > &rhs) const
Equality operator.
Definition: LStack.cpp:498
T & operator*(void)
Dereference operator returns a reference to the item contained at the current position.
Definition: LStack.cpp:461
LStack< T > & stack_
the stack we are dealing with
Definition: LStack.h:185
Defines a node in the LStack that's implemented as a linked list.
Definition: LStack.cpp:23
Exception thrown by methods in this class when an overflow condition occurs.
Definition: LStack.h:53
Exception thrown by methods in this class when an underflow condition occurs.
Definition: LStack.h:44
Defines a generic "last-in/first-out" (LIFO) Abstract Data Type (ADT) using a stack that's implemente...
Definition: LStack.h:30
iterator begin(void)
Get an iterator that points to the beginning of the stack.
Definition: LStack.cpp:426
void pop_i(void)
Remove the front item on the stack. does not throw exceptions.
Definition: LStack.cpp:378
LStack(size_t size_hint=0)
Constructor.
Definition: LStack.cpp:186
~LStack(void)
Perform actions needed when stack goes out of scope.
Definition: LStack.cpp:301
void push(const T &new_item)
Place a new_item at the tail of the stack.
Definition: LStack.cpp:332
bool operator==(const LStack< T > &rhs) const
Compare this stack with rhs for equality.
Definition: LStack.cpp:311
void copy_list(const LStack< T > &rhs)
Copy a linked list of nodes.
Definition: LStack.cpp:217
void delete_list(void)
Delete a linked list of nodes.
Definition: LStack.cpp:263
bool is_full(void) const
Returns 1 if the stack is full, otherwise returns 0.
Definition: LStack.cpp:418
void erase(void)
Delete all the nodes in the LStack.
Definition: LStack.cpp:275
LStack< T > & operator=(const LStack< T > &rhs)
Assignment operator.
Definition: LStack.cpp:282
LStackIterator< T > iterator
Definition: LStack.h:103
LStackNode< T > * head_
We only need to keep a single pointer for the circular linked list.
Definition: LStack.h:133
size_t size(void) const
Returns the current number of elements in the stack.
Definition: LStack.cpp:178
iterator end(void)
Get an iterator that points to the end of the stack.
Definition: LStack.cpp:435
bool operator!=(const LStack< T > &s) const
Definition: LStack.cpp:322
T top(void) const
Returns the front stack item without removing it.
Definition: LStack.cpp:397
size_t count_
Number of items that are currently in the stack.
Definition: LStack.h:136
LStackConstIterator< T > const_iterator
Definition: LStack.h:104
T pop(void)
Remove and return the front item on the stack.
Definition: LStack.cpp:358
bool is_empty(void) const
Returns 1 if the stack is empty, otherwise returns 0.
Definition: LStack.cpp:410
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:15
Copyright(c) 2020 Galois.