Containers: Vectors & Lists

Instantiate Ada.Containers.Vectors and Doubly_Linked_Lists (and their Indefinite and Bounded variants), use Append/Prepend/Insert/Delete, indexing and element iteration with in-place update, cursors, Find/Find_Index/Contains, Generic_Sorting, Ada 2022 container aggregates, and tampering checks.

Ada.Containers is Ada's standard collections library: vectors, doubly linked lists, hashed and ordered maps and sets, multiway trees, holders and queues — all generic, all with automatic memory management. Vectors are growable arrays; lists offer constant-time insertion anywhere. Both support the same iteration syntax as arrays.

Vectors

'package Int_Vectors is new Ada.Containers.Vectors (Index_Type => Positive, Element_Type => Integer);' then 'V : Int_Vectors.Vector;'. Operations: Append, Prepend, Insert (Before, New_Item), Delete (Index, Count), Delete_Last, Length, Is_Empty, Clear, First_Element, Last_Element, Find_Index, Contains, Reverse_Elements, Reserve_Capacity. V (I) reads and writes elements (Variable_Indexing). 'for E of V loop E := E * 2; end loop;' updates in place. Nested package Generic_Sorting provides Sort, Is_Sorted and Merge. In Ada 2022, container aggregates initialize directly: V : Vector := [5, 3, 8];.

• Vectors (Index_Type, Element_Type). • Append / Insert / Delete / Find_Index / Contains / Reverse_Elements. • V (I) indexing; for E of V (updatable). • Generic_Sorting.Sort; container aggregates [..] in Ada 2022.

Cursors and Lists

A cursor designates a position in a container: First, Last, Next, Previous, Has_Element, Element (C), and for vectors To_Index (C). 'for C in V.Iterate loop' iterates cursors when you need positions as well as elements. Doubly_Linked_Lists have no indexing but support O(1) Insert (Before => Cursor), Delete (Cursor), Splice and Find. After Delete the cursor becomes No_Element. Tampering checks raise Program_Error if you insert into or delete from a container while iterating over it or holding a reference to an element.

• Cursor ops: First / Next / Has_Element / Element. • Lists: Prepend / Insert (Before => C) / Delete (C) / Splice. • Tampering during iteration → Program_Error.

Variants

Indefinite_Vectors / Indefinite_Doubly_Linked_Lists store indefinite elements such as String or Shape'Class (each element is allocated separately). Bounded_Vectors and Bounded_Doubly_Linked_Lists have a fixed capacity and no dynamic allocation, suitable for embedded and SPARK code (SPARK also has its own formal containers). Choose vectors for index access and appending, lists for frequent insertion and deletion in the middle.

• Indefinite_*: String, T'Class elements. • Bounded_*: fixed capacity, no heap. • Vector = index access; List = cheap mid-insertion.

Code Examples

Starting from the aggregate [5, 3, 8], Append/Prepend/Insert give 9 5 7 3 8 1. After sorting and deleting two elements, V (1):= 100 and the doubling loop update in place. Indexing past the end raises Constraint_Error. Output: After Append/Prepend/Insert: 9 5 7 3 8 1 (length 6) First = 9, V (2) = 5, index of 8 = 5 Sorted: 1 3 5 7 8 9 (length 6) Deleted first two: 5 7 8 9 (length 4) Doubled: 200 14 16 18 (length 4) Reversed: 18 16 14 200 (length 4) Contains 16? TRUE 1-> 18 2-> 16 3-> 14 4-> 200 List: 1 2 3 4 first 1, last 4 Deleted 3, length 3, cursor valid? FALSE name: Ada ( 3 chars) name: Grace Hopper ( 12 chars) name: Alan ( 4 chars) V (10) raised Constraint_Error (index out of range) for E of V loop if E < 0 then V.Delete (V.Find_Index (E)); end if; end loop;

-- collect indexes first, or iterate backwards with explicit indexes

Deleting while iterating tampers with the container and raises Program_Error.

package Names is new Ada.Containers.Vectors (Positive, String);

package Names is new Ada.Containers.Indefinite_Vectors (Positive, String);

String is indefinite; use the Indefinite variant (or Unbounded_String elements).

Sort (V); -- no such subprogram package Sorting is new Int_Vectors.Generic_Sorting; Sorting.Sort (V);

Sorting is provided by the nested generic Generic_Sorting.

Which container is best for frequent insertions in the middle given a position? A. Vector B. Doubly_Linked_List C. Ordered_Set D. Holder

How do you store Strings of different lengths in a vector? A. Vectors (Positive, String) B. Indefinite_Vectors (Positive, String) C. Impossible D. Bounded_Vectors

What happens if you delete from a vector while a 'for E of V' loop runs over it? A. Works B. Program_Error (tampering check) C. Undefined behavior D. Constraint_Error

Which Ada 2022 feature allows 'V: Vector:= [1, 2, 3];'? A. Container aggregates (Aggregate aspect) B. Implicit conversion C. Generic_Sorting D. Iterators Trace the vectors/lists example.

Real output: After Append/Prepend/Insert: 9 5 7 3 8 1 (length 6) First = 9, V (2) = 5, index of 8 = 5 Sorted: 1 3 5 7 8 9 (length 6) Deleted first two: 5 7 8 9 (length 4) Doubled: 200 14 16 18 (length 4) Reversed: 18 16 14 200 (length 4) Contains 16? TRUE 1-> 18 2-> 16 3-> 14 4-> 200 List: 1 2 3 4 first 1, last 4 Deleted 3, length 3, cursor valid? FALSE name: Ada ( 3 chars) name: Grace Hopper ( 12 chars) name: Alan ( 4 chars) V (10) raised Constraint_Error (index out of range)

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
with Ada.Containers.Doubly_Linked_Lists;
with Ada.Containers.Indefinite_Vectors;

procedure L47_Vectors_Lists is
   use Ada.Containers;

   package Int_Vectors is new Vectors (Index_Type => Positive, Element_Type => Integer);
   package Int_Sorting is new Int_Vectors.Generic_Sorting;          --  uses "<" by default
   package Int_Lists is new Doubly_Linked_Lists (Integer);
   package String_Vectors is new Indefinite_Vectors (Positive, String);   --  any-length elements

   procedure Show (Label : String; V : Int_Vectors.Vector) is
   begin
      Put (Label & ":");
      for E of V loop
         Put (Integer'Image (E));
      end loop;
      Put_Line ("   (length" & Count_Type'Image (V.Length) & ")");
   end Show;

   V : Int_Vectors.Vector := [5, 3, 8];         --  Ada 2022 container aggregate
   L : Int_Lists.List;
   Names : String_Vectors.Vector;
begin
   V.Append (1);
   V.Prepend (9);
   V.Insert (Before => 3, New_Item => 7);
   Show ("After Append/Prepend/Insert", V);
   Put_Line ("First =" & Integer'Image (V.First_Element) & ", V (2) =" & Integer'Image (V (2))
             & ", index of 8 =" & Integer'Image (V.
…