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,ce0900b60ca3f616 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-11-09 20:39:37 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!ppp-1-20.cvx1.telinco.NET!not-for-mail From: "Nick Roberts" Newsgroups: comp.lang.ada Subject: Re: List container strawman Date: Sat, 10 Nov 2001 01:48:57 -0000 Message-ID: <9sib27$13aeg3$5@ID-25716.news.dfncis.de> References: <3BE29AF4.80804@telepath.com> <3BE29BD4.10401@telepath.com> <3BE2DB99.B707D409@boeing.com> <3BE32A18.18404AD1@boeing.com> <3BE443DE.574D669C@acm.org> <3BE58FDD.E1FB1815@san.rr.com> <3bec1cbe$0$15824$626a54ce@news.free.fr> NNTP-Posting-Host: ppp-1-20.cvx1.telinco.net (212.1.136.20) X-Trace: fu-berlin.de 1005367176 37042691 212.1.136.20 (16 [25716]) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Xref: archiver1.google.com comp.lang.ada:16200 Date: 2001-11-10T01:48:57+00:00 List-Id: I'm sorry, I'm throw one of classic spanners in the works here, but I'm afraid I think you've got your approach horribly horribly wrong! For a start, all this mularchy about insertion and deletion is just plain silly! It really is. You don't need them, and shouldn't implement them. Yes, really. Let me show you why. Suppose you want to have a list of names (strings), and you want to normalise them as follows: (a) convert them all to uppercase; (b) all beginning with "MACxy", where x is a letter, to have "MCxy" inserted afterwards; (c) all beginning with "?" to be deleted. I'm going to frame the answer in terms of my own proposal, please forgive me for that. The essential answer is that you don't keep a list of the strings, but you keep a list of access values to them. You then simply build a new list (of access values), and then delete the old one: with Ada.Characters.Handling; use Ada.Characters.Handling; ... type Name_Ref is access [all] String; package Name_Ref_Iteration is new Iteration(Name_Ref); procedure Normalize_Names ( From: in out Name_Ref_Iteration.Sequence_Recorder'Class; Into: Name_Ref_Iteration.Sequence_Recorder'Class) is Ref: Name_Ref; N: Natural; begin Rewrite(Into); -- possibly redundant (possibly poor design, actually) Restart(From); -- ditto while not End_of_Data(From) loop Read(From,Ref); if Ref /= null and then Ref.all'Length > 0 and then Ref(Ref.all'First) /= '?' then To_Upper(Ref.all); Write(Into,Ref); N := Ref.all.First-1; -- in case string doesn't start at 1 if Ref.all'Length >= 5 and then Ref(N+1..N+3) = "MAC" and then Is_Letter(Ref(N+4)) then Write(Into, new String'("MC"&Ref(N+4..Ref.all'Last))); end if; else Ref := null; -- or maybe use Unchecked_Deallocation end if; end loop; Rewrite(From); -- erases it (again, possibly wrong here) Restart(Into); -- again possibly redundant end Normalize_Names; Note how this procedure can be passed parameters of ANY 'sequence recorder' container type (instantiated on Name_Ref), not just lists. Supposing we wanted to use a list container type: package Name_Ref_Lists is new xyz.Lists(Name_Ref_Iteration); subtype Name_List is Name_Ref_Lists.List_Type; L1, L2: Name_List; ... set up L1 with names Normalize_Names(L1,L2); A note about the Restarts and Rewrites: they are probably better left outside procedures such as this, in fact. I have quietly deleted oddities like null pointers: a real program should probably raise exceptions. Trust me, guys, this approach is majorly better in every way (easier to use, easier to read & understand, easier to implement, more memory efficient, more speed efficient) than messing around with inserts and deletes, for the vast majority of situations. I know I can seem pompous, overbearing, etc., etc., especially about this subject, but it pains me to see you going so far astray, and spending so much time arguing about features that you don't need anyway! It also gives me another opportunity to demonstrate the idea of having a set of abstract container types, upon which operations (such as Normalize_Names) can be hung, thus freeing you of: (1) having to worry about which container type to use when writing the procedure; (2) the procedure shackling you to one particular container type. Do you turn away from this Utopia? ;-) -- Best wishes, Nick Roberts