comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthewjheaney@earthlink.net>
Subject: Re: Indexed list
Date: Fri, 15 Aug 2003 23:57:02 GMT
Date: 2003-08-15T23:57:02+00:00	[thread overview]
Message-ID: <ire%a.4699$Nf3.1708@newsread4.news.pas.earthlink.net> (raw)
In-Reply-To: 3922594d.0308150346.65657e94@posting.google.com


"Alex Crivat" <axu@rdsnet.ro> wrote in message
news:3922594d.0308150346.65657e94@posting.google.com...
> Say I have a generic package wich implements a doubly linked list.
> What I want to do, and I could not find a way to do it, is to use this
> list as an array with the posibility of using the index operator "()"
> (I know, in ada this is not used as an operator).
> For example in C++ language I can do this by overloading the "[]"
> operator. So when I call the list with this operator and a interer
> index as parameter it returns the element coresponding to that index.
>
> Example:
> A : ITEM;
> L : DLIST; -- DLIST is a list of ITEM type elements;
>
> And I want to be able to do something like:
> A := L(3); -- wich will put in A the fourth element of my list.
>
> Is there a way of doing this in Ada using operators or should I use a
> custom function;

You can't really use an operator.  You can do it this way in the latest
version of Charles:

procedure Op (L : List_Subtype) is
   I : Iterator_Type := Succ (First (L), Offset => 3);
   E : Element_Subtype := Element (I);
begin
   ...
end;

Of course, as with your example, this implies a linear search.  Another
option for you would be to use a map, instantiated with subtype Natural as
the key subtype.  In fact the hashed maps in Charles are implemented using a
doubly-linked list, allowing you do this:

procedure Op (M : Map_Subtype) is
   E : Element_Type := Element (M, Key => 3);
begin

This doesn't use an operator, but the syntax isn't that much different.

Hash table searches have constant time complexity of average, so lookups are
fast.  You could also use a sorted map, which has logarithmic time
complexity.

Depending on your requirements, you might also consider the vector and deque
containers, too.  Those containers support random access directly.

Charles has both doubly- and singly-linked list containers.

I just released a new version of Charles a couple of days ago:

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

URL:http://home.earthlink.net/~matthewjheaney/charles/charles-20030813.zip

-Matt






      parent reply	other threads:[~2003-08-15 23:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-15 11:46 Indexed list Alex Crivat
2003-08-15 13:01 ` Lutz Donnerhacke
2003-08-15 23:57 ` Matthew Heaney [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