MADARA  3.4.1
LQueue.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _MADARA_LQUEUE_H
3 #define _MADARA_LQUEUE_H
4 
5 // This header defines "size_t"
6 #include <stdlib.h>
7 
8 #include <stdexcept>
9 
10 namespace madara
11 {
12 namespace utility
13 {
14 // Solve circular include problem via forward decls.
15 template<typename T>
16 class LQueueNode;
17 
18 template<typename T>
19 class LQueueIterator;
20 
21 template<typename T>
23 
37 template<class T>
38 class LQueue
39 {
40  friend class LQueueIterator<T>;
41  friend class LQueueConstIterator<T>;
42 
43 public:
44  // Define a "trait"
45  typedef T value_type;
46 
52  class Underflow
53  {
54  };
55 
61  class Overflow
62  {
63  };
64 
66  LQueue(size_t size_hint = 0);
67 
69  LQueue(const LQueue<T>& rhs);
70 
72  LQueue<T>& operator=(const LQueue<T>& rhs);
73 
75  ~LQueue(void);
76 
80  void enqueue(const T& new_item);
81 
84  T dequeue(void);
85 
88  T front(void) const;
89 
91  bool is_empty(void) const;
92 
94  bool is_full(void) const;
95 
97  size_t size(void) const;
98 
102  bool operator==(const LQueue<T>& rhs) const;
103 
107  bool operator!=(const LQueue<T>& s) const;
108 
111 
113  iterator begin(void);
114 
116  const_iterator begin(void) const;
117 
119  iterator end(void);
120 
122  const_iterator end(void) const;
123 
124 protected:
126  void dequeue_i(void);
127 
128  // Copy a linked list of nodes. This can throw a @a ::std::bad_alloc
129  // exception.
130  void copy_list(const LQueue<T>& rhs);
131 
132  // Delete a linked list of nodes.
133  void delete_list(void);
134 
135 private:
140 
142  size_t count_;
143 };
144 
153 template<typename T>
155 {
156 public:
158  LQueueIterator(LQueue<T>& queue, size_t pos = 0);
159 
161  LQueueIterator(LQueue<T>& queue, LQueueNode<T>* pos = 0);
162 
165  T& operator*(void);
166 
168  const T& operator*(void)const;
169 
172 
175 
177  bool operator==(const LQueueIterator<T>& rhs) const;
178 
180  bool operator!=(const LQueueIterator<T>& lhs) const;
181 
182  // = Necessary traits
183  typedef ::std::forward_iterator_tag iterator_category;
184  typedef T value_type;
185  typedef T* pointer;
186  typedef T& reference;
187  typedef int difference_type;
188 
189 private:
192 
193  // the position in the linked list
194  mutable LQueueNode<T>* pos_;
195 };
196 
205 template<typename T>
207 {
208 public:
210  LQueueConstIterator(const LQueue<T>& queue, size_t pos = 0);
211 
213  LQueueConstIterator(const LQueue<T>& queue, LQueueNode<T>* pos);
214 
217  const T& operator*(void)const;
218 
220  const LQueueConstIterator<T>& operator++(void)const;
221 
224 
226  bool operator==(const LQueueConstIterator<T>& rhs) const;
227 
229  bool operator!=(const LQueueConstIterator<T>& lhs) const;
230 
231  // = Necessary traits
232  typedef ::std::forward_iterator_tag iterator_category;
233  typedef T value_type;
234  typedef T* pointer;
235  typedef T& reference;
236  typedef int difference_type;
237 
238 private:
241 
242  // the position in the linked list
243  mutable LQueueNode<T>* pos_;
244 };
245 }
246 }
247 
248 #include "LQueue.cpp"
249 #endif /* _MADARA_LQUEUE_H */
Implements a forward iterator for LQueue type classes.
Definition: LQueue.h:207
::std::forward_iterator_tag iterator_category
Definition: LQueue.h:232
const LQueueConstIterator< T > & operator++(void) const
Preincrement operator.
Definition: LQueue.cpp:521
const T & operator*(void) const
Dereference operator returns a const reference to the item contained at the current position.
Definition: LQueue.cpp:514
const LQueue< T > & queue_
the queue we are dealing with
Definition: LQueue.h:240
LQueueConstIterator(const LQueue< T > &queue, size_t pos=0)
Construct an LQueueIterator at position pos.
Definition: LQueue.cpp:559
bool operator!=(const LQueueConstIterator< T > &lhs) const
Nonequality operator.
Definition: LQueue.cpp:552
bool operator==(const LQueueConstIterator< T > &rhs) const
Equality operator.
Definition: LQueue.cpp:544
Implements a forward iterator for LQueue type classes.
Definition: LQueue.h:155
LQueue< T > & queue_
the queue we are dealing with
Definition: LQueue.h:191
LQueueIterator(LQueue< T > &queue, size_t pos=0)
Construct an LQueueIterator at position pos.
Definition: LQueue.cpp:493
bool operator==(const LQueueIterator< T > &rhs) const
Equality operator.
Definition: LQueue.cpp:475
bool operator!=(const LQueueIterator< T > &lhs) const
Nonequality operator.
Definition: LQueue.cpp:485
::std::forward_iterator_tag iterator_category
Definition: LQueue.h:183
T & operator*(void)
Dereference operator returns a reference to the item contained at the current position.
Definition: LQueue.cpp:438
LQueueIterator< T > & operator++(void)
Preincrement operator.
Definition: LQueue.cpp:451
LQueueNode< T > * pos_
Definition: LQueue.h:194
Defines a node in the LQueue that's implemented as a circular linked list.
Definition: LQueue.cpp:24
Exception thrown by methods in this class when an overflow condition occurs.
Definition: LQueue.h:62
Exception thrown by methods in this class when an underflow condition occurs.
Definition: LQueue.h:53
Defines a generic "first-in/first-out" (FIFO) Abstract Data Type (ADT) using a circular linked list.
Definition: LQueue.h:39
LQueue< T > & operator=(const LQueue< T > &rhs)
Assignment operator.
Definition: LQueue.cpp:253
T front(void) const
Returns the front queue item without removing it.
Definition: LQueue.cpp:374
bool is_full(void) const
Returns 1 if the queue is full, otherwise returns 0.
Definition: LQueue.cpp:395
~LQueue(void)
Perform actions needed when queue goes out of scope.
Definition: LQueue.cpp:272
iterator begin(void)
Get an iterator that points to the beginning of the queue.
Definition: LQueue.cpp:403
LQueueNode< T > * tail_
We only need to keep a single pointer for the circular linked list.
Definition: LQueue.h:139
T dequeue(void)
Remove and return the front item on the queue.
Definition: LQueue.cpp:335
LQueue(size_t size_hint=0)
Constructor.
Definition: LQueue.cpp:185
LQueueConstIterator< T > const_iterator
Definition: LQueue.h:110
void enqueue(const T &new_item)
Place a new_item at the tail of the queue.
Definition: LQueue.cpp:306
bool operator==(const LQueue< T > &rhs) const
Compare this queue with rhs for equality.
Definition: LQueue.cpp:285
size_t size(void) const
Returns the current number of elements in the queue.
Definition: LQueue.cpp:177
bool operator!=(const LQueue< T > &s) const
Compare this queue with rhs for inequality such that *this>!=s is always the complement of the boolea...
Definition: LQueue.cpp:296
size_t count_
Number of items that are currently in the queue.
Definition: LQueue.h:142
void delete_list(void)
Definition: LQueue.cpp:241
bool is_empty(void) const
Returns 1 if the queue is empty, otherwise returns 0.
Definition: LQueue.cpp:387
void dequeue_i(void)
Remove the front item on the queue. Does not throw exceptions.
Definition: LQueue.cpp:355
void copy_list(const LQueue< T > &rhs)
Definition: LQueue.cpp:219
iterator end(void)
Get an iterator that points to the end of the queue.
Definition: LQueue.cpp:412
LQueueIterator< T > iterator
Definition: LQueue.h:109
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:15
Copyright(c) 2020 Galois.