From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4f316de357ae35e9 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-08-05 02:57:00 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!195.27.83.146!news-FFM2.ecrc.net!news.iks-jena.de!lutz From: lutz@iks-jena.de (Lutz Donnerhacke) Newsgroups: comp.lang.ada Subject: Re: FAQ and string functions Date: Mon, 5 Aug 2002 09:56:58 +0000 (UTC) Organization: IKS GmbH Jena Message-ID: References: <20020730093206.A8550@videoproject.kiev.ua> <4519e058.0207300548.15eeb65c@posting.google.com> <20020731104643.C1083@videoproject.kiev.ua> <4519e058.0208010629.5e6182ca@posting.google.com> <20020801194720.Q1080@videoproject.kiev.ua> <4519e058.0208020605.5ab7e092@posting.google.com> <3D4AAF63.72782659@san.rr.com> <3D4B2382.7030209@telepath.com> <3D4B2ACD.FDA29B9A@san.rr.com> <3D4B401E.3060802@telepath.com> <3D4B4477.500088B@san.rr.com> NNTP-Posting-Host: taranis.iks-jena.de X-Trace: branwen.iks-jena.de 1028541418 22164 217.17.192.37 (5 Aug 2002 09:56:58 GMT) X-Complaints-To: usenet@iks-jena.de NNTP-Posting-Date: Mon, 5 Aug 2002 09:56:58 +0000 (UTC) User-Agent: slrn/0.9.6.3 (Linux) Xref: archiver1.google.com comp.lang.ada:27690 Date: 2002-08-05T09:56:58+00:00 List-Id: * 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; --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; --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; --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;