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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED.8dY8omnix++EB5/QBRk4Sw.user.gioia.aioe.org!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Tally Date: Wed, 15 Jan 2020 11:52:56 +0000 Organization: Aioe.org NNTP Server Message-ID: References: <3c6f1486-293d-4eb5-a379-279b108248d8@googlegroups.com> NNTP-Posting-Host: 8dY8omnix++EB5/QBRk4Sw.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain X-Complaints-To: abuse@aioe.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (darwin) X-Notice: Filtered by postfilter v. 0.9.2 Cancel-Lock: sha1:hFQwivKItROYIzNQVGbm/9vd5c0= Xref: reader01.eternal-september.org comp.lang.ada:57846 Date: 2020-01-15T11:52:56+00:00 List-Id: Simon Wright writes: > The Booch Components included Bags, which would solve this directly! > (not that I'd recommend anyone to use the BCs for a new project) This is what it'd look like (it's a lot uglier in Ada 95, because the containers and iterators, while tagged, can't use dotted notation). Sorry about the silly Hash function. with BC.Containers.Bags.Unmanaged; with Ada.Text_IO; use Ada.Text_IO; procedure Main is package Abstract_Containers is new BC.Containers (Natural); package Abstract_Bags is new Abstract_Containers.Bags; function Hash (N : Natural) return Natural is (N); package Bags is new Abstract_Bags.Unmanaged (Buckets => 43, Hash => Hash); type Input_Data is array (Natural range <>) of Natural; Input : Input_Data := (2, 3, 8, 2, 2, 2, 7, 2, 3, 4, 8); Counts : Bags.Bag; begin for Number of Input loop Counts.Add (Number); end loop; declare It : Abstract_Containers.Iterator'Class := Counts.New_Iterator; begin while not It.Is_Done loop Put_Line (It.Current_Item'Image & " -> " & Counts.Count (It.Current_Item)'Image); It.Next; end loop; end; end Main;