ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SortedArrayDictionary.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------- File SortedArrayDictionary.h ----------------------*/
3/*--------------------------------------------------------------------------*/
28/*--------------------------------------------------------------------------*/
29/*----------------------------- DEFINITIONS --------------------------------*/
30/*--------------------------------------------------------------------------*/
31
32#ifndef __ADS_SortedArrayDictionary
33 #define __ADS_SortedArrayDictionary
34/* self-identification: #endif constant */
35
36/*--------------------------------------------------------------------------*/
37/*------------------------------ INCLUDES ----------------------------------*/
38/*--------------------------------------------------------------------------*/
39
40#include "ADS/Contract.h"
41
42#include <concepts>
43#include <cstddef>
44#include <optional>
45#include <utility>
46#include <vector>
47
48/*--------------------------------------------------------------------------*/
49/*--------------------------- NAMESPACE ------------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52namespace ADS
53{
54
55/*--------------------------------------------------------------------------*/
56/*---------------------- CLASS SortedArrayDictionary -----------------------*/
57/*--------------------------------------------------------------------------*/
58
60template< std::totally_ordered K , class V >
62{
63
64/*--------------------------------------------------------------------------*/
65 public:
66 using Key = K;
67 using Value = V;
68
89 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
90
109 void insert( const K & k , const V & v );
110
114 void remove( const K & k );
115
117 [[nodiscard]] bool contains( const K & k ) const;
118
121 [[nodiscard]] std::optional< std::pair< K , V > >
122 lowerBound( const K & k ) const;
123
127 [[nodiscard]] const K * findKey( const K & k ) const;
128
132 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
133
135 [[nodiscard]] bool empty( void ) const;
136
138 [[nodiscard]] std::size_t size( void ) const;
139
141 [[nodiscard]] const std::vector< std::pair< K , V > > & pairs( void ) const;
142
143/*------------------------- THE STANDARD INTERFACE -------------------------*/
144
145 // a constant view of the pairs by increasing key, contiguous
146
148 using value_type = std::pair< K , V >;
150 using size_type = std::size_t;
152 using difference_type = std::ptrdiff_t;
154 using reference = const std::pair< K , V > &;
156 using const_reference = const std::pair< K , V > &;
158 using const_iterator = const std::pair< K , V > *;
161
163 [[nodiscard]] const_iterator begin( void ) const { return( f_A.data() ); }
165 [[nodiscard]] const_iterator end( void ) const {
166 return( f_A.data() + f_A.size() );
167 }
168
169 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
171 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
172
173/*--------------------------------------------------------------------------*/
174 private:
176 [[nodiscard]] std::size_t place( const K & k ) const;
177
179 [[nodiscard]] bool sorted( void ) const;
180
181 std::vector< std::pair< K , V > > f_A;
182
183 }; // end( class SortedArrayDictionary )
184
185/*--------------------------------------------------------------------------*/
186
187} // end( namespace ADS )
188
189/*--------------------------------------------------------------------------*/
190/*-------------------------- IMPLEMENTATION --------------------------------*/
191/*--------------------------------------------------------------------------*/
192
193#include "ADS/dictionary/impl/SortedArrayDictionary.tpp"
194
195/*--------------------------------------------------------------------------*/
196
197#endif /* SortedArrayDictionary.h included */
198
199/*--------------------------------------------------------------------------*/
200/*-------------------- End File SortedArrayDictionary.h --------------------*/
201/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
a DICTIONARY realized with a vector sorted by key
Definition SortedArrayDictionary.h:62
K Key
the type of the keys
Definition SortedArrayDictionary.h:66
const_iterator cbegin(void) const
the constant iterator on the first item
Definition SortedArrayDictionary.h:169
const std::pair< K, V > & reference
a reference to an item
Definition SortedArrayDictionary.h:154
const std::pair< K, V > * const_iterator
the iterator on the items, constant
Definition SortedArrayDictionary.h:158
const std::pair< K, V > & const_reference
a constant reference to an item
Definition SortedArrayDictionary.h:156
const K * findKey(const K &k) const
the address of the key , nil if absent, in time: the find( x ) of a USet of Morin [see DictionarySet...
const K * lowerBoundKey(const K &k) const
the address of the smallest key not smaller than , nil if none, in time: the find( x ) of an SSet of...
V Value
the type of the values
Definition SortedArrayDictionary.h:67
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition SortedArrayDictionary.h:152
void insert(const K &k, const V &v)
associates the value to the key ,
const_iterator end(void) const
the iterator past the last item
Definition SortedArrayDictionary.h:165
bool contains(const K &k) const
true if the key is there, by the binary search of lookup()
const_iterator iterator
the iterator on the items
Definition SortedArrayDictionary.h:160
const std::vector< std::pair< K, V > > & pairs(void) const
the pairs, sorted by key
bool empty(void) const
true if there are no keys
std::size_t size(void) const
the number of keys
std::pair< K, V > value_type
the type of the items, for the standard library
Definition SortedArrayDictionary.h:148
void remove(const K &k)
removes the key , if it is there, moving the pairs after it one place back,
const_iterator begin(void) const
the iterator on the first item
Definition SortedArrayDictionary.h:163
std::size_t size_type
the type of the sizes
Definition SortedArrayDictionary.h:150
const_iterator cend(void) const
the constant iterator past the last item
Definition SortedArrayDictionary.h:171
std::optional< std::pair< K, V > > lowerBound(const K &k) const
the smallest key not smaller than and its value, or nothing (the find( x ) of an SSet of Morin),...
std::optional< V > lookup(const K &k) const
the value of the key , or nothing,
Definition ADT.h:96