ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SortedArrayDictionary< K, V > Class Template Reference

a DICTIONARY realized with a vector sorted by key More...

#include <ADS/dictionary/SortedArrayDictionary.h>

Public types

using Key = K
 the type of the keys
using Value = V
 the type of the values
using value_type = std::pair< K , V >
 the type of the items, for the standard library
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances between two iterators
using reference = const std::pair< K , V > &
 a reference to an item
using const_reference = const std::pair< K , V > &
 a constant reference to an item
using const_iterator = const std::pair< K , V > *
 the iterator on the items, constant
using iterator = const_iterator
 the iterator on the items

Public member functions

std::optional< V > lookup (const K &k) const
 the value of the key \( k \), or nothing, \( O( \log n ) \)
void insert (const K &k, const V &v)
 associates the value \( v \) to the key \( k \), \( O( n ) \)
void remove (const K &k)
 removes the key \( k \), if it is there, moving the pairs after it one place back, \( O( n ) \)
bool contains (const K &k) const
 true if the key \( k \) is there, by the binary search of lookup()
std::optional< std::pair< K, V > > lowerBound (const K &k) const
 the smallest key not smaller than \( k \) and its value, or nothing (the find( x ) of an SSet of Morin), in \( O( \log n ) \) time
const K * findKey (const K &k) const
 the address of the key \( k \), nil if absent, in \( O( \log n ) \) time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed
const K * lowerBoundKey (const K &k) const
 the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the next change
bool empty (void) const
 true if there are no keys
std::size_t size (void) const
 the number of keys
const std::vector< std::pair< K, V > > & pairs (void) const
 the pairs, sorted by key
const_iterator begin (void) const
 the iterator on the first item
const_iterator end (void) const
 the iterator past the last item
const_iterator cbegin (void) const
 the constant iterator on the first item
const_iterator cend (void) const
 the constant iterator past the last item

Detailed description

template<std::totally_ordered K, class V>
class ADS::SortedArrayDictionary< K, V >

a DICTIONARY realized with a vector sorted by key

Member typedef documentation

◆ const_iterator

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::const_iterator = const std::pair< K , V > *

the iterator on the items, constant

◆ const_reference

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::const_reference = const std::pair< K , V > &

a constant reference to an item

◆ difference_type

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::difference_type = std::ptrdiff_t

the type of the distances between two iterators

◆ iterator

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::iterator = const_iterator

the iterator on the items

◆ Key

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::Key = K

the type of the keys

◆ reference

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::reference = const std::pair< K , V > &

a reference to an item

◆ size_type

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::size_type = std::size_t

the type of the sizes

◆ Value

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::Value = V

the type of the values

◆ value_type

template<std::totally_ordered K, class V>
using ADS::SortedArrayDictionary< K, V >::value_type = std::pair< K , V >

the type of the items, for the standard library

Member function documentation

◆ begin()

template<std::totally_ordered K, class V>
const_iterator ADS::SortedArrayDictionary< K, V >::begin ( void ) const
inlinenodiscard

the iterator on the first item

◆ cbegin()

template<std::totally_ordered K, class V>
const_iterator ADS::SortedArrayDictionary< K, V >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<std::totally_ordered K, class V>
const_iterator ADS::SortedArrayDictionary< K, V >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ contains()

template<std::totally_ordered K, class V>
bool ADS::SortedArrayDictionary< K, V >::contains ( const K & k) const
nodiscard

true if the key \( k \) is there, by the binary search of lookup()

◆ empty()

template<std::totally_ordered K, class V>
bool ADS::SortedArrayDictionary< K, V >::empty ( void ) const
nodiscard

true if there are no keys

◆ end()

template<std::totally_ordered K, class V>
const_iterator ADS::SortedArrayDictionary< K, V >::end ( void ) const
inlinenodiscard

the iterator past the last item

◆ findKey()

template<std::totally_ordered K, class V>
const K * ADS::SortedArrayDictionary< K, V >::findKey ( const K & k) const
nodiscard

the address of the key \( k \), nil if absent, in \( O( \log n ) \) time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed

◆ insert()

template<std::totally_ordered K, class V>
void ADS::SortedArrayDictionary< K, V >::insert ( const K & k,
const V & v )

associates the value \( v \) to the key \( k \), \( O( n ) \)

In pseudocode (section 3.2.3), \( \mathit{lo} \) found as in lookup():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad \mathit{lo} \gets \text{the first place whose key is not smaller than } k \text{, by a binary search} \cr 3 & \quad \textbf{if}\ \mathit{lo} = n\ \textbf{or}\ A[ \mathit{lo} ].\mathit{key} \ne k\ \textbf{then}\ \text{shift } A[ \mathit{lo} .. n - 1 ] \text{ one place right};\ n \gets n + 1 \cr 4 & \quad A[ \mathit{lo} ] \gets ( k , v )\quad \triangleright \ \text{a new pair, or a new value} \end{array}} \]

Postcondition
\( k \) has the value \( v \)

◆ lookup()

template<std::totally_ordered K, class V>
std::optional< V > ADS::SortedArrayDictionary< K, V >::lookup ( const K & k) const
nodiscard

the value of the key \( k \), or nothing, \( O( \log n ) \)

In pseudocode (the binary search of section 1.2), \( A[ 0 .. n - 1 ] \) the pairs by increasing key, \( \mathit{lo} \) the first place whose key is not smaller than \( k \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lookup}( k ) \cr 2 & \quad ( \mathit{lo} , \mathit{hi} ) \gets ( 0 , n )\quad \triangleright\ \text{the keys before } \mathit{lo} \text{ are} < k \text{, those from } \mathit{hi} \text{ are} \ge k \cr 3 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 4 & \qquad m \gets \lfloor ( \mathit{lo} + \mathit{hi} ) / 2 \rfloor \cr 5 & \qquad \textbf{if}\ k \le A[ m ].\mathit{key}\ \textbf{then} \ \mathit{hi} \gets m\ \textbf{else}\ \mathit{lo} \gets m + 1 \cr 6 & \quad \textbf{return}\ A[ \mathit{lo} ].\mathit{value} \text{ if } \mathit{lo} < n \text{ and } A[ \mathit{lo} ].\mathit{key} = k \text{, else } \textbf{nil} \end{array}} \]

◆ lowerBound()

template<std::totally_ordered K, class V>
std::optional< std::pair< K, V > > ADS::SortedArrayDictionary< K, V >::lowerBound ( const K & k) const
nodiscard

the smallest key not smaller than \( k \) and its value, or nothing (the find( x ) of an SSet of Morin), in \( O( \log n ) \) time

◆ lowerBoundKey()

template<std::totally_ordered K, class V>
const K * ADS::SortedArrayDictionary< K, V >::lowerBoundKey ( const K & k) const
nodiscard

the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the next change

◆ pairs()

template<std::totally_ordered K, class V>
const std::vector< std::pair< K, V > > & ADS::SortedArrayDictionary< K, V >::pairs ( void ) const
nodiscard

the pairs, sorted by key

◆ remove()

template<std::totally_ordered K, class V>
void ADS::SortedArrayDictionary< K, V >::remove ( const K & k)

removes the key \( k \), if it is there, moving the pairs after it one place back, \( O( n ) \)

Postcondition
\( k \) is not there

◆ size()

template<std::totally_ordered K, class V>
std::size_t ADS::SortedArrayDictionary< K, V >::size ( void ) const
nodiscard

the number of keys


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