DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Verada.Containers.Unbounded.Nested_Vectors

A growable limited vector whose elements are vectors of an instance of Verada.Containers.Unbounded.Vectors: Vibe's list[list[T]].

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

The rows are Inner.Vectors held in place, so the vector is limited, as they are; a row is copied out (Row_Copy), changed in place (Append, Replace_Element), or moved out and back (Take, Put_Back). The model is each row's length and each row's elements, by position from 1; an operation on one row states what it does to that row and keeps every row's length. Only memory limits the lengths: Append raises Storage_Error when memory cannot hold the result (Verada.Storage). The body is proved.

A client proof uses these contracts without proving the body again (see library assurance).

Import and instantiate

with Verada.Containers.Unbounded.Vectors;
with Verada.Containers.Unbounded.Nested_Vectors;
package Rows is new Verada.Containers.Unbounded.Vectors (Integer);
package Grids is new Verada.Containers.Unbounded.Nested_Vectors (Rows);
import Verada.Containers.Unbounded.Vectors;
import Verada.Containers.Unbounded.Nested_Vectors;
package Rows = new Verada.Containers.Unbounded.Vectors<Integer>;
package Grids = new Verada.Containers.Unbounded.Nested_Vectors<Rows>;

A Vibe module writes list[list[int]], which is this generic's instance; see library use across syntaxes.

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.

API 001

Generic formals (declaration) — The instance of Verada.Containers.Unbounded.Vectors whose vectors are the rows (Inner): a row and an Inner.Vector are of one type, so Vibe's list[T] and a row of a list[list[T]] are the same type.

Declared interface:

generic
   with package Inner is new Verada.Containers.Unbounded.Vectors (<>);

API 002

Length_Array (declaration) — Each row's length, by position from 1: the lengths part of the model.

Declared interface:

type Length_Array is array (Positive range <>) of Natural;

API 003

Same_Except (declaration) — Ghost: two lengths arrays of the same bounds that agree except perhaps at Index.

Declared interface:

function Same_Except (A, B : Length_Array; Index : Positive) return Boolean is
  (A'First = B'First and then A'Last = B'Last
   and then (for all I in A'Range => (if I /= Index then A (I) = B (I))))
  with Ghost;

API 004

Prefix_Of (declaration) — Ghost: B's lengths are A's first ones (both from 1).

Declared interface:

function Prefix_Of (A, B : Length_Array) return Boolean is
  (A'First = 1 and then B'First = 1 and then B'Last <= A'Last
   and then (for all I in B'Range => A (I) = B (I)))
  with Ghost;

API 005

Vector (declaration) — A limited vector of rows, each an Inner.Vector held in place; empty by default, its storage freed by Clear when it is finalized.

Declared interface:

type Vector is limited private
  with Default_Initial_Condition => Length (Vector) = 0,
       Finalizable => (Finalize => Clear, Relaxed_Finalization => True);

API 006

Length (declaration) — How many rows (Python's len(grid)).

Declared interface:

function Length (Container : Vector) return Natural
  with Global => null,
       Post => Length'Result < Verada.Storage.Length_Bound;

API 007

Row_Length (declaration) — The length of the row at Index (Python's len(grid[i]), Index from 1).

Declared interface:

function Row_Length (Container : Vector; Index : Positive) return Natural
  with Global => null,
       Pre => Index <= Length (Container),
       Post => Row_Length'Result < Verada.Storage.Length_Bound;

API 008

Lengths (declaration) — Each row's length, by position from 1: the model a client proof uses to know that an operation on one row keeps the others' lengths.

Declared interface:

function Lengths (Container : Vector) return Length_Array
  with Global => null,
       Post => Lengths'Result'First = 1 and then Lengths'Result'Last = Length (Container)
               and then (for all I in 1 .. Length (Container) => Lengths'Result (I) = Row_Length (Container, I));

API 009

Row (declaration) — The row at Index as its elements, from 1.

Declared interface:

function Row (Container : Vector; Index : Positive) return Inner.Element_Array
  with Global => null,
       Pre => Index <= Length (Container),
       Post => Row'Result'First = 1 and then Row'Result'Length = Row_Length (Container, Index);

API 010

Element (declaration) — The element at Position of the row at Index (Python's grid[i][j]).

Declared interface:

function Element (Container : Vector; Index : Positive; Position : Positive) return Inner.Element_Type
  with Global => null,
       Pre => Index <= Length (Container) and then Position <= Row_Length (Container, Index),
       Post => Element'Result = Row (Container, Index) (Position);

API 011

Row_Copy (declaration) — A copy of the row at Index, a vector of its own (Python's row = grid[i] where row is only read).

Declared interface:

function Row_Copy (Container : Vector; Index : Positive) return Inner.Vector
  with Global => null,
       Pre => Index <= Length (Container),
       Post => Inner.Model (Row_Copy'Result) = Row (Container, Index);

API 012

Append (declaration) — Append (Container, Item) stores a copy of Item as a new last row (Python's grid.append(row)); Append (Container, Index, New_Item) puts New_Item at the end of the row at Index (grid[i].append(x)). Each keeps the other rows' lengths; memory alone limits the lengths (Storage_Error, Verada.Storage).

Declared interface:

procedure Append (Container : in out Vector; Item : Inner.Vector)
  with Global => null,
       Post => Length (Container) = Length (Container)'Old + 1
               and Prefix_Of (Lengths (Container), Lengths (Container)'Old)
               and Row (Container, Length (Container)) = Inner.Model (Item);

API 013

Append (declaration) — Append (Container, Item) stores a copy of Item as a new last row (Python's grid.append(row)); Append (Container, Index, New_Item) puts New_Item at the end of the row at Index (grid[i].append(x)). Each keeps the other rows' lengths; memory alone limits the lengths (Storage_Error, Verada.Storage).

Declared interface:

procedure Append (Container : in out Vector; Index : Positive; New_Item : Inner.Element_Type)
  with Global => null,
       Pre => Index <= Length (Container),
       Post => Length (Container) = Length (Container)'Old
               and Row_Length (Container, Index) = Row_Length (Container, Index)'Old + 1
               and Same_Except (Lengths (Container), Lengths (Container)'Old, Index)
               and Inner.Appended (Row (Container, Index), Row (Container, Index)'Old, New_Item);

API 014

Replace_Element (declaration) — New_Item at Position of the row at Index (Python's grid[i][j] = x); every row's length kept.

Declared interface:

procedure Replace_Element (Container : in out Vector; Index : Positive; Position : Positive; New_Item : Inner.Element_Type)
  with Global => null,
       Pre => Index <= Length (Container) and then Position <= Row_Length (Container, Index),
       Post => Lengths (Container) = Lengths (Container)'Old
               and Inner.Same_Except (Row (Container, Index), Row (Container, Index)'Old, Position)
               and Element (Container, Index, Position) = New_Item;

API 015

Take (declaration) — The row at Index moved into Item, which was empty, and the row left empty: a name bound to grid[i] and changed (Vibe's element view, D127).

Declared interface:

procedure Take (Container : in out Vector; Index : Positive; Item : in out Inner.Vector)
  with Global => null,
       Pre => Index <= Length (Container) and then Inner.Length (Item) = 0,
       Post => Length (Container) = Length (Container)'Old
               and Inner.Model (Item) = Row (Container, Index)'Old
               and Row_Length (Container, Index) = 0
               and Same_Except (Lengths (Container), Lengths (Container)'Old, Index);

API 016

Put_Back (declaration) — Item moved back as the row at Index, which was empty, and Item left empty: the view's end.

Declared interface:

procedure Put_Back (Container : in out Vector; Index : Positive; Item : in out Inner.Vector)
  with Global => null,
       Pre => Index <= Length (Container) and then Row_Length (Container, Index) = 0,
       Post => Length (Container) = Length (Container)'Old
               and Row (Container, Index) = Inner.Model (Item)'Old
               and Inner.Length (Item) = 0
               and Same_Except (Lengths (Container), Lengths (Container)'Old, Index);

API 017

Clear (declaration) — No rows, the storage freed; the vector's finalization.

Declared interface:

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