ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ArrayQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File ArrayQueue.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
29/*--------------------------------------------------------------------------*/
30/*----------------------------- DEFINITIONS --------------------------------*/
31/*--------------------------------------------------------------------------*/
32
33#ifndef __ADS_ArrayQueue
34 #define __ADS_ArrayQueue
35/* self-identification: #endif constant */
36
37/*--------------------------------------------------------------------------*/
38/*------------------------------ INCLUDES ----------------------------------*/
39/*--------------------------------------------------------------------------*/
40
41#include "ADS/Contract.h"
42#include "ADS/Iterators.h"
43
44#include <cstddef>
45#include <memory>
46#include <utility>
47
48/*--------------------------------------------------------------------------*/
49/*--------------------------- NAMESPACE ------------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52namespace ADS
53{
54
55/*--------------------------------------------------------------------------*/
56/*---------------------------- CLASS ArrayQueue ----------------------------*/
57/*--------------------------------------------------------------------------*/
58
60template< class T >
62{
63
64/*--------------------------------------------------------------------------*/
65 public:
66 using Item = T;
67
69 explicit ArrayQueue( std::size_t dim );
70
73 ArrayQueue( const ArrayQueue & other );
74
76 ArrayQueue( ArrayQueue && other ) noexcept;
77
79 ArrayQueue & operator=( ArrayQueue other ) noexcept;
80
82 void swap( ArrayQueue & other ) noexcept;
83
85 [[nodiscard]] bool empty( void ) const;
86
89 [[nodiscard]] const T & top( void ) const;
90
105 T dequeue( void );
106
120 void enqueue( const T & v );
121
123 [[nodiscard]] std::size_t size( void ) const;
124
126 [[nodiscard]] std::size_t capacity( void ) const;
127
129 [[nodiscard]] bool contains( const T & v ) const;
130
131/*------------------------- THE STANDARD INTERFACE -------------------------*/
132
133 // a constant view of the items from the front to the back, random
134 // access on the circular vector
135
136 using value_type = T;
137 using size_type = std::size_t;
138 using difference_type = std::ptrdiff_t;
139 using reference = const T &;
140 using const_reference = const T &;
143
145 [[nodiscard]] const_iterator begin( void ) const {
146 return( const_iterator( f_A.get() , f_head , f_capacity , 0 ) );
147 }
148
149 [[nodiscard]] const_iterator end( void ) const {
150 return( const_iterator( f_A.get() , f_head , f_capacity ,
151 std::ptrdiff_t( f_size ) ) );
152 }
153
154 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
155
157 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
158
159/*--------------------------------------------------------------------------*/
160 private:
162 [[nodiscard]] bool fits( void ) const;
163
164 std::unique_ptr< T[] > f_A;
165 std::size_t f_size;
166 std::size_t f_head;
167 std::size_t f_capacity;
168
169 }; // end( class ArrayQueue )
170
171/*--------------------------------------------------------------------------*/
172
173} // end( namespace ADS )
174
175/*--------------------------------------------------------------------------*/
176/*-------------------------- IMPLEMENTATION --------------------------------*/
177/*--------------------------------------------------------------------------*/
178
179#include "ADS/queue/impl/ArrayQueue.tpp"
180
181/*--------------------------------------------------------------------------*/
182
183#endif /* ArrayQueue.h included */
184
185/*--------------------------------------------------------------------------*/
186/*------------------------- End File ArrayQueue.h --------------------------*/
187/*--------------------------------------------------------------------------*/
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...
ArrayQueue(const ArrayQueue &other)
a copy of other, item by item, from the place 0 of a vector of the same capacity
const_iterator end(void) const
the iterator past the item at the back
Definition ArrayQueue.h:149
T Item
the type of the items
Definition ArrayQueue.h:66
std::size_t size(void) const
the number of items,
void swap(ArrayQueue &other) noexcept
exchanges the items and the capacities with other
const_iterator cend(void) const
as end()
Definition ArrayQueue.h:157
const_iterator begin(void) const
the iterator on the item at the front
Definition ArrayQueue.h:145
bool empty(void) const
true if there is no item, i.e.,
std::ptrdiff_t difference_type
the type of the distances
Definition ArrayQueue.h:138
void enqueue(const T &v)
puts at the back
const T & top(void) const
the item at the front,
std::size_t size_type
the type of the sizes
Definition ArrayQueue.h:137
CircularIterator< T > const_iterator
a constant iterator
Definition ArrayQueue.h:142
const T & reference
a constant reference
Definition ArrayQueue.h:139
T dequeue(void)
removes the item at the front and returns it
const_iterator cbegin(void) const
as begin()
Definition ArrayQueue.h:154
ArrayQueue & operator=(ArrayQueue other) noexcept
the items of other, by copy and swap
ArrayQueue(ArrayQueue &&other) noexcept
the items of other, which is left empty with no capacity
const T & const_reference
a constant reference
Definition ArrayQueue.h:140
T value_type
the type of the items
Definition ArrayQueue.h:136
std::size_t capacity(void) const
the maximum number of items,
CircularIterator< T > iterator
a constant iterator
Definition ArrayQueue.h:141
bool contains(const T &v) const
whether is in the queue,
ArrayQueue(std::size_t dim)
the empty queue of at most items
the random-access iterator on the items of a circular vector of the capacity whose first item is at ...
Definition Iterators.h:265
Definition ADT.h:96