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

a USet that keeps the items equal to one another, on a hash table of groups More...

#include <ADS/set/Bag.h>

Public types

using Item = T
 the type of the items

Public member functions

 Bag (H h=H(), Eq eq=Eq())
 the empty bag
std::size_t size (void) const
 the number \( n \) of items, the equal ones counted each
bool empty (void) const
 true if there is no item
std::size_t distinct (void) const
 the number of groups of equal items
bool insert (const T &x)
 adds \( x \), \( O( 1 ) \) expected amortized; always true
bool remove (const T &x)
 removes the last item added equal to \( x \), \( O( 1 ) \) expected amortized; true if there was one
const T * find (const T &x) const
 an item equal to \( x \), the first one added, nil if none, \( O( 1 ) \) expected
std::vector< T > findAll (const T &x) const
 all the items equal to \( x \), in the order of their insertions
std::size_t count (const T &x) const
 the number of items equal to \( x \)

Detailed description

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
class ADS::Bag< T, H, Eq >

a USet that keeps the items equal to one another, on a hash table of groups

Member typedef documentation

◆ Item

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
using ADS::Bag< T, H, Eq >::Item = T

the type of the items

Constructor and destructor documentation

◆ Bag()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
ADS::Bag< T, H, Eq >::Bag ( H h = H(),
Eq eq = Eq() )
inlineexplicit

the empty bag

Member function documentation

◆ count()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
std::size_t ADS::Bag< T, H, Eq >::count ( const T & x) const
nodiscard

the number of items equal to \( x \)

◆ distinct()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
std::size_t ADS::Bag< T, H, Eq >::distinct ( void ) const
inlinenodiscard

the number of groups of equal items

◆ empty()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
bool ADS::Bag< T, H, Eq >::empty ( void ) const
inlinenodiscard

true if there is no item

◆ find()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
const T * ADS::Bag< T, H, Eq >::find ( const T & x) const
nodiscard

an item equal to \( x \), the first one added, nil if none, \( O( 1 ) \) expected

◆ findAll()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
std::vector< T > ADS::Bag< T, H, Eq >::findAll ( const T & x) const
nodiscard

all the items equal to \( x \), in the order of their insertions

◆ insert()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
bool ADS::Bag< T, H, Eq >::insert ( const T & x)

adds \( x \), \( O( 1 ) \) expected amortized; always true

In pseudocode (Morin, exercise 1.5), \( G \) the table of the groups:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( x ) \cr 2 & \quad \textbf{if}\ G.\mathrm{find}( x ) = \textbf{nil}\ \textbf{then} \ G.\mathrm{insert}( x , \langle x \rangle ) \cr 3 & \quad \textbf{else}\ \text{append } x \text{ to } G.\mathrm{find}( x ) \cr 4 & \quad n \gets n + 1;\ \textbf{return true} \end{array}} \]

Postcondition
one item equal to \( x \) more

◆ remove()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
bool ADS::Bag< T, H, Eq >::remove ( const T & x)

removes the last item added equal to \( x \), \( O( 1 ) \) expected amortized; true if there was one

In pseudocode (Morin, exercise 1.5), \( G \) the table of the groups:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{remove}( x ) \cr 2 & \quad g \gets G.\mathrm{find}( x );\ \textbf{if}\ g = \textbf{nil}\ \textbf{then return false} \cr 3 & \quad \textbf{if}\ | g | > 1\ \textbf{then}\ \text{remove the last item of } g\ \textbf{else}\ G.\mathrm{remove}( x ) \cr 4 & \quad n \gets n - 1;\ \textbf{return true} \end{array}} \]

Postcondition
one item equal to \( x \) less, if there was one

◆ size()

template<class T, class H = DefaultHash< T >, class Eq = std::equal_to< T >>
std::size_t ADS::Bag< T, H, Eq >::size ( void ) const
inlinenodiscard

the number \( n \) of items, the equal ones counted each


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