|
ADS
Algorithms and Data Structures
|
The operations of chapter 8 on any SET that can be copied and has the foreach, forEach( f ). More...
Files | |
| file | ADS/set/SetOperations.h |
| The operations of chapter 8 on any SET that can be copied and has the foreach, forEach( f ): the union copies \( A \) and inserts the items of \( B \), the intersection copies the smaller set and removes its items not in the other one, the difference copies \( A \) and removes its items in \( B \), the symmetric difference adds to \( A \setminus B \) the items of \( B \) not in \( A \). | |
Concepts | |
| concept | ADS::IterableSet |
| a SET that can be copied and has the foreach | |
Functions | |
| template<IterableSet S> | |
| S | ADS::setUnion (const S &A, const S &B) |
| the union: a copy of \( A \), plus the items of \( B \) | |
| template<IterableSet S> | |
| S | ADS::setIntersection (const S &A, const S &B) |
| the intersection: a copy of the smaller set \( X \), less its items not in the other one, \( Y \) | |
| template<IterableSet S> | |
| S | ADS::setDifference (const S &A, const S &B) |
| the difference: a copy of \( A \), less its items in \( B \) | |
| template<IterableSet S> | |
| S | ADS::setSymmetricDifference (const S &A, const S &B) |
| the symmetric difference: \( A \setminus B \), plus the items of \( B \) not in \( A \) | |
The operations of chapter 8 on any SET that can be copied and has the foreach, forEach( f ).
The definitions, the invariants and the costs of the area are in Theory of sets.
| S ADS::setDifference | ( | const S & | A, |
| const S & | B ) |
the difference: a copy of \( A \), less its items in \( B \)
In pseudocode (chapter 8):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setDifference}( A , B ) \cr 2 & \quad C \gets A;\ \textbf{foreach}\ x \in A\ \textbf{do} \ \textbf{if}\ B.\mathrm{contains}( x )\ \textbf{then} \ C.\mathrm{remove}( x ) \cr 3 & \quad \textbf{return}\ C \end{array}} \]
| S ADS::setIntersection | ( | const S & | A, |
| const S & | B ) |
the intersection: a copy of the smaller set \( X \), less its items not in the other one, \( Y \)
In pseudocode (chapter 8):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setIntersection}( A , B ) \cr 2 & \quad X \gets \text{the smaller of } A \text{ and } B \text{ (} A \text{ on a tie)};\ Y \gets \text{the other} \cr 3 & \quad C \gets X;\ \textbf{foreach}\ x \in X\ \textbf{do} \ \textbf{if}\ \textbf{not}\ Y.\mathrm{contains}( x )\ \textbf{then} \ C.\mathrm{remove}( x ) \cr 4 & \quad \textbf{return}\ C \end{array}} \]
| S ADS::setSymmetricDifference | ( | const S & | A, |
| const S & | B ) |
the symmetric difference: \( A \setminus B \), plus the items of \( B \) not in \( A \)
In pseudocode (chapter 8):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setSymmetricDifference}( A , B ) \cr 2 & \quad C \gets \mathrm{setDifference}( A , B ) \cr 3 & \quad \textbf{foreach}\ x \in B\ \textbf{do}\ \textbf{if} \ \textbf{not}\ A.\mathrm{contains}( x )\ \textbf{then} \ C.\mathrm{insert}( x ) \cr 4 & \quad \textbf{return}\ C \end{array}} \]
| S ADS::setUnion | ( | const S & | A, |
| const S & | B ) |
the union: a copy of \( A \), plus the items of \( B \)
In pseudocode (chapter 8):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{setUnion}( A , B ) \cr 2 & \quad C \gets A;\ \textbf{foreach}\ x \in B\ \textbf{do} \ C.\mathrm{insert}( x )\quad \triangleright \ \text{a copy is ignored} \cr 3 & \quad \textbf{return}\ C \end{array}} \]