On this page
Generic package Verada.Containers.Unbounded.Ordered_Sets
Verada.Containers.Unbounded.Ordered_Sets: sets of any size, kept in increasing order of "<" (which must be a strict order), on the heap and freed where the set dies (a reclaimed type), the unbounded twin of the bounded ordered sets. The set is limited: copies are made explicitly. Its model is the array of its elements in order, Model, from 1. Only memory limits the size: an operation that adds an element raises Storage_Error when memory cannot hold the result (Verada.Storage).
generic
type Element_Type is private;
with function "<" (Left, Right : Element_Type) return Boolean is <>;Element_Array
type Element_Array is array (Positive range <>) of Element_Type;Has
function Has (A : Element_Array; Item : Element_Type) return Boolean is
(for some I in A'Range => A (I) = Item)
with Ghost;Set
type Set is limited private
with Default_Initial_Condition => Length (Set) = 0,
Finalizable => (Finalize => Clear, Relaxed_Finalization => True),
Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Element);Length
function Length (Container : Set) return Natural
with Global => null,
Post => Length'Result < Verada.Storage.Length_Bound;(below Length_Bound, as memory holds no more: a for e in s loop's cursor always moves on, and the loop ends)
Is_Empty
function Is_Empty (Container : Set) return Boolean is (Length (Container) = 0);Model
function Model (Container : Set) return Element_Array
with Global => null,
Post => Model'Result'Length = Length (Container)
and then (Model'Result'Length = 0 or else Model'Result'First = 1);Element
function Element (Container : Set; Index : Positive) return Element_Type
with Global => null,
Pre => Index <= Length (Container),
Post => Element'Result = Model (Container) (Index);Contains
function Contains (Container : Set; Item : Element_Type) return Boolean is
(for some I in 1 .. Length (Container) => Element (Container, I) = Item);Only_Added
function Only_Added (Container : Set; A : Element_Array; Item : Element_Type) return Boolean is
(for all I in 1 .. Length (Container) => Element (Container, I) = Item or else Has (A, Element (Container, I)))
with Ghost;every element of Container is Item or an element of A
Only_Kept
function Only_Kept (Container : Set; A : Element_Array) return Boolean is
(for all I in 1 .. Length (Container) => Has (A, Element (Container, I)))
with Ghost;every element of Container is an element of A
Clear
procedure Clear (Container : in out Set)
with Global => null, Always_Terminates,
Post => Length (Container) = 0;Insert
procedure Insert (Container : in out Set; Item : Element_Type)
with Global => null, Always_Terminates,
Pre => not Contains (Container, Item),
Post => Length (Container) = Length (Container)'Old + 1
and Contains (Container, Item)
and (for all X of Model (Container)'Old => Contains (Container, X))
and Only_Added (Container, Model (Container)'Old, Item);Item joins the set, in its place in the order
Include
procedure Include (Container : in out Set; Item : Element_Type)
with Global => null, Always_Terminates,
Post => Contains (Container, Item)
and Length (Container) = Length (Container)'Old + (if Contains (Container, Item)'Old then 0 else 1);Item is in the set afterwards (Python's s.add(x)): it joins, unless it is there
Exclude
procedure Exclude (Container : in out Set; Item : Element_Type)
with Global => null, Always_Terminates,
Post => not Contains (Container, Item)
and Length (Container) = Length (Container)'Old - (if Contains (Container, Item)'Old then 1 else 0)
and (for all X of Model (Container)'Old => (if X /= Item then Contains (Container, X)))
and Only_Kept (Container, Model (Container)'Old);Item is not in the set afterwards (Python's s.discard(x)): it leaves, if it is there; the other elements stay, and no other joins
Remove
procedure Remove (Container : in out Set; Item : Element_Type)
with Global => null, Always_Terminates,
Pre => Contains (Container, Item),
Post => not Contains (Container, Item)
and Length (Container) = Length (Container)'Old - 1
and (for all X of Model (Container)'Old => (if X /= Item then Contains (Container, X)))
and Only_Kept (Container, Model (Container)'Old);Item, which is in the set, leaves it (Python's s.remove(x))
function "or" (Left, Right : Set) return Set
with Global => null,
Post => Length ("or"'Result) <= Length (Left) + Length (Right)
and then (for all I in 1 .. Length ("or"'Result) =>
Contains (Left, Element ("or"'Result, I)) or else Contains (Right, Element ("or"'Result, I)))
and then (for all I in 1 .. Length (Left) => Contains ("or"'Result, Element (Left, I)))
and then (for all I in 1 .. Length (Right) => Contains ("or"'Result, Element (Right, I)));the elements of either (Python's s | t)
function "and" (Left, Right : Set) return Set
with Global => null,
Post => Length ("and"'Result) <= Length (Left)
and then (for all I in 1 .. Length ("and"'Result) =>
Contains (Left, Element ("and"'Result, I)) and then Contains (Right, Element ("and"'Result, I)))
and then (for all I in 1 .. Length (Left) =>
(if Contains (Right, Element (Left, I)) then Contains ("and"'Result, Element (Left, I))));the elements of both (Python's s & t)
function "-" (Left, Right : Set) return Set
with Global => null,
Post => Length ("-"'Result) <= Length (Left)
and then (for all I in 1 .. Length ("-"'Result) =>
Contains (Left, Element ("-"'Result, I)) and then not Contains (Right, Element ("-"'Result, I)))
and then (for all I in 1 .. Length (Left) =>
(if not Contains (Right, Element (Left, I)) then Contains ("-"'Result, Element (Left, I))));the elements of Left that are not Right's (Python's s - t)
function "xor" (Left, Right : Set) return Set
with Global => null,
Post => Length ("xor"'Result) <= Length (Left) + Length (Right)
and then (for all I in 1 .. Length ("xor"'Result) =>
Contains (Left, Element ("xor"'Result, I)) /= Contains (Right, Element ("xor"'Result, I)))
and then (for all I in 1 .. Length (Left) =>
(if not Contains (Right, Element (Left, I)) then Contains ("xor"'Result, Element (Left, I))))
and then (for all I in 1 .. Length (Right) =>
(if not Contains (Left, Element (Right, I)) then Contains ("xor"'Result, Element (Right, I))));the elements of one but not the other (Python's s ^ t)
function "<=" (Left, Right : Set) return Boolean is
(for all I in 1 .. Length (Left) => Contains (Right, Element (Left, I)));every element of Left is Right's (Python's s <= t, s.issubset(t))
function "=" (Left, Right : Set) return Boolean is
(Left <= Right and then Right <= Left);the same elements (Python's s == t)
function "<" (Left, Right : Set) return Boolean is
(Left <= Right and then not (Right <= Left));a subset that is not the whole of Right (Python's s < t)
function ">=" (Left, Right : Set) return Boolean is (Right <= Left);Python's s >= t and s > t
function ">" (Left, Right : Set) return Boolean is (Right < Left);Iter_First
function Iter_First (Container : Set) return Natural is (1);for E of S and for all E of S => ...: the elements in increasing order
Iter_Next
function Iter_Next (Container : Set; Position : Natural) return Natural is
(if Position < Natural'Last then Position + 1 else Position);Iter_Has_Element
function Iter_Has_Element (Container : Set; Position : Natural) return Boolean is
(Position in 1 .. Length (Container));Iter_Element
function Iter_Element (Container : Set; Position : Natural) return Element_Type is
(Element (Container, Position))
with Pre => Iter_Has_Element (Container, Position);Copy
procedure Copy (Source : Set; Target : in out Set)
with Global => null, Always_Terminates,
Post => Model (Target) = Model (Source);