a multiset on a hash table, a counter of occurrences per item: the insertion of a new item sets it to 1, of an item already there adds 1, the removal subtracts 1 and removes the item at 0 (exercise 8.10)
More...
|
| | HashMultiset (void) |
| | the empty multiset
|
| std::size_t | size (void) const |
| | the number of items, each counted as many times as it occurs
|
| std::size_t | distinct (void) const |
| | the number of distinct items
|
| bool | empty (void) const |
| | whether the multiset has no item
|
| bool | contains (const T &x) const |
| | whether \( x \) occurs at least once
|
| std::size_t | count (const T &x) const |
| | the occurrences of \( x \), 0 if none
|
| void | insert (const T &x) |
| | one occurrence of \( x \) more
|
| void | remove (const T &x) |
| | one occurrence of \( x \) less, if there is one
|
| template<class F> |
| void | forEach (F &&f) const |
| | f( x , c ) on the distinct items and their occurrences
|
| const_iterator | begin (void) const |
| | the iterator on the first pair
|
| const_iterator | end (void) const |
| | the iterator past the last pair
|
| const_iterator | cbegin (void) const |
| | as begin()
|
| const_iterator | cend (void) const |
| | as end()
|
template<class T, class H = std::hash< T >>
class ADS::HashMultiset< T, H >
a multiset on a hash table, a counter of occurrences per item: the insertion of a new item sets it to 1, of an item already there adds 1, the removal subtracts 1 and removes the item at 0 (exercise 8.10)
template<class T, class H = std::hash< T >>
one occurrence of \( x \) more
In pseudocode (exercise 8.10), \( D \) the table of the counters:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{insert}( x )\quad \triangleright\ D
\text{ maps an item to its copies} \cr
2 & \quad D.\mathrm{insert}( x , 1 + \text{the copies of } x \text{, } 0
\text{ if } D.\mathrm{lookup}( x ) = \textbf{nil} )
\end{array}}
\]
- Postcondition
- one occurrence of \( x \) more
template<class T, class H = std::hash< T >>
one occurrence of \( x \) less, if there is one
In pseudocode (exercise 8.10):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{remove}( x )\quad \triangleright
\ \text{one copy} \cr
2 & \quad c \gets D.\mathrm{lookup}( x );\ \textbf{if}\ c =
\textbf{nil}\ \textbf{then return} \cr
3 & \quad \textbf{if}\ c > 1\ \textbf{then}\ D.\mathrm{insert}( x , c -
1 )\ \textbf{else}\ D.\mathrm{remove}( x )
\end{array}}
\]
- Postcondition
- one occurrence of \( x \) less, or none