agpl_1.0.0_b5da3320/src/agpl-simple_dictionary.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
70
71
72
73
74
75
 

--  Provides a one-way hash table with a single value per key.

with Agpl.Types.Ustrings; use Agpl.Types.Ustrings;

with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;

generic
   type Element_Type is private;
   with function Equal (L, R : in Element_Type) return Boolean;
package Agpl.Simple_Dictionary is

   pragma Preelaborate;

   ------------------------------------------------------------------------
   -- Object                                                             --
   ------------------------------------------------------------------------
   type Object is private;

   type Pairs is record
      Key   : Ustring;
      Value : Element_Type;
   end record;
   type Pair_Array is array (Positive range <>) of Pairs;

   ------------------------------------------------------------------------
   -- Add_Word                                                           --
   ------------------------------------------------------------------------
   --  Add a word with given index (key).
   procedure Add_Word (This : in out Object; Key : in String; Element : in Element_Type);

   ------------------------------------------------------------------------
   -- Are_Compatible                                                     --
   ------------------------------------------------------------------------
   --  True if elements in both containers are equal, extra are ignored.
   --  At least one element must match.
   --  Commutative.
   function Are_Compatible (L, R : in Object) return Boolean;

   ------------------------------------------------------------------------
   -- Contains_Key                                                       --
   ------------------------------------------------------------------------
   --  True if the dictionary contains the given key
   function Contains_Key (This : in Object; Key : in String) return Boolean;

   ------------------------------------------------------------------------
   -- Get_Contents                                                       --
   ------------------------------------------------------------------------
   --  Return an array of contents in the dictionary
   function Get_Contents (This : in Object) return Pair_Array;

   ------------------------------------------------------------------------
   -- Get_Value                                                          --
   ------------------------------------------------------------------------
   function Get_Value (This : in Object; Key : in String) return Element_Type;

   ------------------------------------------------------------------------
   -- Merge                                                              --
   ------------------------------------------------------------------------
   --  Adds elements not in Former from Later.
   --  No compatibility check is performed
   procedure Merge (Former : in out Object; Later : in Object);

private

   package Element_Map is new Ada.Containers.Indefinite_Hashed_Maps (
      String, Element_Type, Ada.Strings.Hash, "=", Equal);

   type Object is new Element_Map.Map with null record;

   pragma Inline (Add_Word);

end Agpl.Simple_Dictionary;