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-Thread: 103376,a3736685ef876ab2 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!out03a.usenetserver.com!news.usenetserver.com!in01.usenetserver.com!news.usenetserver.com!news-in-02.newsfeed.easynews.com!easynews.com!easynews!sn-xt-sjc-03!sn-xt-sjc-01!sn-post-sjc-01!supernews.com!corp.supernews.com!not-for-mail From: Matthew Heaney Newsgroups: comp.lang.ada Subject: Re: OO Style with Ada Containers Date: Mon, 19 Nov 2007 02:36:21 +0000 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <1195082906.420079.195000@d55g2000hsg.googlegroups.com> <1195084214.480299.13970@t8g2000prg.googlegroups.com> <1195084752.840598.174460@v65g2000hsc.googlegroups.com> <1195086265.070953.93180@d55g2000hsg.googlegroups.com> <87oddv3l55.fsf@ludovic-brenta.org> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (windows-nt) Cancel-Lock: sha1:M7te9C27UmUwfS1g7jHqYZc5ox0= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@supernews.com Xref: g2news1.google.com comp.lang.ada:18495 Date: 2007-11-19T02:36:21+00:00 List-Id: Ludovic Brenta writes: Instead of this: type Word_Counter is Word : Ada.Strings.Unbounded.Unbounded_String; Count : Natural; end Word_Counter; I would have declared it something like this: type Word_Counter (Length : Positive) is record Word : String (1 .. Length); Count : Natural; end record; and then used the indefinite form of the (hashed) set. > Current_Word := Get_Next_Word; > Position := Counters.Find (Current_Word); > if Position = Word_Counter_Hashed_Sets.No_Element then > Counters.Insert (New_Item => (Word => Current_Word, Count => 1)); > else > declare > E : constant Word_Counter := Counters.Element (Position); > begin > Counters.Replace_Element > (Position => Position, > New_Item => (Word => E.Word, -- [1] > Counter => E.Counter + 1)); > end; > end if; Right, but this suffers from the same problem as in the original example (Find duplicates the work done by Insert). > (does [1] duplicate E.Word each time we increment the counter? Yes, because this replaces the entire element; the old element will get destroyed, and a new element created. > If so, > consider using > Ada.Containers.Hashed_Sets.Generic_Keys.Update_Element_Preserving_Keys). Well, almost. That does indeed allow you to modify an element in-place, but it must copy the key in order to verify that you didn't modify it, so it doesn't really solve the problem.