comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Ada.Containers.Vectors - querying multiple elements
Date: Wed, 27 Apr 2005 04:59:29 GMT
Date: 2005-04-27T04:59:29+00:00	[thread overview]
Message-ID: <R_Ebe.13911$lP1.2554@newsread1.news.pas.earthlink.net> (raw)
In-Reply-To: <426e4c2b$0$7515$9b4e6d93@newsread2.arcor-online.net>

Georg Bauhaus wrote:

> Duncan Sands wrote:
> 
>>    procedure Query_Elements
>>        (Container   : in Vector;
>>         First_Index : in Index_Type;
>>         Last_Index  : in Extended_Index;
>>         Process   : not null access procedure (Elements : in 
>> Element_Array));
> 
> 
> Couldn't you use Cursors,

IMNSHO, a unbounded-array abstraction doesn't need such operations, nor 
cursors. One should use it in a manner very similar to an array:

for I in Index'First .. Last (Container) loop
    -- operate on Get (Container, I)
end loop;

During early discussions on AI-302 I implemented an unbounded-array 
package based on Ada.Strings.Unbounded, from the point of view that the 
basic operations in Unbounded are an instantiation of the 
unbounded-array with Character, Positive, and String. Here's the spec, 
if anyone's interested (lines may wrap):

with Ada.Finalization;
with System;
generic -- Unbounded_Arrays
    type Index is (<>);
    type Element is private;
    type Fixed is array (Index range <>) of Element;

    with function "=" (Left : Element; Right : Element) return Boolean 
is <>;
package Unbounded_Arrays is
    pragma Preelaborate;

    type Unbounded_Array is private;
    -- Initial value: null, equal to Null_Unbounded_Array, Length = 0.
    --
    -- An Unbounded_Array is similar to a Fixed. It has a lower bound of 
Index'First,
    -- and an upper bound that can vary dynamically up to Index'Last.

    Null_Unbounded_Array : constant Unbounded_Array;

    -- Conversions

    function To_Unbounded (Source : Fixed) return Unbounded_Array;
    function "+" (Source : Fixed) return Unbounded_Array renames 
To_Unbounded;
    -- Converts Source to an Unbounded_Array.

    function To_Fixed (Source : Unbounded_Array) return Fixed;
    function "+" (Source : Unbounded_Array) return Fixed renames To_Fixed;
    -- Converts Source to a Fixed. If Source is null and there is no 
representation
    -- for a null Fixed, raises Constraint_Error.

    -- Size and index information

    Invalid_Index : exception;
    -- Raised by operations dealing with Index when the Index is invalid.

    function Last (Source : Unbounded_Array) return Index;
    -- Returns the Index of the last position occupied in Source.
    -- If Source is null, returns Index'First.
    -- Similar to the array operation Source'Last.

    type Count is mod System.Max_Binary_Modulus;

    function Length (Source : Unbounded_Array) return Count;
    -- Returns the number of occupied positions in Source.
    -- Similar to the array operation Source'Length.
    -- If the result is > Count'Last, raises Constraint_Error.

    -- Access to Elements

    function Get (From : Unbounded_Array; Location : Index) return Element;
    -- Returns the Element stored in From at position Location.
    -- Similar to the array operation From (Location).
    --
    -- Precondition:
    --    Length (From) > 0 and
    --    Location in Index'First .. Last (From)     raises 
Invalid_Index if violated

    procedure Put (Into : in out Unbounded_Array; Location : in Index; 
Item : in Element);
    -- Changes the Element stored in Into at position Location to be Item.
    -- Similar to the array operation Into (Location) := Item.
    --
    -- Precondition:
    --    Length (Into) > 0 and
    --    Location in Index'First .. Last (Into)     raises 
Invalid_Index if violated
    --
    -- Postcondition:
    --    Get (Into, Location) = Item

    function Slice (From : Unbounded_Array; Low : Index; High : Index) 
return Fixed;
    function Slice (From : Unbounded_Array; Low : Index; High : Index) 
return Unbounded_Array;
    -- Return the slice of From with bounds Low .. High. Similar to the 
array operation
    -- From (Low .. High).
    -- The version that returns a Fixed returns a value with bounds of 
Low .. High.
    -- The version that returns an Unbounded_Array follows the 
specification of the type:
    -- the lower bound is always Index'First.
    --
    -- Precondition:
    --    Low > High                          or
    --    (Length (From) > 0                  and
    --     Low  in Index'First .. Last (From) and
    --     High in Index'First .. Last (From) )       raises 
Invalid_Index if violated

    procedure Replace_Slice (Into : in out Unbounded_Array; Low : Index; 
High : Index; Replacement : Fixed);
    procedure Replace_Slice (Into : in out Unbounded_Array; Low : Index; 
High : Index; Replacement : Unbounded_Array);
    -- Replaces the slice of Into with bounds Low .. High with Replacement.
    -- Similar to the array operation Into (Low .. High) := Replacement.
    --
    -- Preconditions:
    --    (Low > High and [Replacement'Length or Length (Replacement)] = 
0) or
    --    (Length (Into) > 0                  and
    --     Low  in Index'First .. Last (Into) and
    --     High in Index'First .. Last (Into) )       raises 
Invalid_Index if violated
    --
    --    [Replacement'Length or Length (Replacement)] =
    --    Length (Slice (Into, Low, High) )           raises 
Constraint_Error if violated
    --
    -- Postcondition:
    --    Slice (Into, Low, High) = Replacement

    -- Concatenation

    procedure Append (Onto : in out Unbounded_Array; Item : in Element);
    procedure Append (Onto : in out Unbounded_Array; Item : in Fixed);
    procedure Append (Onto : in out Unbounded_Array; Item : in 
Unbounded_Array);
    -- Equivalent to Onto := Onto & Item.

    function "&" (Left : Unbounded_Array; Right : Unbounded_Array) 
return Unbounded_Array;
    function "&" (Left : Unbounded_Array; Right : Fixed) 
return Unbounded_Array;
    function "&" (Left : Fixed;           Right : Unbounded_Array) 
return Unbounded_Array;
    function "&" (Left : Unbounded_Array; Right : Element) 
return Unbounded_Array;
    function "&" (Left : Element;         Right : Unbounded_Array) 
return Unbounded_Array;
    -- Similar to the equivalent array operations.

    -- Equality

    function "=" (Left : Unbounded_Array; Right : Unbounded_Array) 
return Boolean;
    function "=" (Left : Unbounded_Array; Right : Fixed) 
return Boolean;
    function "=" (Left : Fixed;           Right : Unbounded_Array) 
return Boolean;
    -- Returns True if Left and Right represent the same array value; 
False otherwise
private -- Unbounded_Arrays
    -- Not specified by the language ...
end Unbounded_Arrays;

Unlike the Vector container that will be part of Ada 0X, this has no 
restrictions on the Index subtype. Any discrete type is acceptable.

-- 
Jeff Carter
"What I wouldn't give for a large sock with horse manure in it."
Annie Hall
42



  parent reply	other threads:[~2005-04-27  4:59 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-26 11:43 Ada.Containers.Vectors - querying multiple elements Duncan Sands
2005-04-26 14:12 ` Georg Bauhaus
2005-04-26 14:39   ` Duncan Sands
2005-04-26 15:44     ` Matthew Heaney
2005-04-26 16:05       ` Duncan Sands
     [not found]       ` <1114531544.32583.142.camel@localhost.localdomain>
     [not found]         ` <426E72C3.9070108@on2.com>
2005-04-26 16:59           ` Duncan Sands
     [not found]           ` <1114534751.32583.144.camel@localhost.localdomain>
     [not found]             ` <426E73DE.2070505@on2.com>
2005-04-26 17:08               ` Duncan Sands
2005-04-26 18:17                 ` Martin Dowie
2005-04-26 18:48                   ` Duncan Sands
2005-04-26 18:59           ` Duncan Sands
2005-04-26 19:05             ` Georg Bauhaus
2005-04-26 20:34               ` Duncan Sands
2005-04-26 21:47               ` Dr. Adrian Wrigley
2005-04-26 23:21                 ` Marius Amado Alves
     [not found]                 ` <9decddc0038674b3c85aeceefb4d3b83@netcabo.pt>
2005-04-27  8:15                   ` Duncan Sands
     [not found]                   ` <1114589729.10418.13.camel@localhost.localdomain>
2005-04-27 11:49                     ` Marius Amado Alves
2005-04-28  0:36                       ` Randy Brukardt
2005-04-28  7:09                         ` Duncan Sands
2005-04-27 11:10                 ` Georg Bauhaus
2005-04-27 11:57                   ` Duncan Sands
2005-04-28 14:17       ` Duncan Sands
2005-04-27  4:59   ` Jeffrey Carter [this message]
2005-04-27  7:21     ` Duncan Sands
2005-04-28  2:54       ` Jeffrey Carter
2005-04-28  7:15         ` Duncan Sands
2005-04-28 12:27           ` Matthew Heaney
2005-04-28 13:18           ` Matthew Heaney
2005-04-28 13:53             ` Duncan Sands
2005-04-29  2:46           ` Jeffrey Carter
2005-04-29 18:22             ` Robert A Duff
2005-04-28  7:18         ` Duncan Sands
2005-04-28  0:33     ` Randy Brukardt
2005-04-28  3:09       ` Jeffrey Carter
2005-04-28 20:55         ` Randy Brukardt
2005-04-29  2:54           ` Jeffrey Carter
2005-04-29 18:34             ` Robert A Duff
2005-04-29 20:18               ` Randy Brukardt
2005-04-29 20:00             ` Randy Brukardt
2005-04-30  4:06               ` Jeffrey Carter
2005-04-29  7:52           ` Dmitry A. Kazakov
2005-04-29 20:26             ` Randy Brukardt
2005-04-30  9:24               ` Dmitry A. Kazakov
2005-05-02  3:21                 ` Randy Brukardt
2005-05-02 17:04                   ` Dmitry A. Kazakov
2005-05-02 18:57                     ` Robert A Duff
2005-05-03  8:14                       ` Dmitry A. Kazakov
2005-05-03 23:30                         ` Robert A Duff
2005-05-05 10:51                           ` Dmitry A. Kazakov
2005-05-07  1:20                             ` Matthew Heaney
2005-05-07  7:17                               ` Dmitry A. Kazakov
     [not found] <1114515832.32583.41.camel@localhost.localdomain>
     [not found] ` <426E5A0B.3010109@on2.com>
2005-04-26 16:00   ` Duncan Sands
2005-04-28  0:54     ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox