ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LinkedQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File LinkedQueue.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
29/*--------------------------------------------------------------------------*/
30/*----------------------------- DEFINITIONS --------------------------------*/
31/*--------------------------------------------------------------------------*/
32
33#ifndef __ADS_LinkedQueue
34 #define __ADS_LinkedQueue
35/* self-identification: #endif constant */
36
37/*--------------------------------------------------------------------------*/
38/*------------------------------ INCLUDES ----------------------------------*/
39/*--------------------------------------------------------------------------*/
40
41#include "ADS/Contract.h"
42#include "ADS/Iterators.h"
44
45#include <cstddef>
46#include <utility>
47
48/*--------------------------------------------------------------------------*/
49/*--------------------------- NAMESPACE ------------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52namespace ADS
53{
54
55/*--------------------------------------------------------------------------*/
56/*-------------------------- CLASS LinkedQueue -----------------------------*/
57/*--------------------------------------------------------------------------*/
58
60template< class T >
62{
63
64/*--------------------------------------------------------------------------*/
65 public:
66 using Item = T;
67
69 LinkedQueue( void ) = default;
70
72 LinkedQueue( const LinkedQueue & other );
73
75 LinkedQueue( LinkedQueue && other ) noexcept;
76
78 LinkedQueue & operator=( LinkedQueue other ) noexcept;
79
82
84 void swap( LinkedQueue & other ) noexcept;
85
87 [[nodiscard]] bool empty( void ) const;
88
105 void enqueue( const T & v );
106
122 T dequeue( void );
123
126 [[nodiscard]] const T & top( void ) const;
127
129 [[nodiscard]] std::size_t size( void ) const;
130
132 [[nodiscard]] bool operator==( const LinkedQueue & o ) const;
133
134/*--------------------------------------------------------------------------*/
135 private:
137 struct Cell
138 {
139 T value;
140 Cell * next;
141 };
142
143 public:
144/*------------------------- THE STANDARD INTERFACE -------------------------*/
145
146 // a constant view of the items from the front to the back, forward
147
148 using value_type = T;
149 using size_type = std::size_t;
150 using difference_type = std::ptrdiff_t;
151 using reference = const T &;
152 using const_reference = const T &;
156 using const_iterator = LinkedIterator< Cell , T , &Cell::next ,
157 &Cell::value >;
158
160 [[nodiscard]] const_iterator begin( void ) const {
161 return( const_iterator( f_front ) );
162 }
163
165 [[nodiscard]] const_iterator end( void ) const {
166 return( const_iterator() );
167 }
168
170 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
171
173 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
174
175 private:
176 Cell * f_front = nullptr;
177 Cell * f_back = nullptr;
178 std::size_t f_size = 0;
179 ObjectPool< Cell > f_pool;
180
181 }; // end( class LinkedQueue )
182
183/*--------------------------------------------------------------------------*/
184
185} // end( namespace ADS )
186
187/*--------------------------------------------------------------------------*/
188/*-------------------------- IMPLEMENTATION --------------------------------*/
189/*--------------------------------------------------------------------------*/
190
191#include "ADS/queue/impl/LinkedQueue.tpp"
192
193/*--------------------------------------------------------------------------*/
194
195#endif /* LinkedQueue.h included */
196
197/*--------------------------------------------------------------------------*/
198/*------------------------- End File LinkedQueue.h -------------------------*/
199/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
A pool of objects of one type, beyond the book: make() builds an object in a slot of the pool and fre...
a forward iterator along the cells C linked by the member Next, on the items V in the member Value; n...
Definition Iterators.h:132
const T & top(void) const
the item at the front
LinkedIterator< Cell, T, &Cell::next, &Cell::value > const_iterator
a constant iterator along the cells
Definition LinkedQueue.h:156
LinkedQueue & operator=(LinkedQueue other) noexcept
the items of other, by copy and swap
const_iterator begin(void) const
the iterator on the item at the front
Definition LinkedQueue.h:160
const T & reference
a constant reference
Definition LinkedQueue.h:151
LinkedQueue(void)=default
the empty queue
const_iterator end(void) const
the iterator past the item at the back, nil
Definition LinkedQueue.h:165
void swap(LinkedQueue &other) noexcept
exchanges the cells with other
T dequeue(void)
removes the item at the front and returns it
T Item
the type of the items
Definition LinkedQueue.h:66
std::ptrdiff_t difference_type
the type of the distances
Definition LinkedQueue.h:150
LinkedQueue(const LinkedQueue &other)
a copy of other, cell by cell
LinkedIterator< Cell, T, &Cell::next, &Cell::value > iterator
a constant iterator along the cells
Definition LinkedQueue.h:154
const_iterator cbegin(void) const
as begin()
Definition LinkedQueue.h:170
~LinkedQueue()
frees the cells
void enqueue(const T &v)
puts at the back
bool operator==(const LinkedQueue &o) const
the same items in the same order
std::size_t size_type
the type of the sizes
Definition LinkedQueue.h:149
bool empty(void) const
true if there is no item
const T & const_reference
a constant reference
Definition LinkedQueue.h:152
T value_type
the type of the items
Definition LinkedQueue.h:148
std::size_t size(void) const
the number of items,
const_iterator cend(void) const
as end()
Definition LinkedQueue.h:173
LinkedQueue(LinkedQueue &&other) noexcept
the cells of other, which is left empty
Definition ADT.h:96