|
ADS
Algorithms and Data Structures
|
The abstract data types of the book, as concepts of C++20: SEQUENCE, SET and DICTIONARY (chapter 3), STACK and QUEUE (chapter 4). More...
#include <concepts>#include <cstddef>#include <optional>Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Concepts | |
| concept | ADS::Sequence |
| SEQUENCE (section 3.2.1): a dynamic linear sequence of items; as axioms, \( \mathrm{empty}( s ) = \mathrm{finished}( \mathrm{head}( s ) ) \) (empty() is redundant), \( \mathrm{read}( \mathrm{insert}( p , v ) ) = v \), \( \mathrm{remove}( \mathrm{insert}( p , v ) ) = s \), and \( \mathrm{read}( \mathrm{write}( p , v ) ) = v \) with the other items unchanged. | |
| concept | ADS::Set |
| SET (section 3.2.2): a finite collection of distinct items; union(), intersection() and difference() are functions of two sets. | |
| concept | ADS::Dictionary |
| DICTIONARY (section 3.2.3): a univocal relation from keys to values; lookup() returns the value of a key, or nothing (the nil of the book, std::nullopt). | |
| concept | ADS::Stack |
| STACK (section 4.3.1): items are added and removed at the top (LIFO). | |
| concept | ADS::Queue |
| [Stack] | |
The abstract data types of the book, as concepts of C++20: SEQUENCE, SET and DICTIONARY (chapter 3), STACK and QUEUE (chapter 4).
A specification of an abstract data type is a triple \( ( D , F , A ) \): \( D \) is a set of domains, one of which is the designated domain (the type being defined, e.g., the stacks) and the others are the auxiliary ones (e.g., the items and the booleans); \( F \) is a set of operations on \( D \); \( A \) is a set of axioms, i.e., the semantics of \( F \). The pair \( ( D , F ) \) is the syntactic specification, which a concept states as the names of the operations and their domains. The semantic one may be given in two ways: (i) algebraic, i.e., by equations among the operations, those that observe or remove defined on those that construct (e.g., \( \mathrm{top}( \mathrm{push}( S , v ) ) = v \) below); (ii) constructive, i.e., by the preconditions and the postconditions of each operation, expressed in terms of a type already known (e.g., the sequences of items). The library follows (ii) in the code, through the ADS_PRE and ADS_POST of Contract.h, and states (i) here; the tests check both on every realization. The operations are of four kinds: constructors, which make a value of the type (create(), insert(), push()); modifiers, which change it; observers, which return a property of it (empty(), top(), read()); iterators, which visit its items (the positions of a SEQUENCE, the forEach() of the sets).
A realization meets the requirement of abstraction, i.e., a program that uses the type does not depend on how it is realized, and the one of protection, i.e., only the operations of the type act on it: hence each realization is a class whose data are private, and a realization can replace another one under the same concept.
The names are those of the book, so that the pseudocode reads the same in the code: a SEQUENCE has empty(), finished(), head(), tail(), next(), prev(), insert(), remove(), read() and write(), a position of the type Pos. As in the book,