comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: Arrays and Access types...
Date: Fri, 26 Oct 2001 05:39:09 GMT
Date: 2001-10-26T05:39:09+00:00	[thread overview]
Message-ID: <3BD8F70E.3667B286@worldnet.att.net> (raw)
In-Reply-To: 20011025214647.2788a60b.egm2@jps.net

Unlike C, Ada arrays are not directly related to access types or
pointers. 

There are several ways to solve your problem. My personal approach
would be to create a record structure to keep track of your slicing
on a single array element. One such data structure follows:

--- Slicable array package
generic
   Max_Size : Positive;
   type Element is private;
package Slicing_Array is
   
   subtype Array_Index is Positive range 1..Max_Size;
   type Element_Array is array(Array_Index range <>) of Element;
   type Slicable_Array is private;

   function Create(Item : Element_Array) return Slicable_Array;
   function First(Item : Slicable_Array) return Positive;
   function Last(Item : Slicable_Array) return Positive;
   function To_Array(Item : Slicable_Array) return Element_Array;
   procedure Slice(Item : in out Slicable_Array; 
                   First : Positive; Last : Positive);
   Bounds_Error : Exception; -- When slicing beyond the array bounds
private
   type Slicable_Array is record 
      First : Positive := 1;
      Last  : Positive := Max_Size;
      Buffer : Element_Array(Array_Index'Range);
   end record;
end Slicing_Array;

--- Slicable array package

package body Slicing_Array is
   function Create(Item : Element_Array) return Slicable_Array is
      Result : Slicable_Array;
   begin
      Result.Last := Item'Length;
      Result.Buffer(1..Result.Last) := Item;
      return Result;
   end Create;

   function First(Item : Slicable_Array) return Positive is
   begin
      return Item.First;
   end First;

   function Last(Item : Slicable_Array) return Positive is
   begin
      return Item.Last;
   end Last;

   function To_Array(Item : Slicable_Array) return Element_Array is
   begin
      return Item.Buffer(Item.First..Item.Last);
   end To_Array;

   procedure Slice(Item : in out Slicable_Array; 
                   First : Positive; Last : Positive) is
   begin
      if First not in Array_Index or Last not in Array_Index then
         raise Bounds_Error;
      end if;
      Item.First := First;
      Item.Last := Last; 
   end Slice;
end Slicing_Array;
   
Jim Rogers
Colorado Springs, Colorado USA

"Eric G. Miller" wrote:
> 
> New to Ada and trying to figure out how to deal with arbitrary
> slices of arrays.  I defined an array type and an access type
> for it, but I can't seem to figure out how to take a slice of
> the array and assign that slice to an access type.
> ...
> 
> What I want to do is progressively subset the array, and I
> won't know exactly how it'll get broken up until runtime.  The
> original array will get divvied up into smaller and smaller
> slices of an earlier slice (the remaining can be discarded).



  reply	other threads:[~2001-10-26  5:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-26  4:46 Arrays and Access types Eric G. Miller
2001-10-26  5:39 ` James Rogers [this message]
2001-10-26  6:45   ` Eric G. Miller
2001-10-26 16:40     ` James Rogers
2001-10-26  6:14 ` tmoran
2001-10-26 14:26 ` Ted Dennison
2001-10-26 19:31 ` chris.danx
2001-10-26 23:32 ` Jeffrey Carter
2001-10-27  1:08 ` Eric G. Miller
2001-10-27  2:09   ` DuckE
2001-10-27  4:23     ` Steven Deller
2001-10-27 18:30       ` Eric G. Miller
replies disabled

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