sciada_0.1.0_29e19539/src/sci-occurrences-finites.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-- --------------------------------------------------------------------
--  sci-occurrences-finites -- helper to identify occurrences of a given item
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--  SPDX-License-Identifier: Apache-2.0
-----------------------------------------------------------------------

with Ada.Containers.Ordered_Sets;
with Ada.Containers.Vectors;
generic
   type Element_Type is private;
   type Occurrence_Type is private;
   with function "<" (Left, Right : Element_Type) return Boolean is <>;
package SCI.Occurrences.Finites is

   type Occurrence is record
      Element    : Element_Type;
      Occurrence : Occurrence_Type;
   end record;

   --  Compare the two occurrence only on their name.
   function "<" (Left, Right : Occurrence)
                 return Boolean is (Left.Element < Right.Element);
   function Same (Left, Right : Occurrence)
                  return Boolean is (Left.Element = Right.Element);

   package Vectors is
     new Ada.Containers.Vectors (Positive, Occurrence);
   subtype Vector is Vectors.Vector;

   package Sets is
     new Ada.Containers.Ordered_Sets (Occurrence, "<", Same);
   subtype Set is Sets.Set;

   --  Add the item in the set.  If the item already exists, add the value
   --  to the current count.
   generic
      with function "+" (Left, Right : Occurrence_Type) return Occurrence_Type is <>;
   procedure Add (Set   : in out Sets.Set;
                  Item  : in Element_Type;
                  Value : in Occurrence_Type);

   --  Get the list of occurrence items sorted on the occurence value.
   generic
      with function "<" (Left, Right : Occurrence_Type) return Boolean is <>;
   procedure List (Set  : in Sets.Set;
                   Into : in out Vectors.Vector);

   --  Return the longest item in the list.
   generic
      with function Length (Item : Element_Type) return Natural is <>;
   function Longest (From : in Vectors.Vector) return Natural;

   --  Compute the sum of every element count.
   generic
      with function "+" (Left, Right : Occurrence_Type) return Occurrence_Type is <>;
   function Sum (From    : in Vectors.Vector;
                 Initial : in Occurrence_Type) return Occurrence_Type;

end SCI.Occurrences.Finites;