DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Generic package Verada.Containers.Formal.Doubly_Linked_Lists

Verada predefined library: bounded doubly linked lists of at most Capacity elements, with cursors that stay valid while other elements come and go, and no storage allocated after the list is made. Contracts say what each operation does to the list's model (its elements in order, a functional sequence) and to the position of every cursor in it.

generic
   type Element_Type is private;
   Capacity : Positive;

Count_Type

subtype Count_Type is Natural range 0 .. Capacity;

List

type List 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);

Is_Full

function Is_Full (Container : List) return Boolean is (Length (Container) = Capacity);

Models

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

the functional model: the elements in order, as a sequence

Model

function Model (Container : List) return Models.Sequence
  with Ghost,
       Global => null,
       Post => Models.Length (Model'Result) = Length (Container);

Node_At

function Node_At (Container : List; Index : Positive) 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 => Element'Result = Models.Get (Model (Container), Index_Of (Container, Position));

Same_Places

function Same_Places (Left, Right : List) return Boolean is
  (for all N in 1 .. 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) return Boolean is
  (for all N in 1 .. Capacity =>
     (if Node_Index (Old, N) /= 0 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 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) return Boolean is
  (for all N in 1 .. 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; Item : Element_Type) return Boolean is
  (Models.Length (Old) < Natural'Last
   and then At_Index <= Models.Length (Old) + 1
   and then Models.Length (New_Model) = Models.Length (Old) + 1
   and then Models.Get (New_Model, At_Index) = Item
   and then (for all I in 1 .. At_Index - 1 => Models.Get (New_Model, I) = Models.Get (Old, I))
   and then (for all I in At_Index .. Models.Length (Old) => Models.Get (New_Model, I + 1) = Models.Get (Old, I)))
  with Ghost;

New is Old with Item at At_Index

Removed

function Removed (Old, New_Model : Models.Sequence; At_Index : Positive) return Boolean is
  (At_Index <= Models.Length (Old)
   and then Models.Length (Old) < Natural'Last
   and then Models.Length (New_Model) = Models.Length (Old) - 1
   and then (for all I in 1 .. At_Index - 1 => Models.Get (New_Model, I) = Models.Get (Old, I))
   and then (for all I in At_Index + 1 .. Models.Length (Old) => Models.Get (New_Model, I - 1) = Models.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);

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) => Models.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 => not Is_Full (Container) 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

Append

procedure Append (Container : in out List; New_Item : Element_Type)
  with Global => null,
       Pre => not Is_Full (Container),
       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 => not Is_Full (Container),
       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 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.Get (Model (Container), I) = Models.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));