ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PositionIndex.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File PositionIndex.h --------------------------*/
3/*--------------------------------------------------------------------------*/
49/*--------------------------------------------------------------------------*/
50/*----------------------------- DEFINITIONS --------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53#ifndef __ADS_PositionIndex
54 #define __ADS_PositionIndex
55/* self-identification: #endif constant */
56
57/*--------------------------------------------------------------------------*/
58/*------------------------------ INCLUDES ----------------------------------*/
59/*--------------------------------------------------------------------------*/
60
61#include "ADS/Contract.h"
64
65#include <algorithm>
66#include <bit>
67#include <cstddef>
68#include <cstdint>
69#include <cstring>
70#include <limits>
71#include <memory>
72#include <ranges>
73#include <span>
74#include <stdexcept>
75#include <utility>
76#include <vector>
77
78/*--------------------------------------------------------------------------*/
79/*--------------------------- NAMESPACE ------------------------------------*/
80/*--------------------------------------------------------------------------*/
81
82namespace ADS
83{
84
85/*--------------------------------------------------------------------------*/
86/*-------------------------- CLASS PositionIndex ---------------------------*/
87/*--------------------------------------------------------------------------*/
88
90template< class T , class H = FastHash< const T * > >
92{
93
94/*--------------------------------------------------------------------------*/
95 public:
96 using Position = std::uint32_t;
97
99 static constexpr Position NONE = std::numeric_limits< Position >::max();
100
102 explicit PositionIndex( std::size_t n = 0 , H h = H() )
103 : f_table( n , std::move( h ) ) { }
104
105/*--------------------------------------------------------------------------*/
106
108 [[nodiscard]] Position find( const T * p ) const;
109
111 [[nodiscard]] bool contains( const T * p ) const;
112
116 bool insert( const T * p , Position i );
117
119 bool erase( const T * p );
120
125 template< std::ranges::random_access_range R >
126 void assign( const R & v );
127
155 void renumber( std::span< const Position > E );
156
158 [[nodiscard]] std::size_t size( void ) const { return( f_table.size() ); }
159
161 [[nodiscard]] bool empty( void ) const { return( f_table.empty() ); }
162
164 [[nodiscard]] std::size_t capacity( void ) const {
165 return( f_table.capacity() );
166 }
167
169 void reserve( std::size_t n ) { f_table.reserve( n ); }
170
172 void clear( void ) { f_table.clear(); }
173
176 template< class F >
177 void forEach( F && f ) const {
178 f_table.forEach( f );
179 }
180
182 void swap( PositionIndex & o ) noexcept {
183 f_table.swap( o.f_table );
184 f_words.swap( o.f_words );
185 f_drop.swap( o.f_drop );
186 }
187
188/*--------------------------------------------------------------------------*/
189 private:
191 struct Word
192 {
193 std::uint64_t bits;
194 Position rank;
195 };
196
197 FlatHashTable< const T * , Position , H > f_table;
198 std::vector< Word > f_words;
199 std::vector< const T * > f_drop;
200
201 }; // end( class PositionIndex )
202
203/*--------------------------------------------------------------------------*/
204
205} // end( namespace ADS )
206
207/*--------------------------------------------------------------------------*/
208/*-------------------------- IMPLEMENTATION --------------------------------*/
209/*--------------------------------------------------------------------------*/
210
211#include "ADS/dictionary/impl/PositionIndex.tpp"
212
213/*--------------------------------------------------------------------------*/
214
215#endif /* PositionIndex.h included */
216
217/*--------------------------------------------------------------------------*/
218/*------------------------ End File PositionIndex.h ------------------------*/
219/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The engineered hash functions of ADS, beyond the book [see HashFunctions.h for those of section 7....
A hash table with open addressing and control bytes, beyond the book: the engineered DICTIONARY of AD...
Position find(const T *p) const
the position of , NONE if absent
void clear(void)
removes all the keys, keeping the places
Definition PositionIndex.h:172
bool erase(const T *p)
removes ; false if it was absent
void swap(PositionIndex &o) noexcept
exchanges the contents with o in
Definition PositionIndex.h:182
std::size_t size(void) const
the number of keys
Definition PositionIndex.h:158
bool insert(const T *p, Position i)
gives the position ; true if was absent, false if its position was replaced
void assign(const R &v)
the index of the vector of pointers (any range with ): at the position , all the other keys gone
static constexpr Position NONE
the position of a key absent
Definition PositionIndex.h:99
void renumber(std::span< const Position > E)
the erasure of the positions from the vector: the keys at a position in go away,...
bool empty(void) const
true if there are no keys
Definition PositionIndex.h:161
std::uint32_t Position
the type of the positions
Definition PositionIndex.h:96
PositionIndex(std::size_t n=0, H h=H())
an empty index with room for keys
Definition PositionIndex.h:102
std::size_t capacity(void) const
the number of places of the table
Definition PositionIndex.h:164
void reserve(std::size_t n)
room for keys with no rebuild
Definition PositionIndex.h:169
void forEach(F &&f) const
f( p , i ) for each key at the position , in the order of the places
Definition PositionIndex.h:177
bool contains(const T *p) const
true if is in the index
Definition ADT.h:96
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73