ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ThreadedTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File ThreadedTree.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
33/*--------------------------------------------------------------------------*/
34/*----------------------------- DEFINITIONS --------------------------------*/
35/*--------------------------------------------------------------------------*/
36
37#ifndef __ADS_ThreadedTree
38 #define __ADS_ThreadedTree
39/* self-identification: #endif constant */
40
41/*--------------------------------------------------------------------------*/
42/*------------------------------ INCLUDES ----------------------------------*/
43/*--------------------------------------------------------------------------*/
44
45#include "ADS/Contract.h"
46#include "ADS/Iterators.h"
47
48#include <concepts>
49#include <cstddef>
50#include <iterator>
51#include <vector>
52
53/*--------------------------------------------------------------------------*/
54/*--------------------------- NAMESPACE ------------------------------------*/
55/*--------------------------------------------------------------------------*/
56
57namespace ADS
58{
59
60/*--------------------------------------------------------------------------*/
61/*-------------------------- CLASS ThreadedTree ----------------------------*/
62/*--------------------------------------------------------------------------*/
63
65template< std::totally_ordered K >
67{
68
69/*--------------------------------------------------------------------------*/
70 public:
72 ThreadedTree( void );
73
75 ThreadedTree( const ThreadedTree & ) = delete;
76
78 ThreadedTree & operator=( const ThreadedTree & ) = delete;
79
82
109 void insert( const K & k );
110
113 [[nodiscard]] bool contains( const K & k ) const;
114
116 [[nodiscard]] std::size_t size( void ) const;
117
120 template< class F >
121 void forEach( F && f ) const;
122
125 [[nodiscard]] std::vector< K > reversed( void ) const;
126
127/*--------------------------------------------------------------------------*/
128 private:
130 struct Node
131 {
132 K key{};
133 Node *left = nullptr ,
134 *right = nullptr;
135 bool lthread = true ,
136 rthread = true;
137 };
138
139 public:
140/*------------------------- THE STANDARD INTERFACE -------------------------*/
141
142 // a constant view of the keys in increasing order, bidirectional by the
143 // threads with no stack, the head node being the end
144
147 {
148 public:
150 using iterator_category = std::bidirectional_iterator_tag;
151 using value_type = K;
152 using difference_type = std::ptrdiff_t;
153 using reference = const K &;
154 using pointer = const K *;
155
157 const_iterator( void ) = default;
158
160 const_iterator( const ThreadedTree * t , const Node * x )
161 : f_t( t ) , f_x( x ) { }
162
164 reference operator*( void ) const { return( f_x->key ); }
166 pointer operator->( void ) const { return( &f_x->key ); }
169 f_x = f_t->successor( f_x );
170 return( *this );
171 }
172
174 f_x = f_t->predecessor( f_x );
175 return( *this );
176 }
177
179 auto t = *this;
180 ++*this;
181 return( t );
182 }
183
185 auto t = *this;
186 --*this;
187 return( t );
188 }
189
190 bool operator==( const const_iterator & o ) const {
191 return( f_x == o.f_x );
192 }
193
194 private:
195 const ThreadedTree * f_t = nullptr;
196 const Node * f_x = nullptr;
197 };
198
199 using value_type = K;
200 using size_type = std::size_t;
201 using difference_type = std::ptrdiff_t;
202 using reference = const K &;
203 using const_reference = const K &;
205
207 [[nodiscard]] const_iterator begin( void ) const {
208 return( const_iterator( this , successor( &f_head ) ) );
209 }
210
212 [[nodiscard]] const_iterator end( void ) const {
213 return( const_iterator( this , &f_head ) );
214 }
215
217 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
218
220 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
221
222 private:
237 [[nodiscard]] const Node * successor( const Node * x ) const;
238
241 [[nodiscard]] const Node * predecessor( const Node * x ) const;
242
244 [[nodiscard]] bool threaded( void ) const;
245
246 Node f_head;
247 std::size_t f_size = 0;
248
249 }; // end( class ThreadedTree )
250
251/*--------------------------------------------------------------------------*/
252
253} // end( namespace ADS )
254
255/*--------------------------------------------------------------------------*/
256/*-------------------------- IMPLEMENTATION --------------------------------*/
257/*--------------------------------------------------------------------------*/
258
259#include "ADS/tree/impl/ThreadedTree.tpp"
260
261/*--------------------------------------------------------------------------*/
262
263#endif /* ThreadedTree.h included */
264
265/*--------------------------------------------------------------------------*/
266/*------------------------ End File ThreadedTree.h -------------------------*/
267/*--------------------------------------------------------------------------*/
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...
the constant iterator in symmetric order, by the threads
Definition ThreadedTree.h:147
std::bidirectional_iterator_tag iterator_category
the category of the iterator, bidirectional
Definition ThreadedTree.h:150
bool operator==(const const_iterator &o) const
equal on the same node
Definition ThreadedTree.h:190
const K & reference
a constant reference
Definition ThreadedTree.h:153
const_iterator & operator--(void)
to the predecessor, by predecessor()
Definition ThreadedTree.h:173
K value_type
the type of the keys
Definition ThreadedTree.h:151
const K * pointer
a constant pointer
Definition ThreadedTree.h:154
const_iterator(void)=default
an iterator on no tree
const_iterator operator++(int)
to the successor, returning the iterator before the step
Definition ThreadedTree.h:178
const_iterator & operator++(void)
to the successor, by successor()
Definition ThreadedTree.h:168
pointer operator->(void) const
the address of the key
Definition ThreadedTree.h:166
const_iterator operator--(int)
to the predecessor, returning the iterator before the step
Definition ThreadedTree.h:184
std::ptrdiff_t difference_type
the type of the distances
Definition ThreadedTree.h:152
reference operator*(void) const
the key
Definition ThreadedTree.h:164
const_iterator(const ThreadedTree *t, const Node *x)
on the node of , the end if is the head
Definition ThreadedTree.h:160
bool contains(const K &k) const
whether is there, by the search of a binary search tree that stops at a thread
const_iterator cbegin(void) const
as begin()
Definition ThreadedTree.h:217
const_iterator begin(void) const
the iterator on the least key, the successor of the head
Definition ThreadedTree.h:207
std::size_t size(void) const
the number of keys
void forEach(F &&f) const
f( k ) on the keys by increasing order, by the threads: no stack, no recursion
const_iterator cend(void) const
as end()
Definition ThreadedTree.h:220
const_iterator end(void) const
the iterator past the largest key, the head
Definition ThreadedTree.h:212
std::vector< K > reversed(void) const
the keys by decreasing order, by the left threads, as forEach() with the predecessors
ThreadedTree(const ThreadedTree &)=delete
no copy
std::ptrdiff_t difference_type
the type of the distances
Definition ThreadedTree.h:201
K value_type
the type of the keys
Definition ThreadedTree.h:199
const_iterator iterator
a constant iterator
Definition ThreadedTree.h:204
ThreadedTree & operator=(const ThreadedTree &)=delete
no copy
ThreadedTree(void)
the empty tree, the head node alone, its left link a thread to itself
const K & reference
a constant reference
Definition ThreadedTree.h:202
std::size_t size_type
the type of the sizes
Definition ThreadedTree.h:200
void insert(const K &k)
inserts , if it is not there
~ThreadedTree()
frees the nodes
const K & const_reference
a constant reference
Definition ThreadedTree.h:203
Definition ADT.h:96