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

a set as a list sorted in increasing order More...

#include <ADS/set/SortedListSet.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, constant
using iterator = const_iterator
 the same, constant

Public member functions

 SortedListSet (void)=default
 the empty set
 SortedListSet (std::initializer_list< T > items)
 the set of the given items, in any order, a repeated one taken once
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 scan that stops at the first item not smaller than \( x \), \( O( n ) \) time
void insert (const T &x)
 \( x \) into the set, \( O( n ) \) time, to keep the order
void insertLast (const T &x)
 \( x \), larger than all the items, at the end of the list, \( O( 1 ) \) time
void remove (const T &x)
 \( x \) out of the set, \( O( n ) \) time
std::optional< T > min (void) const
 the smallest item, at the head, none if the set is empty, \( O( 1 ) \) time
std::optional< T > max (void) const
 the largest item, at the tail, none if the set is empty, \( O( 1 ) \) time
template<class F>
void forEach (F &&f) const
 f( x ) on the items in increasing order, \( O( n ) \) time
const List< T > & list (void) const
 the sorted list of the items
const_iterator begin (void) const
 the iterator on the smallest item
const_iterator end (void) const
 the iterator past the largest item
const_iterator cbegin (void) const
 as begin()
const_iterator cend (void) const
 as end()

Detailed description

template<std::totally_ordered T>
class ADS::SortedListSet< T >

a set as a list sorted in increasing order

Member typedef documentation

◆ const_iterator

template<std::totally_ordered T>
using ADS::SortedListSet< T >::const_iterator = typename List< T >::const_iterator

the iterator of the list, constant

◆ const_reference

template<std::totally_ordered T>
using ADS::SortedListSet< T >::const_reference = const T &

a constant reference

◆ difference_type

template<std::totally_ordered T>
using ADS::SortedListSet< T >::difference_type = std::ptrdiff_t

the distances

◆ Item

template<std::totally_ordered T>
using ADS::SortedListSet< T >::Item = T

the type of the items

◆ iterator

template<std::totally_ordered T>
using ADS::SortedListSet< T >::iterator = const_iterator

the same, constant

◆ reference

template<std::totally_ordered T>
using ADS::SortedListSet< T >::reference = const T &

a constant reference

◆ size_type

template<std::totally_ordered T>
using ADS::SortedListSet< T >::size_type = std::size_t

the sizes

◆ value_type

template<std::totally_ordered T>
using ADS::SortedListSet< T >::value_type = T

the items

Constructor and destructor documentation

◆ SortedListSet() [1/2]

template<std::totally_ordered T>
ADS::SortedListSet< T >::SortedListSet ( void )
default

the empty set

◆ SortedListSet() [2/2]

template<std::totally_ordered T>
ADS::SortedListSet< T >::SortedListSet ( std::initializer_list< T > items)

the set of the given items, in any order, a repeated one taken once

Member function documentation

◆ begin()

template<std::totally_ordered T>
const_iterator ADS::SortedListSet< T >::begin ( void ) const
inlinenodiscard

the iterator on the smallest item

◆ cbegin()

template<std::totally_ordered T>
const_iterator ADS::SortedListSet< T >::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

template<std::totally_ordered T>
const_iterator ADS::SortedListSet< T >::cend ( void ) const
inlinenodiscard

as end()

◆ contains()

template<std::totally_ordered T>
bool ADS::SortedListSet< T >::contains ( const T & x) const
nodiscard

whether \( x \) is in the set, by a scan that stops at the first item not smaller than \( x \), \( O( n ) \) time

◆ empty()

template<std::totally_ordered T>
bool ADS::SortedListSet< T >::empty ( void ) const
nodiscard

whether the set has no item

◆ end()

template<std::totally_ordered T>
const_iterator ADS::SortedListSet< T >::end ( void ) const
inlinenodiscard

the iterator past the largest item

◆ forEach()

template<std::totally_ordered T>
template<class F>
void ADS::SortedListSet< T >::forEach ( F && f) const

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

◆ insert()

template<std::totally_ordered T>
void ADS::SortedListSet< T >::insert ( const T & x)

\( x \) into the set, \( O( n ) \) time, to keep the order

In pseudocode (section 8.3), \( L \) the list:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( x ) \cr 2 & \quad p \gets \text{the first position of } L \text{ that is finished or holds an item } \ge x \cr 3 & \quad \textbf{if}\ L.\mathrm{finished}( p )\ \textbf{or}\ x < L.\mathrm{read}( p )\ \textbf{then}\ L.\mathrm{insert}( p , x )\quad \triangleright\ \text{before } p \end{array}} \]

Postcondition
\( x \) is in the set

◆ insertLast()

template<std::totally_ordered T>
void ADS::SortedListSet< T >::insertLast ( const T & x)

\( x \), larger than all the items, at the end of the list, \( O( 1 ) \) time

Precondition
the set is empty or its largest item is smaller than \( x \)

◆ list()

template<std::totally_ordered T>
const List< T > & ADS::SortedListSet< T >::list ( void ) const
nodiscard

the sorted list of the items

◆ max()

template<std::totally_ordered T>
std::optional< T > ADS::SortedListSet< T >::max ( void ) const
nodiscard

the largest item, at the tail, none if the set is empty, \( O( 1 ) \) time

◆ min()

template<std::totally_ordered T>
std::optional< T > ADS::SortedListSet< T >::min ( void ) const
nodiscard

the smallest item, at the head, none if the set is empty, \( O( 1 ) \) time

◆ remove()

template<std::totally_ordered T>
void ADS::SortedListSet< T >::remove ( const T & x)

\( x \) out of the set, \( O( n ) \) time

In pseudocode (section 8.3), the scan of insert():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( x ) \cr 2 & \quad p \gets \text{the first position of } L \text{ that is finished or holds an item } \ge x \cr 3 & \quad \textbf{if}\ \textbf{not}\ L.\mathrm{finished}( p ) \ \textbf{and}\ L.\mathrm{read}( p ) = x\ \textbf{then} \ L.\mathrm{remove}( p ) \end{array}} \]

Postcondition
\( x \) is not in the set

◆ size()

template<std::totally_ordered T>
std::size_t ADS::SortedListSet< T >::size ( void ) const
nodiscard

the number of items


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