|
| 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
|
|
| 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
|
template<std::totally_ordered K, class V>
class ADS::SortedArrayDictionary< K, V >
a DICTIONARY realized with a vector sorted by key
template<std::totally_ordered K, class 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 \)
template<std::totally_ordered K, class V>
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}}
\]