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

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]

Detailed description

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,

  • the positions \( \mathit{pos}_{-1} \) and \( \mathit{pos}_n \) come before the first and after the last element of a sequence \( s_0 , \ldots , s_{n-1} \), and finished( p ) is true exactly on them;
  • insert( p , v ) with \( p = \mathit{pos}_i \) puts \( v \) at the index \( i \), the old elements from the index \( i \) moving one place on, and returns the position of the new element; remove( p ) with \( p = \mathit{pos}_i \) removes the element at the index \( i \) and returns the position of the new element at the index \( i \);
  • a precondition that costs a scan of the sequence (e.g., that \( p \) is a position of a list, which the book does not check in the realizations with pointers) is an audit of Contract.h, checked only at its level 3.
See also
A. Montresor, Algoritmi e Strutture Dati, 2025, sections 3.2 and 4.3.1, https://algoritmicamente.it/materiali/libro/capitoli/cap03.pdf and https://algoritmicamente.it/materiali/libro/capitoli/cap04.pdf
B. Liskov, S. Zilles, Programming with abstract data types, ACM SIGPLAN Notices 9(4), 50-59, 1974, doi:10.1145/942572.807045
D. L. Parnas, On the criteria to be used in decomposing systems into modules, Communications of the ACM 15(12), 1053-1058, 1972, doi:10.1145/361598.361623
J. V. Guttag, J. J. Horning, The algebraic specification of abstract data types, Acta Informatica 10(1), 27-52, 1978, doi:10.1007/BF00260922
M. Cadoli, M. Lenzerini, P. Naggar, A. Schaerf, Fondamenti della progettazione dei programmi: principi, tecniche e loro applicazioni in C++, CittaStudi, 1997
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa