ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SortedListSet.h File Reference

The SET as a sorted LIST of section 8.3: insert() in \( O( n ) \), min() and max() in \( O( 1 ) \), the operations of two sets by merging the two lists in \( O( n + m ) \), optimal since merging needs \( \Omega( n + m ) \) (exercises 8.3 and 8.4). More...

#include "ADS/Contract.h"
#include "ADS/sequence/List.h"
#include "ADS/set/SetOperations.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <functional>
#include <optional>
#include <stdexcept>
#include <utility>
#include <vector>
#include "ADS/set/impl/SortedListSet.tpp"

Go to the source code of this file.

Classes

class  ADS::SortedListSet< T >
 a set as a list sorted in increasing order More...

Namespaces

namespace  ADS

Functions

template<class T>
SortedListSet< T > ADS::merge (const SortedListSet< T > &A, const SortedListSet< T > &B, bool a, bool b, bool both)
 the merge of the sorted lists of \( A \) and \( B \): an item of \( A \) only, of \( B \) only or of both goes into the result if a, b or both say so; \( O( n + m ) \) time
template<class T>
SortedListSet< T > ADS::setUnion (const SortedListSet< T > &A, const SortedListSet< T > &B)
 the union, \( O( n + m ) \) time (exercise 8.3)
template<class T>
SortedListSet< T > ADS::setIntersection (const SortedListSet< T > &A, const SortedListSet< T > &B)
 the intersection, \( O( n + m ) \) time (the procedure of section 8.3)
template<class T>
SortedListSet< T > ADS::setDifference (const SortedListSet< T > &A, const SortedListSet< T > &B)
 the difference, \( O( n + m ) \) time (exercise 8.4)
template<class T>
SortedListSet< T > ADS::setSymmetricDifference (const SortedListSet< T > &A, const SortedListSet< T > &B)
 the symmetric difference, \( O( n + m ) \) time (exercise 8.5)

Detailed description

The SET as a sorted LIST of section 8.3: insert() in \( O( n ) \), min() and max() in \( O( 1 ) \), the operations of two sets by merging the two lists in \( O( n + m ) \), optimal since merging needs \( \Omega( n + m ) \) (exercises 8.3 and 8.4).

The merges append to the end of the result, the place \( \mathit{pos}_n = \mathrm{next}( \mathrm{tail}() ) \) of a LIST: the C.insert( C.tail() , x ) of the book, with the insert() of section 4.1 that puts \( x \) before the item in the given position, would put \( x \) before the last item.

The contracts [see Contract.h]: insertLast() needs an item larger than the others, after insert() the item is there and after remove() it is not, and each operation of two sets gives what its name says; the invariant is that the list increases strictly. The operations are defined in SortedListSet.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 8.3, https://algoritmicamente.it/materiali/libro/capitoli/cap08.pdf
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa