On this page
- Positive_Count
- List
- Cursor
- No_Element
- Length
- Is_Empty
- Models
- Model_Length
- Model_Get
- Model
- Node_At
- Node_Index
- Index_Of
- Has_Element
- Element
- Same_Places
- Places_Shifted_Up
- Places_Shifted_Down
- Inserted
- Removed
- First
- Last
- Next
- Previous
- First_Element
- Last_Element
- Find
- Contains
- Clear
- Insert
- Insert
- Append
- Prepend
- Replace_Element
- Delete
- Delete_First
- Delete_Last
Generic package Ada.Containers.Bounded_Doubly_Linked_Lists
Verada predefined library: Ada.Containers.Bounded_Doubly_Linked_Lists (RM A.18.20), doubly linked lists of at most Capacity elements, in the design of Verada.Containers.Formal.Doubly_Linked_Lists: no storage allocated after the list is made, cursors that stay valid while other elements come and go, and contracts over the list's model (its elements in order, a functional sequence) and the place of every cursor in it. A cursor is a node's number; the operations that read through one take the list too (Ada's take the cursor alone, which names its list by access, a second name the proof does not follow). Where Ada raises Constraint_Error, Program_Error or Capacity_Error a precondition requires what avoids it. Contracts compare elements with Models.Equal_Elements, the element type's own equality; the generic "=" is the one Find, Contains and the lists' "=" use, as in Ada.
generic
type Element_Type is private;
with function "=" (Left, Right : Element_Type) return Boolean is <>;Positive_Count
subtype Positive_Count is Count_Type range 1 .. Count_Type'Last;a place in a list, from 1
List
type List (Capacity : Count_Type) is private
with Default_Initial_Condition => Length (List) = 0;Cursor
type Cursor is record
Node : Count_Type := 0;
end record;a node of a list; No_Element designates none
No_Element
No_Element : constant Cursor := (Node => 0);Length
function Length (Container : List) return Count_Type
with Global => null;Is_Empty
function Is_Empty (Container : List) return Boolean is (Length (Container) = 0);Models
package Models is new Verada.Containers.Functional.Sequences (Element_Type);the functional model: the elements in order, as a sequence
Model_Length
function Model_Length (S : Models.Sequence) return Count_Type is (Count_Type (Models.Length (S)))
with Ghost;(proof only) a model's length and its element at a place, counted as the list is
Model_Get
function Model_Get (S : Models.Sequence; I : Count_Type) return Element_Type is (Models.Get (S, Natural (I)))
with Ghost,
Pre => I in 1 .. Model_Length (S);Model
function Model (Container : List) return Models.Sequence
with Ghost,
Global => null,
Post => Model_Length (Model'Result) = Length (Container);Node_At
function Node_At (Container : List; Index : Positive_Count) return Count_Type
with Ghost,
Global => null,
Pre => Index <= Length (Container);the node of the list's element at a place, from 1 (proof only)
Node_Index
function Node_Index (Container : List; Node : Count_Type) return Count_Type
with Global => null,
Post => Node_Index'Result <= Length (Container)
and then (if Node = 0 then Node_Index'Result = 0)
and then (if Node_Index'Result /= 0 then Node_At (Container, Node_Index'Result) = Node)
and then (for all I in 1 .. Length (Container) => (Node_At (Container, I) = Node) = (I = Node_Index'Result));the place of a node's element in the list, from 1, or 0 when the list does not use it
Index_Of
function Index_Of (Container : List; Position : Cursor) return Count_Type is
(Node_Index (Container, Position.Node));the place of a cursor's element in the list, from 1, or 0 when it designates none
Has_Element
function Has_Element (Container : List; Position : Cursor) return Boolean is
(Index_Of (Container, Position) /= 0);Element
function Element (Container : List; Position : Cursor) return Element_Type
with Global => null,
Pre => Has_Element (Container, Position),
Post => Models.Equal_Elements (Element'Result, Model_Get (Model (Container), Index_Of (Container, Position)));Same_Places
function Same_Places (Left, Right : List) return Boolean is
(for all N in 1 .. Left.Capacity => Node_Index (Left, N) = Node_Index (Right, N))
with Ghost;every cursor keeps its place
Places_Shifted_Up
function Places_Shifted_Up (Old, New_List : List; At_Index : Positive_Count) return Boolean is
(for all N in 1 .. Old.Capacity =>
(if Node_Index (Old, N) /= 0 then
Node_Index (New_List, N) - (if Node_Index (Old, N) < At_Index then 0 else 1) = Node_Index (Old, N)))
with Ghost;the cursors of Old keep their place in New, those from At on one further
Places_Shifted_Down
function Places_Shifted_Down (Old, New_List : List; At_Index : Positive_Count) return Boolean is
(for all N in 1 .. Old.Capacity =>
(if Node_Index (Old, N) /= 0 and then Node_Index (Old, N) /= At_Index then
Node_Index (New_List, N)
= (if Node_Index (Old, N) < At_Index then Node_Index (Old, N) else Node_Index (Old, N) - 1)))
with Ghost;the cursors of Old other than the one at At_Index keep their place in New, those after it one nearer
Inserted
function Inserted (Old, New_Model : Models.Sequence; At_Index : Positive_Count; Item : Element_Type) return Boolean is
(Model_Length (Old) < Count_Type'Last
and then At_Index <= Model_Length (Old) + 1
and then Model_Length (New_Model) = Model_Length (Old) + 1
and then Models.Equal_Elements (Model_Get (New_Model, At_Index), Item)
and then (for all I in 1 .. At_Index - 1 => Models.Equal_Elements (Model_Get (New_Model, I), Model_Get (Old, I)))
and then (for all I in At_Index .. Model_Length (Old) => Models.Equal_Elements (Model_Get (New_Model, I + 1), Model_Get (Old, I))))
with Ghost;New is Old with Item at At_Index
Removed
function Removed (Old, New_Model : Models.Sequence; At_Index : Positive_Count) return Boolean is
(At_Index <= Model_Length (Old)
and then Model_Length (New_Model) = Model_Length (Old) - 1
and then (for all I in 1 .. At_Index - 1 => Models.Equal_Elements (Model_Get (New_Model, I), Model_Get (Old, I)))
and then (for all I in At_Index .. Model_Length (Old) => (if I > At_Index then Models.Equal_Elements (Model_Get (New_Model, I - 1), Model_Get (Old, I)))))
with Ghost;New is Old without the element at At_Index
First
function First (Container : List) return Cursor
with Global => null,
Post => (if Is_Empty (Container) then First'Result = No_Element else Index_Of (Container, First'Result) = 1);Last
function Last (Container : List) return Cursor
with Global => null,
Post => (if Is_Empty (Container) then Last'Result = No_Element else Index_Of (Container, Last'Result) = Length (Container));Next
function Next (Container : List; Position : Cursor) return Cursor
with Global => null,
Pre => Has_Element (Container, Position),
Post => (if Index_Of (Container, Position) = Length (Container) then Next'Result = No_Element
else Index_Of (Container, Next'Result) = Index_Of (Container, Position) + 1);Previous
function Previous (Container : List; Position : Cursor) return Cursor
with Global => null,
Pre => Has_Element (Container, Position),
Post => (if Index_Of (Container, Position) = 1 then Previous'Result = No_Element
else Index_Of (Container, Previous'Result) = Index_Of (Container, Position) - 1);First_Element
function First_Element (Container : List) return Element_Type
with Global => null,
Pre => not Is_Empty (Container),
Post => Models.Equal_Elements (First_Element'Result, Model_Get (Model (Container), 1));Last_Element
function Last_Element (Container : List) return Element_Type
with Global => null,
Pre => not Is_Empty (Container),
Post => Models.Equal_Elements (Last_Element'Result, Model_Get (Model (Container), Length (Container)));Find
function Find (Container : List; Item : Element_Type) return Cursor
with Global => null,
Post => (if Find'Result = No_Element then
(for all I in 1 .. Length (Container) => not (Model_Get (Model (Container), I) = Item))
else Has_Element (Container, Find'Result) and then Element (Container, Find'Result) = Item);the first cursor of Item, or No_Element
Contains
function Contains (Container : List; Item : Element_Type) return Boolean is
(Find (Container, Item) /= No_Element);Clear
procedure Clear (Container : in out List)
with Global => null,
Post => Length (Container) = 0;Insert
procedure Insert (Container : in out List; Before : Cursor; New_Item : Element_Type; Position : out Cursor)
with Global => null,
Pre => Length (Container) < Container.Capacity and then (Before = No_Element or else Has_Element (Container, Before)),
Post => Inserted (Model (Container'Old), Model (Container),
(if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before)), New_Item)
and then Index_Of (Container, Position) = (if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before))
and then Places_Shifted_Up (Container'Old, Container, Index_Of (Container, Position));New_Item before Before (at the end when Before is No_Element); Position designates it
Insert
procedure Insert (Container : in out List; Before : Cursor; New_Item : Element_Type)
with Global => null,
Pre => Length (Container) < Container.Capacity and then (Before = No_Element or else Has_Element (Container, Before)),
Post => Inserted (Model (Container'Old), Model (Container),
(if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before)), New_Item)
and then Places_Shifted_Up (Container'Old, Container,
(if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before)));New_Item before Before (at the end when Before is No_Element)
Append
procedure Append (Container : in out List; New_Item : Element_Type)
with Global => null,
Pre => Length (Container) < Container.Capacity,
Post => Inserted (Model (Container'Old), Model (Container), Length (Container), New_Item)
and then Places_Shifted_Up (Container'Old, Container, Length (Container));Prepend
procedure Prepend (Container : in out List; New_Item : Element_Type)
with Global => null,
Pre => Length (Container) < Container.Capacity,
Post => Inserted (Model (Container'Old), Model (Container), 1, New_Item)
and then Places_Shifted_Up (Container'Old, Container, 1);Replace_Element
procedure Replace_Element (Container : in out List; Position : Cursor; New_Item : Element_Type)
with Global => null,
Pre => Has_Element (Container, Position),
Post => Length (Container) = Length (Container'Old)
and then Models.Equal_Elements (Element (Container, Position), New_Item)
and then Same_Places (Container'Old, Container)
and then (for all I in 1 .. Length (Container) =>
(if I /= Index_Of (Container, Position) then Models.Equal_Elements (Model_Get (Model (Container), I), Model_Get (Model (Container'Old), I))));Delete
procedure Delete (Container : in out List; Position : in out Cursor)
with Global => null,
Pre => Has_Element (Container, Position),
Post => Removed (Model (Container'Old), Model (Container), Index_Of (Container'Old, Position'Old))
and then Position = No_Element
and then not Has_Element (Container, Position'Old)
and then Places_Shifted_Down (Container'Old, Container, Index_Of (Container'Old, Position'Old));the element Position designates is gone, and so is Position
Delete_First
procedure Delete_First (Container : in out List)
with Global => null,
Pre => not Is_Empty (Container),
Post => Removed (Model (Container'Old), Model (Container), 1)
and then Places_Shifted_Down (Container'Old, Container, 1);Delete_Last
procedure Delete_Last (Container : in out List)
with Global => null,
Pre => not Is_Empty (Container),
Post => Removed (Model (Container'Old), Model (Container), Length (Container'Old))
and then Places_Shifted_Down (Container'Old, Container, Length (Container'Old));