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

a set on a hash table whose foreach follows the order of insertion: a LIST keeps the items in that order and the table maps each item to its position, so insert(), remove() and contains() take \( O( 1 ) \) expected time and forEach() \( O( n ) \) (exercise 8.9) More...

#include <ADS/set/LinkedHashSet.h>

Public types

using Item = T
 the type of the items
using value_type = T
 the items
using size_type = std::size_t
 the sizes
using difference_type = std::ptrdiff_t
 the distances
using reference = const T &
 a constant reference
using const_reference = const T &
 a constant reference
using const_iterator = typename List< T >::const_iterator
 the iterator of the list of the order, constant
using iterator = const_iterator
 the same, constant

Public member functions

 LinkedHashSet (void)
 the empty set
 LinkedHashSet (const LinkedHashSet &o)
 a copy, with positions into its own list
LinkedHashSet & operator= (const LinkedHashSet &)=delete
 no assignment, which would copy the positions into the list of another set
std::size_t size (void) const
 the number of items
bool empty (void) const
 whether the set has no item
bool contains (const T &x) const
 whether \( x \) is in the set, by a search in the table
void insert (const T &x)
 \( x \) into the set: a new item goes to the end of the order, and an item already there keeps its place
void remove (const T &x)
 \( x \) out of the set, and out of the order
template<class F>
void forEach (F &&f) const
 f( x ) on the items in the order of insertion, \( O( n ) \) time
const_iterator begin (void) const
 the iterator on the first item inserted
const_iterator end (void) const
 the iterator past the last item inserted
const_iterator cbegin (void) const
 as begin()
const_iterator cend (void) const
 as end()

Detailed description

template<class T, class H = std::hash< T >>
class ADS::LinkedHashSet< T, H >

a set on a hash table whose foreach follows the order of insertion: a LIST keeps the items in that order and the table maps each item to its position, so insert(), remove() and contains() take \( O( 1 ) \) expected time and forEach() \( O( n ) \) (exercise 8.9)

Member typedef documentation

◆ const_iterator

template<class T, class H = std::hash< T >>
using ADS::LinkedHashSet< T, H >::const_iterator = typename List< T >::const_iterator

the iterator of the list of the order, constant

◆ const_reference

template<class T, class H = std::hash< T >>
using ADS::LinkedHashSet< T, H >::const_reference = const T &

a constant reference

◆ difference_type

template<class T, class H = std::hash< T >>
using ADS::LinkedHashSet< T, H >::difference_type = std::ptrdiff_t

the distances

◆ Item

template<class T, class H = std::hash< T >>
using ADS::LinkedHashSet< T, H >::Item = T

the type of the items

◆ iterator

template<class T, class H = std::hash< T >>
using ADS::LinkedHashSet< T, H >::iterator = const_iterator

the same, constant

◆ reference

template<class T, class H = std::hash< T >>
using ADS::LinkedHashSet< T, H >::reference = const T &

a constant reference

◆ size_type

template<class T, class H = std::hash< T >>
using ADS::LinkedHashSet< T, H >::size_type = std::size_t

the sizes

◆ value_type

template<class T, class H = std::hash< T >>
using ADS::LinkedHashSet< T, H >::value_type = T

the items

Constructor and destructor documentation

◆ LinkedHashSet() [1/2]

template<class T, class H = std::hash< T >>
ADS::LinkedHashSet< T, H >::LinkedHashSet ( void )

the empty set

◆ LinkedHashSet() [2/2]

template<class T, class H = std::hash< T >>
ADS::LinkedHashSet< T, H >::LinkedHashSet ( const LinkedHashSet< T, H > & o)

a copy, with positions into its own list

Member function documentation

◆ begin()

template<class T, class H = std::hash< T >>
const_iterator ADS::LinkedHashSet< T, H >::begin ( void ) const
inlinenodiscard

the iterator on the first item inserted

◆ cbegin()

template<class T, class H = std::hash< T >>
const_iterator ADS::LinkedHashSet< T, H >::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

template<class T, class H = std::hash< T >>
const_iterator ADS::LinkedHashSet< T, H >::cend ( void ) const
inlinenodiscard

as end()

◆ contains()

template<class T, class H = std::hash< T >>
bool ADS::LinkedHashSet< T, H >::contains ( const T & x) const
nodiscard

whether \( x \) is in the set, by a search in the table

◆ empty()

template<class T, class H = std::hash< T >>
bool ADS::LinkedHashSet< T, H >::empty ( void ) const
nodiscard

whether the set has no item

◆ end()

template<class T, class H = std::hash< T >>
const_iterator ADS::LinkedHashSet< T, H >::end ( void ) const
inlinenodiscard

the iterator past the last item inserted

◆ forEach()

template<class T, class H = std::hash< T >>
template<class F>
void ADS::LinkedHashSet< T, H >::forEach ( F && f) const

f( x ) on the items in the order of insertion, \( O( n ) \) time

◆ insert()

template<class T, class H = std::hash< T >>
void ADS::LinkedHashSet< T, H >::insert ( const T & x)

\( x \) into the set: a new item goes to the end of the order, and an item already there keeps its place

In pseudocode (exercise 8.9), \( L \) the list of the order and \( D \) the table of the positions:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( x )\quad \triangleright\ D \text{ maps an item to its cell in } L \cr 2 & \quad \textbf{if}\ D.\mathrm{lookup}( x ) \ne \textbf{nil} \ \textbf{then return} \cr 3 & \quad p \gets L.\mathrm{insert}( L.\mathrm{next}( L.\mathrm{tail}() ) , x )\quad \triangleright\ \text{last, in the order of insertion} \cr 4 & \quad D.\mathrm{insert}( x , p ) \end{array}} \]

Postcondition
\( x \) is in the set

◆ operator=()

template<class T, class H = std::hash< T >>
LinkedHashSet & ADS::LinkedHashSet< T, H >::operator= ( const LinkedHashSet< T, H > & )
delete

no assignment, which would copy the positions into the list of another set

◆ remove()

template<class T, class H = std::hash< T >>
void ADS::LinkedHashSet< T, H >::remove ( const T & x)

\( x \) out of the set, and out of the order

In pseudocode (exercise 8.9):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( x ) \cr 2 & \quad p \gets D.\mathrm{lookup}( x );\ \textbf{if}\ p = \textbf{nil}\ \textbf{then return} \cr 3 & \quad D.\mathrm{remove}( x );\ L.\mathrm{remove}( p )\quad \triangleright\ O( 1 ) \text{, the cell is known} \end{array}} \]

Postcondition
\( x \) is not in the set

◆ size()

template<class T, class H = std::hash< T >>
std::size_t ADS::LinkedHashSet< T, H >::size ( void ) const
nodiscard

the number of items


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