|
ADS
Algorithms and Data Structures
|
The bag of exercise 1.5 of Morin's book, beyond the book: a USet [see SetInterfaces.h] that keeps the items equal to one another, all of them: insert( x ) always adds \( x \), find( x ) gives one of the items equal to \( x \), findAll( x ) all of them, count( x ) how many, and remove( x ) removes the last one added, each in \( O( 1 ) \) expected time (amortized for insert() and remove(), which may rebuild the table) but findAll(), linear in what it gives. More...
#include "ADS/Contract.h"#include "ADS/dictionary/FlatHashTable.h"#include <cstddef>#include <functional>#include <utility>#include <vector>#include "ADS/set/impl/Bag.tpp"Go to the source code of this file.
Classes | |
| class | ADS::Bag< T, H, Eq > |
| a USet that keeps the items equal to one another, on a hash table of groups More... | |
Namespaces | |
| namespace | ADS |
The bag of exercise 1.5 of Morin's book, beyond the book: a USet [see SetInterfaces.h] that keeps the items equal to one another, all of them: insert( x ) always adds \( x \), find( x ) gives one of the items equal to \( x \), findAll( x ) all of them, count( x ) how many, and remove( x ) removes the last one added, each in \( O( 1 ) \) expected time (amortized for insert() and remove(), which may rebuild the table) but findAll(), linear in what it gives.
The bag is a USet of groups, as the exercise asks: a hash table [see FlatHashTable.h] from an item to the vector of the items equal to it, in the order of their insertions; equal items need not be identical (records equal by their key, e.g.), which is what sets it apart from a multiset of counters [see HashMultiset.h].
The contracts [see Contract.h]: after insert( x ) there is one more item equal to \( x \), after remove( x ) one less if there was one; the invariant is that no group is empty and that the groups count size() items. The operations are defined in Bag.tpp.