comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Re: Unconstrained Arrays
Date: Tue, 17 Mar 2009 20:14:29 GMT
Date: 2009-03-17T20:14:29+00:00	[thread overview]
Message-ID: <FwTvl.454056$Mh5.370218@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: 1a8008fb-c840-45bc-824c-d10eec9fe569@d36g2000prf.googlegroups.com

Most people just use pointer to a string. Then they must write a set of
routines to handle the pointers

But Ada 95 and Ada 2005 contains package called 
   "Ada.Strings.Unbounded"
which was designed to handle this problem.

Instead of using type "String" you will use "Unbounded_String".



--  For a simplier design:
--
--  From "Ada Problem Solving and Program Design", 1992
--
--  Program 10.9 : Specification Page 504
--  Program 10.10: Gives the Body on Page 508 (not posted)
--
--  Modified for Ada 95. Some comments have been removed
--  easy to adapt for generic package.
--
with Ada.Text_IO ;
package VString is

  -- Specification for ADT to handle string variable 1-80 characyer

  MaxLength : constant Integer := 80 ;
  subtype Index is Integer range 0..MaxLength ;

  type VString is Private ;

  -- exceptions

  StringOverflow  : exception ;
  EmptyString     : exception ;
  InvalidArgument : exception ;

  -- operators

  function MakeVString (S : String) return VString ; 
  function MakeVString (C : Character) return VString ; 
  function EmptyVString return VString ; 

  -- selectors

  function Length (S : VString) return Index ; 
  function Value  (S : VString) return String ; 
  function Head   (S : VString) return Character ; 
  
  -- inquiry

  function IsEmpty (S : VString) return Boolean ; 

  -- concattenation

  function "&" (S1, S2 : VString) return VString ; 
  function "&" (S1 : VString; C : Character) return VString ; 
  function "&" (C : Character; S1 : VString) return VString ; 
  function "&" (S1 : VString; S : String) return VString ; 
  function "&" (S : String; S1 : VString) return VString ; 

  -- lexical compariason

  function "<" (S1, S2 : VString) return Boolean ; 
  function "<=" (S1, S2 : VString) return Boolean ; 
  function ">" (S1, S2 : VString) return Boolean ; 
  function ">=" (S1, S2 : VString) return Boolean ; 

 -- search

  function locate (Sub : VString; within : VString) return Index ; 
  function locate (Sub : String;  within : VString) return Index ; 
  function locate (C : Character;  within : VString) return Index ; 


  function Tail  (S : String) return VString ; 

  function substring (S : VString; start, size : Index) return VString ; 

  -- I/O 

  procedure Get_Line ( Item : out VString ) ;
  procedure Put ( Item : VString ) ;

  procedure Get_Line ( File : Ada.Text_IO.File_Type;
                       Item : out VString ) ;
  procedure Put ( File : Ada.Text_IO.File_Type;
                  Item : VString ) ;

private

  type VString is record
                    CurrentLength : Index := 0 ;
                    StringPart : String ( 1 .. MaxLength ) := 
                                              ( others => Ascii.Nul ) ;
                  end record ;  
end VString ;













In <1a8008fb-c840-45bc-824c-d10eec9fe569@d36g2000prf.googlegroups.com>, belteshazzar <gbelteshazzar@gmail.com> writes:
>I have a program that uses a lot of different sized arrays and passed
>into math functions. So I'm using an unconstrained array type. The
>problem that I have is that I'm not allowed to use "new" this means
>that I have to use pools (which I can't because to intantiate a pool I
>have to constrain the array type) or allocated the arrays on the
>stack. Allocating the arrays on the stack is fine, BUT it means that i
>have to use array initializers so that they remain unconstrained
>rather than becoming an anonomous contrained type that can no longer
>be passed to my math functions.
>
>We have now noticed that because of the size of our arrays that the
>array intialisation is causing significant performance issues. Thus,
>we need a new way of handling our arrays.
>
>The best option seems to be to turn the functions that take arrays
>into generic functions and instantiate them on the fly according to
>the required size. Does anyone know if there are performance issues
>with dynamically creating instantiations of generic functions all over
>teh place?
>
>Also, and the main point of my post, I've found that I can place the
>unconstrained array inside a record with a distriminant and this seems
>to solve all our problems. We don't have to use array initialisers and
>we can get pointers to aliased objects that can be easily passed to
>the math functions.
>
>Has anyone come across these sorts of issues? I'm curious as to the
>solution that others have found, and why Ada seems to handle
>unconstrained arrays differently to unconstrained arrays inside a
>record.




  parent reply	other threads:[~2009-03-17 20:14 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-17  0:59 Unconstrained Arrays belteshazzar
2009-03-17  1:49 ` Jeffrey R. Carter
2009-03-17  2:58   ` belteshazzar
2009-03-17  4:15     ` Jeffrey Creem
2009-03-17  5:20     ` Jeffrey R. Carter
2009-03-17 17:56       ` Jeffrey R. Carter
2009-03-17 23:10         ` belteshazzar
2009-03-18 18:31           ` Jeffrey R. Carter
2009-03-20  1:53           ` Peter C. Chapin
2009-03-20  6:45             ` sjw
2009-03-20  9:46               ` Jacob Sparre Andersen
2009-03-20 11:40               ` Jean-Pierre Rosen
2009-03-25 21:11                 ` sjw
2009-03-25 22:30                   ` Robert A Duff
2009-03-25 23:28                     ` Randy Brukardt
2009-03-26  0:03                       ` Jeffrey R. Carter
2009-03-26  1:00                         ` Robert A Duff
2009-03-20 12:15               ` christoph.grein
2009-03-20 15:45               ` Adam Beneschan
2009-03-23  8:26                 ` belteshazzar
2009-03-25 21:21                 ` sjw
2009-03-25 22:03                   ` Adam Beneschan
2009-03-26  1:32                     ` tmoran
2009-03-27  8:39                   ` Jean-Pierre Rosen
2009-03-27 20:07                     ` sjw
2009-03-29 16:24                     ` sjw
2009-03-27 11:57                   ` Gautier
2009-03-17 15:33     ` Adam Beneschan
2009-03-17 23:00       ` belteshazzar
2009-03-17 20:14 ` anon [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-12-11 17:17 Unconstrained arrays Michael Unverzagt
2001-12-11 18:22 ` Stephen Leake
2001-12-11 18:24 ` Mark Lundquist
1993-08-15  5:01 Alex Blakemore
1993-08-13 21:08 J. Craig Heberle
1993-08-13 12:34 Paul Durbin
1993-08-12 21:23 Robert Dewar
1993-08-12 19:25 Wes Groleau x1240 C73-8
1993-08-12 17:27 agate!howland.reston.ans.net!math.ohio-state.edu!magnus.acs.ohio-state.ed
1993-08-12 16:26 Mark A Biggar
1993-08-12 16:00 Dave Collar d x7468
1993-08-12 15:28 Robert I. Eachus
1993-08-12 15:00 Robert Dewar
1993-08-12 13:03 Raymond Blaak
1993-08-12 12:14 cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!howland.
1993-08-12 12:03 cis.ohio-state.edu!pacific.mps.ohio-state.edu!math.ohio-state.edu!magnus.
1993-08-11 23:42 Kenneth Anderson
1993-08-11 23:40 cis.ohio-state.edu!math.ohio-state.edu!cs.utexas.edu!swrinde!menudo.uh.ed
1993-08-11 22:29 Kenneth Anderson
replies disabled

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