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!news3.google.com!proxad.net!feeder1-2.proxad.net!news.tiscali.fr!club-internet.fr!feedme-small.clubint.net!feeder.news-service.com!feeder6.cambrium.nl!feeder3.cambrium.nl!feed.tweaknews.nl!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: OO Style with Ada Containers 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> Date: Thu, 15 Nov 2007 10:36:54 +0100 Message-ID: <87oddv3l55.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:NhfjZjUttyku2F6JT59js2Bd3/0= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tele2 X-Trace: DXC=59F6nPXLTi`oPfY8k93I=f6`Y6aWje^Yj1QHd=aeT6Jfm@[o=d]MiOm8e[W?MA4bXi_O;WIh2Y:7j Xref: g2news1.google.com comp.lang.ada:18415 Date: 2007-11-15T10:36:54+01:00 List-Id: braver writes: > my sample application [...] gets a Vector of tokens and inserts them > into an Ordered_Map to count word occurrences: I don't think you need to store all the tokens. All you need is a Hashed_Set (which stores objects without duplication). I would do something like type Word_Counter is Word : Ada.Strings.Unbounded.Unbounded_String; Count : Natural; end Word_Counter; function Equivalent (L, R: Word_Counter) is use type Ada.Strings.Unbounded.Unbounded_String; begin return L.Word = R.Word; -- ignore Count in the comparison end Equivalent; function Hash (Element : Word_Counter) is begin return Ada.Strings.Unbounded.Hash (Element.Word); end Hash; package Word_Counter_Hashed_Sets is new Ada.Containers.Hadhed_Sets (Element_Type => Word_Counter, Hash => Hash, Equivalent_Elements => Equivalent); Counters : Word_Counter_Hashed_Sets.Set; Current_Word : Ada.Strings.Unbounded.Unbounded_String; Position : Word_Counter_Hashed_Sets.Cursor; use type Word_Counter_Hashed_Sets.Cursor; begin ... 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; ... end; (does [1] duplicate E.Word each time we increment the counter? If so, consider using Ada.Containers.Hashed_Sets.Generic_Keys.Update_Element_Preserving_Keys). > -- would need to import "=" for private type WC.Cursor: > -- if Ngram_Cursor = WC.No_Element declare use type WC.Cursor; begin if Ngram_Cursor = WC.No_Element then > I'd appreciate any improvements to the above, which I deduced from ARM HTH -- Ludovic Brenta.