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,db9fec59d36b64c1 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-08-15 16:57:02 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread4.news.pas.earthlink.net.POSTED!not-for-mail From: "Matthew Heaney" Newsgroups: comp.lang.ada References: <3922594d.0308150346.65657e94@posting.google.com> Subject: Re: Indexed list X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: Date: Fri, 15 Aug 2003 23:57:02 GMT NNTP-Posting-Host: 65.110.133.134 X-Complaints-To: abuse@earthlink.net X-Trace: newsread4.news.pas.earthlink.net 1060991822 65.110.133.134 (Fri, 15 Aug 2003 16:57:02 PDT) NNTP-Posting-Date: Fri, 15 Aug 2003 16:57:02 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.lang.ada:41543 Date: 2003-08-15T23:57:02+00:00 List-Id: "Alex Crivat" 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