ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DirectTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File DirectTable.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
26/*--------------------------------------------------------------------------*/
27/*----------------------------- DEFINITIONS --------------------------------*/
28/*--------------------------------------------------------------------------*/
29
30#ifndef __ADS_DirectTable
31 #define __ADS_DirectTable
32/* self-identification: #endif constant */
33
34/*--------------------------------------------------------------------------*/
35/*------------------------------ INCLUDES ----------------------------------*/
36/*--------------------------------------------------------------------------*/
37
38#include "ADS/Contract.h"
39#include "ADS/Iterators.h"
40
41#include <cstddef>
42#include <limits>
43#include <optional>
44#include <utility>
45#include <vector>
46
47/*--------------------------------------------------------------------------*/
48/*--------------------------- NAMESPACE ------------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51namespace ADS
52{
53
54/*--------------------------------------------------------------------------*/
55/*--------------------------- CLASS DirectTable ----------------------------*/
56/*--------------------------------------------------------------------------*/
57
61template< class V >
63{
64
65/*--------------------------------------------------------------------------*/
66 public:
67 using Key = long;
68 using Value = V;
69
73 DirectTable( long a , long b );
74
94 [[nodiscard]] std::optional< V > lookup( long k ) const;
95
99 void insert( long k , const V & v );
100
104 void remove( long k );
105
107 [[nodiscard]] std::size_t size( void ) const;
108
110 [[nodiscard]] std::size_t capacity( void ) const;
111
112/*------------------------- THE STANDARD INTERFACE -------------------------*/
113
114 // a constant view of the pairs ( key , value ) by increasing key,
115 // bidirectional, the key computed from the place
116
118 struct Places
119 {
121 static std::size_t size( const auto & o ) { return( o.f_A.size() ); }
123 static bool occupied( const auto & o , std::size_t i ) {
124 return( o.f_A[ i ].has_value() );
125 }
126
127 static auto read( const auto & o , std::size_t i ) {
128 return( PairRef< long , const V & >( o.f_a + long( i ) , *o.f_A[ i ] ) );
129 }
130 };
131
133 using value_type = std::pair< long , V >;
135 using key_type = long;
137 using mapped_type = V;
139 using size_type = std::size_t;
141 using difference_type = std::ptrdiff_t;
150
152 [[nodiscard]] const_iterator begin( void ) const {
153 return( const_iterator( this , 0 ) );
154 }
155
156 [[nodiscard]] const_iterator end( void ) const {
157 return( const_iterator( this , Places::size( *this ) ) );
158 }
159
160 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
162 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
163
164/*--------------------------------------------------------------------------*/
165 private:
167 [[nodiscard]] std::size_t H( long k ) const;
168
170 [[nodiscard]] bool inRange( long k ) const;
171
173 [[nodiscard]] bool counted( void ) const;
174
175 long f_a;
176 std::vector< std::optional< V > > f_A;
177 std::size_t f_size = 0;
178
179 }; // end( class DirectTable )
180
181/*--------------------------------------------------------------------------*/
182
183} // end( namespace ADS )
184
185/*--------------------------------------------------------------------------*/
186/*-------------------------- IMPLEMENTATION --------------------------------*/
187/*--------------------------------------------------------------------------*/
188
189#include "ADS/dictionary/impl/DirectTable.tpp"
190
191/*--------------------------------------------------------------------------*/
192
193#endif /* DirectTable.h included */
194
195/*--------------------------------------------------------------------------*/
196/*------------------------- End File DirectTable.h -------------------------*/
197/*--------------------------------------------------------------------------*/
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...
long Key
the type of the keys
Definition DirectTable.h:67
const_iterator iterator
the iterator on the items
Definition DirectTable.h:149
void insert(long k, const V &v)
associates to , writing it in
long key_type
the type of the keys
Definition DirectTable.h:135
std::optional< V > lookup(long k) const
the value of , the place , or nothing
const_iterator cbegin(void) const
the constant iterator on the first item
Definition DirectTable.h:160
std::pair< long, V > value_type
the type of the items, for the standard library
Definition DirectTable.h:133
const_iterator begin(void) const
the iterator on the first item
Definition DirectTable.h:152
std::size_t size_type
the type of the sizes
Definition DirectTable.h:139
void remove(long k)
removes , if it is there, emptying
SlotIterator< DirectTable, Places > const_iterator
the iterator on the items, constant
Definition DirectTable.h:147
PairRef< long, const V & > reference
a reference to an item
Definition DirectTable.h:143
std::size_t capacity(void) const
the size of the vector
DirectTable(long a, long b)
the empty table of the keys in
V mapped_type
the type of the values
Definition DirectTable.h:137
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition DirectTable.h:141
PairRef< long, const V & > const_reference
a constant reference to an item
Definition DirectTable.h:145
const_iterator cend(void) const
the constant iterator past the last item
Definition DirectTable.h:162
const_iterator end(void) const
the iterator past the last item
Definition DirectTable.h:156
std::size_t size(void) const
the number of keys
V Value
the type of the values
Definition DirectTable.h:68
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
std::conditional_t< detail::plainPair< A, B, VA, VB >, std::pair< A, B >, detail::PairProxy< A, B, VA, VB > > PairRef
the pair that an iterator reads by value, in general two references into the container,...
Definition Iterators.h:648
Definition ADT.h:96
the places of the table, for SlotIterator
Definition DirectTable.h:119
static std::size_t size(const auto &o)
the number of places
Definition DirectTable.h:121
static bool occupied(const auto &o, std::size_t i)
whether the place holds a key
Definition DirectTable.h:123
static auto read(const auto &o, std::size_t i)
the pair of the place , its key computed as
Definition DirectTable.h:127