comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <Nick.Roberts@dial.pipex.com>
Subject: Re: Array index ?
Date: 1998/01/31
Date: 1998-01-31T00:00:00+00:00	[thread overview]
Message-ID: <01bd2e90$af2ef360$LocalHost@xhv46.dial.pipex.com> (raw)
In-Reply-To: mheaney-ya023680002901982253280001@news.ni.net


One simple scheme might be to do a simple conversion from the string
T_Index to a discrete type, an integer type T_Int_Index say.  Supposing the
string will only ever contain the digits 0 to 9, the following function
would do the conversion:

   type T_Int_Index is Integer range 0..9999;

   function To_Int(S: T_Index) return T_Int_Index is
   begin
      return S(1)*1000 + S(2)*100 + S(3)*10 + S(4);
   end;

The array could then be declared as:

   type T is array(T_Int_Index) of R;

then an array, such as

   Stock: T;

could be accessed by an index N like this:

   Q1 := Q1 + Stock(To_Int(N)).Quant_In;

This is a simple example of 'hashing'.  The disadvantage of this simple
system may be that the array, which has 10000 elements, may be much too big
(in that most of it remains unsed).  If this is the case, you might be able
to get around the problem with a more complicated 'hashing index' -- the
function defined above.  Have fun!

-- 

== Nick Roberts ================================================
== Croydon, UK                       ===========================
==                                              ================
== Proprietor, ThoughtWing Software                   ==========
== Independent Software Development Consultant            ======
== Nick.Roberts@dial.pipex.com                              ====
== Voicemail & Fax +44 181-405 1124                          ===
==                                                            ==
==           I live not in myself, but I become               ==
===          Portion of that around me; and to me             ==
====         High mountains are a feeling, but the hum        ==
=======      Of human cities torture.
===========                             -- Byron [Childe Harold]


Matthew Heaney <mheaney@ni.net> wrote in article
<mheaney-ya023680002901982253280001@news.ni.net>...
> Do you mean you want an array whose index subtype is a subtype of String,
as in
> 
> subtype T_Index is String (1 .. 4);
> 
> type T is array (T_Index) of R;
> 
> If so, you can't do that, because arrays can only have a discrete subtype
> (integer or enumeration type) as the index subtype.  
> 
> You probably want a more abstract data structure, such as a dictionary
> (sort of like a symbol table - consult you fave compiler book) or a
> trie-structure (not a misspell) or a hash table, etc.





      reply	other threads:[~1998-01-31  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-01-29  0:00 Array index ? SiliconJesus
1998-01-29  0:00 ` Matthew Heaney
1998-01-31  0:00   ` Nick Roberts [this message]
replies disabled

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