I'm learning Ada as a spare-time activity and, while I realise that it is greatly inspired by Pascal, I can't find a type that is similar to Pascal's set
.
set
is built into the Pascal language itself and uses one bit per element. So, for example, var a: set of char;
declares a (typically) 256-bit data type that can hold any set of characters.
In Ada, I can see that it has sets as part of its library (not a built-in language feature) but those are more like Java's HashSet
or C++'s unordered_set
and take up much more memory than 1 bit per element. Am I missing something?
You can use a Boolean array to get an equivalent type:
type Element is (A, B, C, D);
-- The possible elements of the sets.
type Set is array (Element) of Boolean;
-- A set of Elements. An Element E is in the
-- set S if and only if S(E) is True.
B_and_D : Set := (B | D => True, others => False);
-- A set initialized to contain B and D.
If you want to store the elements in single bits, add "with Pack" to the declaration of type Set. For example, if you want a set of Characters, using 256 bits, that would be
type Char_Set is array (Character) of Boolean
with Pack;