sciada_0.1.0_29e19539/src/sci-occurrences-arrays.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
60
61
62
63
64
65
66
67
68
69
-- --------------------------------------------------------------------
--  sci-occurrences-arrays -- identify occurrences of array based items
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--  SPDX-License-Identifier: Apache-2.0
-----------------------------------------------------------------------

--  === Array based items ===
--  To find occurence of strings, instantiate with:
--
--     package String_Occurrence is
--        new SCI.Occurrences.Arrays (Element_Type => Character,
--                                    Index_Type   => Positive,
--                                    Array_Type   => String,
--                                    Occurrence_Type => Natural);
with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Containers.Indefinite_Vectors;
generic
   type Element_Type is private;
   type Index_Type is (<>);
   type Array_Type is array (Index_Type range <>) of Element_Type;
   type Occurrence_Type is private;
   with function "<" (Left, Right : Array_Type) return Boolean is <>;
package SCI.Occurrences.Arrays is

   type Occurrence (First, Last : Index_Type) is record
      Element    : Array_Type (First .. Last);
      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.Indefinite_Vectors (Positive, Occurrence);
   subtype Vector is Vectors.Vector;

   package Sets is
     new Ada.Containers.Indefinite_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 Array_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 : Array_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.Arrays;