ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::PositionIndex< T, H > Class Template Reference

the positions of objects of type T, by their addresses More...

#include <ADS/dictionary/PositionIndex.h>

Public types

using Position = std::uint32_t
 the type of the positions

Public member functions

 PositionIndex (std::size_t n=0, H h=H())
 an empty index with room for \( n \) keys
Position find (const T *p) const
 the position of \( p \), NONE if absent
bool contains (const T *p) const
 true if \( p \) is in the index
bool insert (const T *p, Position i)
 gives \( p \) the position \( i \); true if \( p \) was absent, false if its position was replaced
bool erase (const T *p)
 removes \( p \); false if it was absent
template<std::ranges::random_access_range R>
void assign (const R &v)
 the index of the vector \( v \) of pointers (any range with \( v[ i ] \)): \( v[ i ] \) at the position \( i \), all the other keys gone
void renumber (std::span< const Position > E)
 the erasure of the positions \( E \) from the vector: the keys at a position in \( E \) go away, and each other position \( p \) becomes \( p - |\{ e \in E : e < p \}| \)
std::size_t size (void) const
 the number of keys
bool empty (void) const
 true if there are no keys
std::size_t capacity (void) const
 the number of places of the table
void reserve (std::size_t n)
 room for \( n \) keys with no rebuild
void clear (void)
 removes all the keys, keeping the places
template<class F>
void forEach (F &&f) const
 f( p , i ) for each key \( p \) at the position \( i \), in the order of the places
void swap (PositionIndex &o) noexcept
 exchanges the contents with o in \( O( 1 ) \)

Static public attributes

static constexpr Position NONE = std::numeric_limits< Position >::max()
 the position of a key absent

Detailed description

template<class T, class H = FastHash< const T * >>
class ADS::PositionIndex< T, H >

the positions of objects of type T, by their addresses

Member typedef documentation

◆ Position

template<class T, class H = FastHash< const T * >>
using ADS::PositionIndex< T, H >::Position = std::uint32_t

the type of the positions

Constructor and destructor documentation

◆ PositionIndex()

template<class T, class H = FastHash< const T * >>
ADS::PositionIndex< T, H >::PositionIndex ( std::size_t n = 0,
H h = H() )
inlineexplicit

an empty index with room for \( n \) keys

Member function documentation

◆ assign()

template<class T, class H = FastHash< const T * >>
template<std::ranges::random_access_range R>
void ADS::PositionIndex< T, H >::assign ( const R & v)

the index of the vector \( v \) of pointers (any range with \( v[ i ] \)): \( v[ i ] \) at the position \( i \), all the other keys gone

Precondition
no \( v[ i ] \) is nil, and \( v \) has fewer than NONE items

◆ capacity()

template<class T, class H = FastHash< const T * >>
std::size_t ADS::PositionIndex< T, H >::capacity ( void ) const
inlinenodiscard

the number of places of the table

◆ clear()

template<class T, class H = FastHash< const T * >>
void ADS::PositionIndex< T, H >::clear ( void )
inline

removes all the keys, keeping the places

◆ contains()

template<class T, class H = FastHash< const T * >>
bool ADS::PositionIndex< T, H >::contains ( const T * p) const
nodiscard

true if \( p \) is in the index

◆ empty()

template<class T, class H = FastHash< const T * >>
bool ADS::PositionIndex< T, H >::empty ( void ) const
inlinenodiscard

true if there are no keys

◆ erase()

template<class T, class H = FastHash< const T * >>
bool ADS::PositionIndex< T, H >::erase ( const T * p)

removes \( p \); false if it was absent

◆ find()

template<class T, class H = FastHash< const T * >>
Position ADS::PositionIndex< T, H >::find ( const T * p) const
nodiscard

the position of \( p \), NONE if absent

◆ forEach()

template<class T, class H = FastHash< const T * >>
template<class F>
void ADS::PositionIndex< T, H >::forEach ( F && f) const
inline

f( p , i ) for each key \( p \) at the position \( i \), in the order of the places

◆ insert()

template<class T, class H = FastHash< const T * >>
bool ADS::PositionIndex< T, H >::insert ( const T * p,
Position i )

gives \( p \) the position \( i \); true if \( p \) was absent, false if its position was replaced

Precondition
\( p \) is not nil and \( i < \) NONE

◆ renumber()

template<class T, class H = FastHash< const T * >>
void ADS::PositionIndex< T, H >::renumber ( std::span< const Position > E)

the erasure of the positions \( E \) from the vector: the keys at a position in \( E \) go away, and each other position \( p \) becomes \( p - |\{ e \in E : e < p \}| \)

In pseudocode (beyond the book, the rank by the counts of the words of Jacobson), \( B \) the bit vector of \( E \), \( w_j \) its words of 64 bits and \( r_j \) the number of 1s in \( w_0 , \ldots , w_{j-1} \), \( N = \max E + 1 \) and \( m = |E| \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{renumber}( E ) \cr 2 & \quad \text{build } B \text{ and the counts } r_j \cr 3 & \quad D \gets \emptyset \cr 4 & \quad \textbf{foreach}\ ( k , p ) \text{ in the order of the places} \ \textbf{do} \cr 5 & \qquad \textbf{if}\ p \ge N\ \textbf{then}\ p \gets p - m \cr 6 & \qquad \textbf{else} \cr 7 & \qquad\quad \textbf{if}\ B[ p ] = 1\ \textbf{then}\ D \gets D \cup \{ k \} \cr 8 & \qquad\quad p \gets p - r_{\lfloor p / 64 \rfloor} - \mathrm{popcount}( w_{\lfloor p / 64 \rfloor} \bmod 2^{p \bmod 64} ) \cr 9 & \quad \textbf{foreach}\ k \in D\ \textbf{do}\ \mathrm{remove}( k ) \end{array}} \]

Precondition
\( E \) is strictly increasing

◆ reserve()

template<class T, class H = FastHash< const T * >>
void ADS::PositionIndex< T, H >::reserve ( std::size_t n)
inline

room for \( n \) keys with no rebuild

◆ size()

template<class T, class H = FastHash< const T * >>
std::size_t ADS::PositionIndex< T, H >::size ( void ) const
inlinenodiscard

the number of keys

◆ swap()

template<class T, class H = FastHash< const T * >>
void ADS::PositionIndex< T, H >::swap ( PositionIndex< T, H > & o)
inlinenoexcept

exchanges the contents with o in \( O( 1 ) \)

Member data documentation

◆ NONE

template<class T, class H = FastHash< const T * >>
Position ADS::PositionIndex< T, H >::NONE = std::numeric_limits< Position >::max()
staticconstexpr

the position of a key absent


The documentation for this class was generated from the following file: