comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: phone number database
Date: 27 Feb 2003 16:07:16 -0800
Date: 2003-02-28T00:07:16+00:00	[thread overview]
Message-ID: <1ec946d1.0302271607.607503a1@posting.google.com> (raw)
In-Reply-To: uk7fliqhx.fsf_-_@nasa.gov

Stephen Leake <Stephen.A.Leake@nasa.gov> wrote in message news:<uk7fliqhx.fsf_-_@nasa.gov>...
> "Anders Wirzenius" <anders.wirzenius@pp.qnet.fi> writes:
> 
> > I HAVE:
> > 1.
> > Six phone numbers: 1795, 2006, 2007, 2012, 2013, 2014.
> > 2.
> > A log (text file) from the company's phone system with data like:
> > From_Phone_Nr, To_Phone_Nr, Answering_Time, Duration... 
> > 
> > I WANT TO:
> > 3.
> > Pick only those log data where the To_Phone_Nr is one of the six numbers.
> > 4.
> > Set up some statistics about those phone calls.
> > 5.
> > Be able to add or remove phone numbers from the list (six becomes
> > seven some sunny winterday). 
> > 
> > I WISH I HAD:
> > type Help_Desk is (1795, 2006, 2007, 2012, 2013, 2014); -- with the
> > dynamics described under point 5. 
> 
> > What is a proper way to implement "To_Phone_Nr in Help_Desk" ? 
> 
> Use a SAL binary tree
> (http://users.erols.com/leakstan/Stephe/Ada/Sal_Packages/index.htm) to
> store the help desk phone numbers; then do
[ex snipped]

If you just want a membership test, you can always use a set:

package Phone_Number_Type is
   new Charles.Sets.Sorted.Unbounded (Phone_Number_Type);

Set : Phone_Number_Types.Container_Type;

if Find (Set, Phone_Number) /= Back (Set) then ...

If you need to store some data with each number, you can could still
use a set:

type Info_Type is
   record
      Phone_Number : Phone_Number_Type;
      ...
   end record;

function "<" (L, R : Info_Type) return Boolean is
begin
   return L.Phone_Number < R.Phone_Number;
end;

package Phone_Number_Info is
   new Charles.Sets.Sorted.Unbounded (Info_Type, "<");

Set : Phone_Number_Info.Container_Type;

procedure Op (X : Phone_Number_Type) is
   I : constant Iterator_Type := Find (Set, X);
begin
   if I /= Back (Set) then
      declare
         Info : Info_Type renames To_Access (I).all;
      begin
         --manipulate Info as necessary
      end;
   end if;
end Op;

You could also use a map, which stores the phone number separately
from the info:

package Phone_Number_Info is
   new Charles.Maps.Sorted.Unbounded
     (Phone_Number_Type, 
      Info_Type); --extra info only, sans phone number

Map : Phone_Number_Info.Container_Type;

procedure Op (X : Phone_Number_Type) is
   I : constant Iterator_Type := Find (Map, X);
begin
   if I /= Back (Map) then
     declare
        Info : Info_Type renames To_Access (I).all;
     begin
        --manipulate Info as necessary
     end;
   end if;
end Op;

To add another phone number, just insert it into the set or map.  For
example:

procedure Add 
  (X : Phone_Number_Type;
   Y : Info_Type) is

   I       : Iterator_Type;
   Success : Boolean;
begin
   Insert (Map, X, Y, I, Success);

   if not Success then
      --X already in map
   end if;
end Add;

You could also use the hashed versions, and use the phone number
itself as the value of the hash function for the phone number key. 
Then your lookups would only have O(1) average time complexity.

The hashed associative containers are nice because you can preallocate
the actual hash table:

Map : Hashed_Map_Subtype;

Resize (Map, Size => 6);

That guarantees there's no hash table allocation until the load factor
exceeds 1.

http://home.earthlink.net/~matthewjheaney/charles/

Drop me a line if you have any questions about Charles.

Matt



  parent reply	other threads:[~2003-02-28  0:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-26 18:00 Variant Record Component John Harbaugh
2003-02-26 19:08 ` Stephen Leake
2003-02-27  8:17   ` Anders Wirzenius
2003-02-27  8:46     ` John McCabe
2003-02-27 17:26     ` phone number database Stephen Leake
2003-02-27 18:09       ` tmoran
2003-02-28  0:07       ` Matthew Heaney [this message]
2003-02-28  6:46       ` Hijacking threads (was phone number database (was Variant Record Component)) Anders Wirzenius
2003-02-26 20:50 ` Variant Record Component David C. Hoos
2003-02-28 16:15   ` John Harbaugh
2003-02-28 18:18     ` tmoran
2003-02-28 22:07       ` John Harbaugh
2003-02-28 20:51     ` Randy Brukardt
2003-03-01  2:34     ` Jeffrey Carter
2003-03-03  9:24     ` John McCabe
2003-02-26 21:37 ` 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