comp.lang.ada
 help / color / mirror / Atom feed
From: lutz@iks-jena.de (Lutz Donnerhacke)
Subject: Re: FAQ and string functions
Date: Mon, 5 Aug 2002 09:56:58 +0000 (UTC)
Date: 2002-08-05T09:56:58+00:00	[thread overview]
Message-ID: <slrnaksiv8.oo.lutz@taranis.iks-jena.de> (raw)
In-Reply-To: 3D4B4477.500088B@san.rr.com

* Darren New wrote:
>Tell me how you declare a variable for an array whose bounds you don't know
>until after you're past the declaration? Tell me how you add more elements
>to the end of an array?
>
>You can't do something like
>  X := X & Y
>as far as *I* understand.

--test_unbound.ads--
with Unbound;

package Test_Unbound is
   type Int is range 0 .. 1000;
   type Ind is range 1 .. 20;
   package UI is new Unbound (Int, Ind);
   subtype Int_Array is UI.Unbounded_Array;

   procedure Test;
end Test_Unbound;
\f
--test_unbound.adb--

package body Test_Unbound is
   procedure Test is
      use UI;
      a, b : Int_Array;
   begin
      a := To_Unbounded ((1, 2, 3, 4));
      b := To_Unbounded ((5, 6, 7, 8));
      a := a & b;
      Add_Back (b, To_Unbounded ((9, 10)));
   end Test;
end Test_Unbound;
\f
--unbound.ads--
with Ada.Finalization;

generic
   type Item is private;
   type Index is range <>;
package Unbound is
   type Unbounded_Array is private;
   Empty_Array : constant Unbounded_Array;      
   procedure Swap (a, b : in out Unbounded_Array);      
   function "&" (a, b : Unbounded_Array) return Unbounded_Array;
   procedure Add_Back (a : in out Unbounded_Array; b : Unbounded_Array);
   type Bounded_Array is array (Index range <>) of Item;
   function To_Unbounded (a : Bounded_Array) return Unbounded_Array;
   function To_Bounded (a : Unbounded_Array) return Bounded_Array;
private
   type Array_Access is access Bounded_Array;
   type Unbounded_Array is new Ada.Finalization.Controlled with record
      array_p : array_access := null;
   end record;
   procedure Initialize (a : in out Unbounded_Array);
   procedure Adjust     (a : in out Unbounded_Array);
   procedure Finalize   (a : in out Unbounded_Array);
   Empty_Array : constant Unbounded_Array :=
     (Ada.Finalization.Controlled with array_p => null);
end Unbound;
\f
--unbound.adb--
with Unchecked_Deallocation;

package body Unbound is
   ----------
   -- Swap --
   ----------
   
   procedure Swap (a, b : in out Unbounded_Array) is
      temp : constant Array_Access := a.array_p;
   begin
      a.array_p := b.array_p;
      b.array_p := temp;
   end Swap;

   ---------
   -- "&" --
   ---------

   function "&" (a, b : Unbounded_Array) return Unbounded_Array is
      res : Unbounded_Array;
   begin
      if a.array_p = null then
         if b.array_p /= null then
            res.array_p := new Bounded_Array'(b.array_p.all);
         end if;
      else
         if b.array_p /= null then
            res.array_p := new Bounded_Array'(a.array_p.all & b.array_p.all);
         else
            res.array_p := new Bounded_Array'(a.array_p.all);
         end if;
      end if;
      return res;
   end "&";

   --------------
   -- Add_Back --
   --------------

   procedure Add_Back (a : in out Unbounded_Array; b : Unbounded_Array) is
      new_a : Unbounded_Array := a & b;
   begin
      Swap (a, new_a);
   end Add_Back;

   ------------
   -- Adjust --
   ------------

   procedure Adjust (a : in out Unbounded_Array) is
      new_a : Unbounded_Array := a & Empty_Array;
   begin
      Swap (a, new_a);
      new_a.array_p := null;
   end Adjust;

   --------------
   -- Finalize --
   --------------

   procedure Finalize (a : in out Unbounded_Array) is
      procedure Free is
         new Unchecked_Deallocation (Bounded_Array, Array_Access);
   begin
      if a.array_p /= null then
         free (a.array_p);
      end if;
   end Finalize;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (a : in out Unbounded_Array) is
   begin
      null;
   end Initialize;

   ------------------
   -- To_Unbounded --
   ------------------
   function To_Unbounded (a : Bounded_Array) return Unbounded_Array is
   begin
      return (Ada.Finalization.Controlled with
        array_p => new Bounded_Array'(a));
   end To_Unbounded;

   ----------------
   -- To_Bounded --
   ----------------
   function To_Bounded (a : Unbounded_Array) return Bounded_Array is
   begin
      return a.array_p.all;
   end To_Bounded;
end Unbound;



  parent reply	other threads:[~2002-08-05  9:56 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-30  6:32 FAQ and string functions Oleg Goodyckov
2002-07-30  8:52 ` Colin Paul Gloster
2002-07-30 13:48 ` Ted Dennison
2002-07-31  4:52   ` Brian May
2002-08-01 16:09     ` Ted Dennison
2002-08-02  0:21       ` Brian May
2002-08-02  1:56         ` tmoran
2002-08-02 13:59         ` Ted Dennison
2002-07-31  7:46   ` Oleg Goodyckov
2002-07-31  9:04     ` Lutz Donnerhacke
2002-07-31  9:39       ` Pascal Obry
2002-07-31 15:06         ` Oleg Goodyckov
2002-07-31 16:50       ` Oleg Goodyckov
2002-07-31 20:16     ` Simon Wright
2002-07-31 20:56       ` Robert A Duff
2002-08-01  0:11         ` Darren New
2002-08-01  1:08           ` tmoran
2002-08-01  9:25           ` Brian May
2002-08-01 11:20           ` Oleg Goodyckov
2002-08-01 15:43             ` Darren New
2002-08-01 21:37               ` Robert A Duff
2002-08-03  0:42                 ` Ted Dennison
2002-08-03 13:51                   ` Robert A Duff
2002-08-03 16:43                   ` Darren New
2002-08-05 13:37                   ` Stephen Leake
2002-08-02  8:01               ` Oleg Goodyckov
2002-08-02 16:09                 ` Darren New
2002-08-01 11:09         ` Oleg Goodyckov
2002-08-01 14:08           ` Frank J. Lhota
2002-08-01 15:06             ` Robert A Duff
2002-08-01 16:05             ` Oleg Goodyckov
2002-08-01 14:57         ` Georg Bauhaus
2002-07-31 22:04     ` Dmitry A.Kazakov
2002-07-31 15:23       ` Oleg Goodyckov
2002-08-01 21:57         ` Dmitry A.Kazakov
2002-08-01 13:10           ` Oleg Goodyckov
2002-08-02 23:29             ` Dmitry A.Kazakov
2002-08-02 16:35               ` Oleg Goodyckov
2002-08-05 11:50                 ` Dmitry A. Kazakov
2002-08-05 14:29                   ` Larry Kilgallen
2002-08-05 14:57                     ` Dmitry A. Kazakov
2002-08-05 15:12                   ` Oleg Goodyckov
2002-08-05 16:20                   ` Darren New
2002-08-05 17:01                     ` Georg Bauhaus
2002-08-05 17:48                       ` Darren New
2002-08-05 19:06                         ` tmoran
2002-08-05 20:08                           ` Darren New
     [not found]                     ` <slrnakv3q9.p2.lutz@taranis.iks-jena.de>
     [not found]                       ` <3D4FEFCB.3B74F5E5@san.rr.com>
2002-08-14  0:07                         ` Randy Brukardt
2002-08-01 14:29     ` Ted Dennison
2002-08-01 16:47       ` Oleg Goodyckov
2002-08-02 14:05         ` Ted Dennison
2002-08-02 16:11           ` Darren New
2002-08-03  0:30             ` Ted Dennison
2002-08-03  0:58               ` Darren New
2002-08-03  2:04                 ` Dale Stanbrough
2002-08-03  2:32                 ` Ted Dennison
2002-08-03  2:47                   ` Darren New
2002-08-03 12:41                     ` Ted Dennison
2002-08-03 16:53                       ` Darren New
2002-08-04  1:08                         ` Ted Dennison
2002-08-04 16:23                           ` Darren New
2002-08-05  2:16                             ` Robert Dewar
2002-08-05  3:45                               ` Darren New
2002-08-05  9:56                     ` Lutz Donnerhacke [this message]
2002-08-05 16:02                       ` Darren New
2002-08-14  0:42                         ` Randy Brukardt
2002-08-14  1:45                           ` Darren New
2002-08-14 19:37                             ` Randy Brukardt
2002-08-14 20:25                               ` Stephen Leake
2002-08-14 20:22                           ` Stephen Leake
2002-08-15 19:24                             ` Randy Brukardt
     [not found]                         ` <jb1vkustkugeutalhvrhv1n0k9hqn2fpip@4ax.com>
     [not found]                           ` <3D4FF351.8F4A6C0A@san.rr.com>
2002-08-14  1:03                             ` Randy Brukardt
2002-08-14  1:05                       ` Robert A Duff
     [not found]                       ` <3D4EA1AC.80D17170@s <wccofc6b66u.fsf@shell01.TheWorld.com>
2002-08-14 20:29                         ` Stephen Leake
2002-08-26 17:53                           ` Robert A Duff
2002-08-26 18:40                             ` Chad R. Meiners
2002-08-26 18:52                               ` Robert A Duff
2002-08-26 21:46                                 ` Chad R. Meiners
2002-08-05 13:29                     ` Stephen Leake
2002-08-03  5:07                   ` achrist
2002-08-03 12:52                     ` Ted Dennison
2002-08-05 15:34                       ` Ted Dennison
2002-08-05 13:24                 ` Stephen Leake
2002-08-05 16:02                   ` Darren New
2002-08-05  7:18           ` Oleg Goodyckov
2002-08-02  1:04     ` tmoran
replies disabled

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