Generic package: Ada.Containers.Vectors

Dependencies

with Ada.Finalization;
with Ada.Streams;

Description

AI-302 Reference Implementation

Copyright (C) 2003-2004 Matthew J Heaney

The AI-302 Reference Implementation is free software; you can redistribute it and/or modify it under terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. Charles is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License distributed with Charles; see file COPYING.TXT. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

As a special exception, if other files instantiate generics from this unit, or you link this unit with other files to produce an executable, this unit does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU Public License.

The AI-302 Reference Implementation is maintained by Matthew J Heaney.

mailto:matthewjheaney@earthlink.net http://home.earthlink.net/~matthewjheaney/index.html


Header

generic

   type Index_Type is range <>;

   type Element_Type is private;

   with function "=" (Left, Right : Element_Type)
     return Boolean is <>;

package Ada.Containers.Vectors is
 
pragma Preelaborate (Vectors);

Type Summary

Cursor
Primitive Operations:  Delete, Element, Find, First, Has_Element, Insert, Insert, Insert, Insert, Insert_Space, Last, Next, Next, Previous, Previous, Replace_Element, Reverse_Find, Swap, To_Cursor, To_Index, Update_Element
Iterate_Process
Update_Element_Process
Vector
Primitive Operations:  "&", "&", "&", "&", "=", Append, Append, Assign, Capacity, Clear, Delete, Delete, Delete_First, Delete_Last, Element, Find, Find, First, First_Element, First_Index, Insert, Insert, Insert, Insert, Insert, Insert, Insert_Space, Insert_Space, Is_Empty, Is_In, Iterate, Last, Last_Element, Last_Index, Length, Move, Prepend, Prepend, Replace_Element, Reverse_Find, Reverse_Find, Reverse_Iterate, Set_Capacity, Set_Length, Swap, Swap, To_Cursor, To_Vector, To_Vector, Update_Element

Constants and Named Numbers

Empty_Vector : constant Vector;
Represents the empty vector container. It has a length of 0. If an object of type Vector is not otherwise initialized, it will be initialized to the same value as Empty_Vector.
No_Element : constant Cursor;
Represents a cursor that designates no element. If an object of type Cursor is not otherwise initialized, it will be initialized to the same value as No_Element.

Other Items:

pragma Assert (Index_Type'Base'First < Index_Type'First);

subtype Index_Subtype is Index_Type;

type Vector is tagged private;

type Cursor is private;

function To_Vector (Count : Count_Type) return Vector;
Returns a vector with a length of Count. The vector is filled with empty elements.

function To_Vector (New_Item : Element_Type;
                    Count    : Count_Type)
   return Vector;
Returns a vector with a length of Count, and elements initialized to the value New_Item.

function "&" (Left, Right : Vector) return Vector;
Returns a vector comprising the elements of Right appended in their original order to the vector Left.

function "&" (Left  : Vector;
              Right : Element_Type) return Vector;
Returns a vector comprising the element Right appended to the vector Left.

function "&" (Left  : Element_Type;
              Right : Vector) return Vector;
Returns a vector comprising the element Left prepended to the vector Right.

function "&" (Left, Right : Element_Type) return Vector;
Returns a vector comprising the element Left followed by the element Right.

function "=" (Left, Right : Vector) return Boolean;
If Left denotes the same object as Right, then this function returns True. Otherwise if Left and Right have different lengths, then this function returns False. Otherwise, it compares each element in Left to the corresponding element in Right using the generic formal equality operator. If element equality returns False, then this function returns False; otherwise, this function returns True. Any exceptions raised during evaluation of element equality are propagated.

function Capacity (Container : Vector) return Count_Type;
Returns the length of the internal array.

procedure Set_Capacity (Container : in out Vector;
                        Capacity  : in     Count_Type);
If Length (Container) > Count, Constraint_Error is raised. Otherwise, Set_Capacity sets the capacity of Container to a value which is at least the value Count, expanding (or contracting) the internal array to hold at least Count elements. Expansion or contraction will require allocation, and possibly copying and deallocation of elements. Any exceptions raised by these operations re propagated, leaving the container with at least the original Length and elements.

function Length (Container : Vector) return Count_Type;
The Length function returns the number of active elements in Container.

function Is_Empty (Container : Vector) return Boolean;
Equivalent to Length (Container) = 0.

procedure Clear (Container : in out Vector);
Sets the length of Container to 0. Its capacity does not change.

function To_Cursor (Container : Vector;
                    Index     : Index_Type'Base)
   return Cursor;
If the value of Index is not in the range First_Index (Container) .. Last_Index (Container), then No_Element is returned. Otherwise, a cursor designating the Element currently at Index in Container is returned.

function To_Index (Position : Cursor) return Index_Type'Base;
If Position is No_Element, Constraint_Error is propagated. Otherwise, the index (within its containing vector) of the element designated by Cursor is returned.

function Element (Container : Vector;
                  Index     : Index_Type'Base)
   return Element_Type;
If Index is not in the range First_Index (Container) .. Last_Index (Container), then Constraint_Error is propagated. Otherwise this function returns the element at position Index.

function Element (Position : Cursor) return Element_Type;
Returns the element designated by Position. If Position equals No_Element, then Constraint_Error is propagated.

type Update_Element_Process is
   access procedure (Element : in out Element_Type);
If Element_Type is unconstrained and definite, then the Element parameter shall be unconstrained.

procedure Update_Element (Container : in Vector;
                          Index     : in Index_Type'Base;
                          Process   :    Update_Element_Process);
Ada0Y not null access procedure (Element : in out Element_Type));

The best we can do in Ada95 is to generate a run-time error, so if Process is null a Program_Error is raised.

If Index is not in the range First_Index (Container) .. Last_Index (Container), then Constraint_Error is propagated. Otherwise, it calls Process.all with the element at position Index as the parameter. Any exceptions raised by Process are propagated.


procedure Update_Element (Position : in Cursor;
                          Process  :    Update_Element_Process);
Ada0Y not null access procedure (Element : in out Element_Type));

The best we can do in Ada95 is to generate a run-time error, so if Process is null a Program_Error is raised.

Calls Process.all with the element designated by Position as the parameter. If Position equals No_Element, then Constraint_Error is propagated. Any exceptions raised by Process are propagated.


procedure Replace_Element (Container : in Vector;
                           Index     : in Index_Type'Base;
                           By        : in Element_Type);
If Index does not specify a value in the range First_Index (Container) .. Last_Index (Container), then Constraint_Error is propagated. Otherwise this function assigns the value By to the element at position Index. Any exceptions raised during the assignment are propagated. The element at position Index is not an empty element after successful completion of this operation.

procedure Replace_Element (Position : in Cursor;
                           By       : in Element_Type);
This function assigns the value By to the element designated by Position. If Position equals No_Element, then Constraint_Error is propagated. Any exceptions raised during the assignment are propagated. The element designated by Position is not an empty element after successful completion of this operation.

procedure Assign (Target : in out Vector;
                  Source : in     Vector);
If Target denotes the same object as Source, then the operation has no effect. Otherwise, Assign first calls Clear (Target), then Set_Capacity (Target, Length (Source)). It then assigns the active elements of Source to the corresponding positions in Target, and then sets the length of Target to the length of Source. Any exceptions raised during element assignment are propagated.

procedure Move (Target : in out Vector;
                Source : in out Vector);
If Target denotes the same object as Source, then the operation has no effect. If Length (Target) is greater than 0, then it raises Constraint_Error . Otherwise, the internal array is removed from Source (making its size 0) and moved to Target (making its size the original size of Source). The length of Target is set to the length of Source, and the length of Source is set to 0.

procedure Insert (Container : in out Vector;
                  Before    : in     Index_Type'Base;
                  New_Item  : in     Vector);
If Length (New_Item) is 0, then Insert does nothing. Otherwise, it calculates the new length *NL* as the sum of the current length and Length (New_Item); if the value of Last appropriate for length NL would be greater than Index_Type'Last then Constraint_Error is propagated. If the value of Before is not in the range First_Index (Container) .. Index_Type'Succ (Last_Index (Container)), then Constraint_Error is propagated.

If the current vector capacity is less than or equal to the new length, Set_Capacity (Container, NL) is called to increase the vector capacity. Then Insert slides the elements in the range Before .. Last_Index (Container) up by Length (New_Item) positions, and then copies the elements of New_Item to the positions starting at Before. Any exceptions raised during the copying are propagated.


procedure Insert (Container : in out Vector;
                  Before    : in     Cursor;
                  New_Item  : in     Vector);
If Before is No_Element, then is equivalent to Insert (Container, Index_Type'Succ (Last_Index (Container)), New_Item); otherwise is equivalent to Insert (Container, To_Index (Before), New_Item);

procedure Insert (Container : in out Vector;
                  Before    : in     Cursor;
                  New_Item  : in     Vector;
                  Position  :    out Cursor);
Create a temporary (call it Temp_Index) and set it to Index_Type'Succ (Last_Index (Container)) if Before equals No_Element, and To_Index (Before) otherwise. Then Insert (Container, Before, New_Item) is called, and finally Position is set to To_Cursor (Container, Temp_Index).

procedure Insert (Container : in out Vector;
                  Before    : in     Index_Type'Base;
                  New_Item  : in     Element_Type;
                  Count     : in     Count_Type := 1);
Equivalent to Insert (Container, Before, To_Vector (New_Item, Count));

procedure Insert (Container : in out Vector;
                  Before    : in     Cursor;
                  New_Item  : in     Element_Type;
                  Count     : in     Count_Type := 1);
Equivalent to Insert (Container, Before, To_Vector (New_Item, Count));

procedure Insert (Container : in out Vector;
                  Before    : in     Cursor;
                  New_Item  : in     Element_Type;
                  Position  :    out Cursor;
                  Count     : in     Count_Type := 1);
Equivalent to Insert (Container, Before, To_Vector (New_Item, Count), Position);

procedure Prepend (Container : in out Vector;
                   New_Item  : in     Vector);
Equivalent to Insert (Container, Index_Type'First, New_Item).

procedure Prepend (Container : in out Vector;
                   New_Item  : in     Element_Type;
                   Count     : in     Count_Type := 1);
Equivalent to Insert (Container, Index_Type'First, New_Item, Count).

procedure Append (Container : in out Vector;
                  New_Item  : in     Vector);
Equivalent to Insert (Container, Index_Type'Succ (Last_Index (Container)), New_Item).

procedure Append (Container : in out Vector;
                  New_Item  : in     Element_Type;
                  Count     : in     Count_Type := 1);
Equivalent to Insert (Container, Index_Type'Succ (Last_Index (Container)), New_Item, Count).

procedure Insert_Space (Container : in out Vector;
                        Before    : in     Index_Type'Base;
                        Count     : in     Count_Type := 1);
Equivalent to Insert (Container, Before, New_Item, Count), with the difference that the inserted elements are empty elements.

procedure Insert_Space (Container : in out Vector;
                        Before    : in     Cursor;
                        Position  :    out Cursor;
                        Count     : in     Count_Type := 1);
Create a temporary (call it Temp_Index) and set it to Index_Type'Succ (Last_Index (Container)) if Before equals No_Element, and To_Index (Before) otherwise. Then Insert_Space (Container, Temp_Index, Count) is called, and finally Position is set to To_Cursor (Container, Temp_Index).

procedure Set_Length (Container : in out Vector;
                      Length    : in     Count_Type);
Calls Set_Capacity (Length), then sets the length of the Container to Length. If Length is greater than the original length of Container, the added elements are empty elements.

procedure Delete (Container : in out Vector;
                  Index     : in     Index_Type'Base;
                  Count     : in     Count_Type := 1);
If Count is 0, the operation has no effect. If Index does not specify a value in the range First_Index (Container) .. Last_Index (Container), then Constraint_Error is propagated. Otherwise Delete slides the active elements (if any) starting Index plus Count down to Index. Any exceptions raised during element assignment are propagated.

procedure Delete (Container : in out Vector;
                  Position  : in out Cursor;
                  Count     : in     Count_Type := 1);
If Count is 0, the operation has no effect. Otherwise is equivalent to Delete (Container, To_Index (Position), Count).

procedure Delete_First (Container : in out Vector;
                        Count     : in     Count_Type := 1);
Equivalent to Delete (Container, Index_Type'First, Count).

procedure Delete_Last (Container : in out Vector;
                       Count     : in     Count_Type := 1);
If Length (Container) < Count then is equivalent to Delete (Container, Index_Type'First, Count); otherwise is equivalent to Delete (Container, Index_Type'Val (Index_Type'Pos (Last_Index(Container)) - Count + 1), Count).

function First_Index (Container : Vector) return Index_Type;
Returns the value Index_Type'First.

function First (Container : Vector) return Cursor;
If Container is empty, First returns the value No_Element. Otherwise, returns a cursor that designates the first element in Container.

function First_Element (Container : Vector) return Element_Type;
Equivalent to Element (Container, First_Index (Container)).

function Last_Index (Container : Vector) return Index_Type'Base;
Returns the position of the last element in Container.

function Last (Container : Vector) return Cursor;
If Container is empty, Last returns the value No_Element. Otherwise, returns a cursor that designates the last element in Container.

function Last_Element (Container : Vector) return Element_Type;
Equivalent to Element (Container, Last_Index (Container)).

procedure Swap (Container : in Vector;
                I, J      : in Index_Type'Base);
If I or J does not specify a value in the range First_Index (Container) .. Last_Index (Container), then Constraint_Error is propagated. Otherwise Swap exchanges the values of the elements at positions I and J.

procedure Swap (Container : in Vector;
                I, J      : in Cursor);
MJH: The inclusion of the Container parameter appears to be an error. ENDMJH.
  Equivalent to Swap (Container, To_Index (I), To_Index (J)).

generic
   with function "<" (Left, Right : Element_Type) return Boolean is <>;
procedure Generic_Sort (Container : in Vector);
Reorders the elements of Container such that the elements are sorted smallest first as determined by the generic formal "<" operator provided. Any exceptions raised during evalution of "<" are propagated.

function Find (Container : Vector;
               Item      : Element_Type;
               Index     : Index_Type'Base := Index_Type'First)
   return Index_Type'Base;
Searches the elements of Container for an element equal to Item, starting at position Index. If Index is less than Index_Type'First, then Constraint_Error is propagated. If there are no elements in the range Index .. Last_Index (Container) equal to Item, then Find returns Index_Type'Succ (Last_Index (Container)). Otherwise, it returns the index of the matching element.

function Find (Container : Vector;
               Item      : Element_Type;
               Position  : Cursor := No_Element)
   return Cursor;
Searches the elements of Container for an element equal to Item, starting at the first element if Cursor equals No_Element, and at the element designated by Cursor otherwise, and searching to the last element in Container. If an item equal to Item is found, Find returns a cursor designating the first element found equal to Item. If no such item is found, it returns No_Element.

function Reverse_Find (Container : Vector;
                       Item      : Element_Type;
                       Index     : Index_Type'Base := Index_Type'Last)
   return Index_Type'Base;
Searches the elements of Container in reverse for an element equal to Item, starting at position Index . If Index is greater than Last_Index (Container), then the search begins at position Last_Index (Container). If there are no elements in the range Index_Type'First .. Index equal to Item, then Reverse_Find returns Index_Type'Succ (Last_Index (Container)). Otherwise, it returns the index of the matching element.

function Reverse_Find (Container : Vector;
                       Item      : Element_Type;
                       Position  : Cursor := No_Element)
   return Cursor;
Searches the elements of Container for an element equal to Item, starting at the last element if Cursor equals No_Element, and at the element designated by Cursor otherwise, and searching backwards to the first element in Container. If an item equal to Item is found, Find returns a cursor designating the first element found equal to Item. If no such item is found, it returns No_Element.

function Is_In (Item      : Element_Type;
                Container : Vector)
   return Boolean;
MJH: I have left the parameters in this order, pending a ruling from the ARG. ENDMJH.
  Equivalent to Has_Element (Find (Container, Item)).

function Next (Position : Cursor) return Cursor;
If Position equals No_Element or designates the last element of the container, then Next returns the value No_Element. Otherwise, returns a cursor that designates the element with index Index_Type'Succ (To_Index (Position)) in the same vector as Position.

function Previous (Position : Cursor) return Cursor;
If Position equals No_Element or designates the first element of the container, then Previous returns the value No_Element. Otherwise, returns a cursor that designates the element with index Index_Type'Pred (To_Index (Position)) in the same vector as Position.

procedure Next (Position : in out Cursor);
Equivalent to Position := Next (Position).

procedure Previous (Position : in out Cursor);
Equivalent to Position := Previous (Position).

function Has_Element (Position : Cursor) return Boolean;
Returns True if Position designates an element, and returns False otherwise.

type Iterate_Process is
   access procedure (Position : Cursor);

procedure Iterate
  (Container : in Vector;
   Process   :    Iterate_Process);
Ada0Y not null access procedure (Position : Cursor));

The best we can do in Ada95 is to generate a run-time error, so if Process is null a Program_Error is raised.

  Invokes Process.all with a cursor that designates each
  element in Container, in index order. Any exceptions raised
  during Process are propagated.

procedure Reverse_Iterate
  (Container : in Vector;
   Process   :    Iterate_Process);
Ada0Y not null access procedure (Position : Cursor));

The best we can do in Ada95 is to generate a run-time error, so if Process is null a Program_Error is raised.

  Iterates over the nodes in Container as per Iterate
  , except that elements are traversed in reverse order.

private

   --  Implementation-defined ...
end Ada.Containers.Vectors;