DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Ada.Containers.Bounded_Hashed_Sets

Sets of at most Capacity elements, in insertion order, looked up by Equivalent_Elements (RM A.18.23).

Read the family guide for representation, selection and resource rules. The complete declaration page retains exact signatures/contracts; the source specification is the implementation baseline.

Availability and ownership

Ada's generic formals and a capacity discriminant, in the design of Verada.Containers.Formal: contracts at every place over a ghost functional model; preconditions where Ada raises Constraint_Error or Capacity_Error. Instantiate it as Ada's, as in package P is new Ada.Containers.Bounded_Hashed_Sets (Character, Hash, "=");, and declare objects with their capacity. Elements are reached by value or place; Hash and the modulus are taken and not needed.

A client proof uses these contracts without proving the body again (see library assurance). Global => null excludes package-global effects, not changes made through an in out argument.

Import and instantiate

with Ada.Containers.Bounded_Hashed_Sets;
import Ada.Containers.Bounded_Hashed_Sets;

A Vibe module imports this unit's names with from Ada.Containers.Bounded_Hashed_Sets import Name; see library use across syntaxes. In Varc, keep each name's declared spelling and write & on arguments the call changes.

Contract and lifecycle reading

A Pre condition is the caller's obligation; a Post is what a normal return guarantees. What a postcondition does not state, a client cannot assume. Ghost functions are for contracts and ghost code. The family guide describes copying, lifetime, order and cost.

API 001

Element_Type (declaration) — The generic formals, as Ada's: the instance's types and the operations it compares with.

Declared interface:

generic
   type Element_Type is private;
   with function Hash (Element : Element_Type) return Hash_Type;
   with function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
   with function "=" (Left, Right : Element_Type) return Boolean is <>;

API 002

Set (declaration) — The set type, with its capacity (and modulus) as discriminants; a new set is empty.

Declared interface:

type Set (Capacity : Count_Type; Modulus : Hash_Type) is private
  with Default_Initial_Condition => Length (Set) = 0,
       Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Element);

API 003

Default_Modulus (declaration) — The modulus for a capacity, which a set's declaration gives with it.

Declared interface:

function Default_Modulus (Capacity : Count_Type) return Hash_Type is (Hash_Type'Mod (Capacity));

API 004

Length (declaration) — The number of elements (or entries) the container holds now.

Declared interface:

function Length (Container : Set) return Count_Type
  with Global => null,
       Post => Length'Result <= Container.Capacity;

API 005

Is_Empty (declaration) — Whether the container holds nothing.

Declared interface:

function Is_Empty (Container : Set) return Boolean is (Length (Container) = 0);

API 006

Element_At (declaration) — The element at a place, from 1.

Declared interface:

function Element_At (Container : Set; Index : Count_Type) return Element_Type
  with Global => null,
       Pre => Index in 1 .. Length (Container);

API 007

Models (declaration) — The functional model: the elements in order, as a sequence.

Declared interface:

package Models is new Verada.Containers.Functional.Sequences (Element_Type);

API 008

Model (declaration) — Ghost: the functional model of the contents, in order, for contracts.

Declared interface:

function Model (Container : Set) return Models.Sequence
  with Ghost,
       Global => null,
       Post => Models.Length (Model'Result) = Natural (Length (Container))
               and then (for all I in 1 .. Length (Container) =>
                           Models.Equal_Elements (Models.Get (Model'Result, Natural (I)), Element_At (Container, I)));

API 009

Same_Elements (declaration) — The element at each place of Right is Left's at the same place, or, from Skip on when Skip is not 0, at the next place (proof only).

Declared interface:

function Same_Elements (Left, Right : Set; Skip : Count_Type) return Boolean is
  (for all I in 1 .. Length (Right) =>
     (if Skip = 0 or else I < Skip
      then I <= Length (Left) and then Models.Equal_Elements (Element_At (Right, I), Element_At (Left, I))
      else I < Length (Left) and then Models.Equal_Elements (Element_At (Right, I), Element_At (Left, I + 1))))
  with Ghost;

API 010

Index_Of (declaration) — The place of the element equivalent to Item, or 0.

Declared interface:

function Index_Of (Container : Set; Item : Element_Type) return Count_Type
  with Global => null,
       Post => Index_Of'Result <= Length (Container)
               and then (if Index_Of'Result = 0
                         then (for all I in 1 .. Length (Container) => not Equivalent_Elements (Element_At (Container, I), Item))
                         else Equivalent_Elements (Element_At (Container, Index_Of'Result), Item)
                              and then (for all I in 1 .. Index_Of'Result - 1 => not Equivalent_Elements (Element_At (Container, I), Item)));

API 011

Contains (declaration) — Whether the container holds an element (or key) matching the argument.

Declared interface:

function Contains (Container : Set; Item : Element_Type) return Boolean is (Index_Of (Container, Item) /= 0);

API 012

Clear (declaration) — Empties the container; its capacity is unchanged.

Declared interface:

procedure Clear (Container : in out Set)
  with Global => null,
       Post => Length (Container) = 0;

API 013

Insert (declaration) — New_Item joins the set, at the end.

Declared interface:

procedure Insert (Container : in out Set; New_Item : Element_Type)
  with Global => null,
       Pre => not Contains (Container, New_Item) and then Length (Container) < Container.Capacity,
       Post => Length (Container) = Length (Container'Old) + 1
               and then Models.Equal_Elements (Element_At (Container, Length (Container)), New_Item)
               and then Same_Elements (Container, Container'Old, 0);

API 014

Replace (declaration) — The element equivalent to New_Item is now New_Item.

Declared interface:

procedure Replace (Container : in out Set; New_Item : Element_Type)
  with Global => null,
       Pre => Contains (Container, New_Item),
       Post => Length (Container) = Length (Container'Old)
               and then Models.Equal_Elements (Element_At (Container, Index_Of (Container'Old, New_Item)), New_Item)
               and then (for all I in 1 .. Length (Container) =>
                           (if I /= Index_Of (Container'Old, New_Item) then
                              Models.Equal_Elements (Element_At (Container, I), Element_At (Container'Old, I))));

API 015

Include (declaration) — New_Item is in the set, in place of the element equivalent to it or new at the end.

Declared interface:

procedure Include (Container : in out Set; New_Item : Element_Type)
  with Global => null,
       Pre => Contains (Container, New_Item) or else Length (Container) < Container.Capacity,
       Post => (if Contains (Container'Old, New_Item) then
                  Length (Container) = Length (Container'Old)
                  and then Models.Equal_Elements (Element_At (Container, Index_Of (Container'Old, New_Item)), New_Item)
                  and then (for all I in 1 .. Length (Container) =>
                              (if I /= Index_Of (Container'Old, New_Item) then
                                 Models.Equal_Elements (Element_At (Container, I), Element_At (Container'Old, I))))
                else
                  Length (Container) = Length (Container'Old) + 1
                  and then Models.Equal_Elements (Element_At (Container, Length (Container)), New_Item)
                  and then Same_Elements (Container, Container'Old, 0));

API 016

Delete (declaration) — The element equivalent to Item is gone; those after it move one place nearer.

Declared interface:

procedure Delete (Container : in out Set; Item : Element_Type)
  with Global => null,
       Pre => Contains (Container, Item),
       Post => Length (Container) = Length (Container'Old) - 1
               and then Same_Elements (Container'Old, Container, Index_Of (Container'Old, Item));

API 017

Exclude (declaration) — The element equivalent to Item is gone, if there was one.

Declared interface:

procedure Exclude (Container : in out Set; Item : Element_Type)
  with Global => null,
       Post => (if Contains (Container'Old, Item)
                then Length (Container) = Length (Container'Old) - 1
                     and then Same_Elements (Container'Old, Container, Index_Of (Container'Old, Item))
                else Length (Container) = Length (Container'Old)
                     and then Same_Elements (Container'Old, Container, 0));

API 018

"=" (declaration) — The same length, and each element of Left equal ("=") to the one equivalent to it in Right.

Declared interface:

function "=" (Left, Right : Set) return Boolean
  with Global => null,
       Post => "="'Result = (Length (Left) = Length (Right)
                             and then (for all I in 1 .. Length (Left) =>
                                         Contains (Right, Element_At (Left, I))
                                         and then Element_At (Left, I) = Element_At (Right, Index_Of (Right, Element_At (Left, I)))));

API 019

Assign (declaration) — Target holds Source's elements.

Declared interface:

procedure Assign (Target : in out Set; Source : Set)
  with Global => null,
       Pre => Length (Source) <= Target.Capacity,
       Post => Length (Target) = Length (Source) and then Same_Elements (Source, Target, 0);

API 020

Copy (declaration) — A set of Source's elements with room for Capacity, or for Source's length when Capacity is 0.

Declared interface:

function Copy (Source : Set; Capacity : Count_Type := 0) return Set
  with Global => null,
       Pre => Capacity = 0 or else Capacity >= Length (Source),
       Post => Copy'Result.Capacity = (if Capacity = 0 then Length (Source) else Capacity)
               and then Length (Copy'Result) = Length (Source)
               and then Same_Elements (Source, Copy'Result, 0);

API 021

Iter_First (declaration) — for E of S and for all E of S =>...: the elements, in their order.

Declared interface:

function Iter_First (Container : Set) return Count_Type is (1);

API 022

Iter_Next (declaration) — Iteration support: the position after another.

Declared interface:

function Iter_Next (Container : Set; Position : Count_Type) return Count_Type is
  (if Position < Count_Type'Last then Position + 1 else Position);

API 023

Iter_Has_Element (declaration) — Iteration support: whether a position holds an element.

Declared interface:

function Iter_Has_Element (Container : Set; Position : Count_Type) return Boolean is
  (Position in 1 .. Length (Container));

API 024

Iter_Element (declaration) — Iteration support: the element at a position.

Declared interface:

function Iter_Element (Container : Set; Position : Count_Type) return Element_Type is
  (Element_At (Container, Position))
  with Pre => Iter_Has_Element (Container, Position);