On this page
Generic package Verada.Containers.Unbounded.Nested_Vectors
Verada.Containers.Unbounded.Nested_Vectors: vectors whose elements are vectors (Vibe's list[list[T]], D188), the inner vectors those of Inner, held in place. The vector is limited, as its rows are. Its model is each row's length and each row's elements, by position from 1; an operation on one row says what it does to that row and keeps every row's length. Only memory limits the lengths: an operation that lengthens the vector or a row raises Storage_Error when memory cannot hold the result (Verada.Storage).
generic
with package Inner is new Verada.Containers.Unbounded.Vectors (<>);Length_Array
type Length_Array is array (Positive range <>) of Natural;Same_Except
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;the arrays have the same lengths, except perhaps at Index
Prefix_Of
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;A's first elements are B's
Vector
type Vector is limited private
with Default_Initial_Condition => Length (Vector) = 0,
Finalizable => (Finalize => Clear, Relaxed_Finalization => True);Length
function Length (Container : Vector) return Natural
with Global => null,
Post => Length'Result < Verada.Storage.Length_Bound;how many rows
Row_Length
function Row_Length (Container : Vector; Index : Positive) return Natural
with Global => null,
Pre => Index <= Length (Container),
Post => Row_Length'Result < Verada.Storage.Length_Bound;the length of the row at Index
Lengths
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));each row's length, by position
Row
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);the row at Index, as its elements
Element
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);the element at Position of the row at Index (xs[i][j])
Row_Copy
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);a copy of the row at Index, a vector of its own (ys = xs[i] where ys is only read)
Append
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);a copy of Item as a new last row (xs.append(row))
Append
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);New_Item at the end of the row at Index (xs[i].append(x))
Replace_Element
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;New_Item at Position of the row at Index (xs[i][j] = x)
Take
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);the row at Index moved into Item, which was empty, and the row left empty (a view of xs[i] that is changed, put back after)
Put_Back
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);Item moved back as the row at Index, which was empty, and Item left empty
Clear
procedure Clear (Container : in out Vector)
with Global => null, Always_Terminates,
Post => Length (Container) = 0;