OSDN Git Service

2007-04-20 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-pehage.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --          G N A T . P E R F E C T _ H A S H _ G E N E R A T O R S         --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                     Copyright (C) 2002-2005, AdaCore                     --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This package provides a generator of static minimal perfect hash functions.
35 --  To understand what a perfect hash function is, we define several notions.
36 --  These definitions are inspired from the following paper:
37
38 --    Zbigniew J. Czech, George Havas, and Bohdan S. Majewski ``An Optimal
39 --    Algorithm for Generating Minimal Perfect Hash Functions'', Information
40 --    Processing Letters, 43(1992) pp.257-264, Oct.1992
41
42 --  Let W be a set of m words. A hash function h is a function that maps the
43 --  set of words W into some given interval of integers [0, k-1], where k is an
44 --  integer, usually k >= m. h (w) where is a word computes an address or an
45 --  integer from I for the storage or the retrieval of that item. The storage
46 --  area used to store items is known as a hash table. Words for which the same
47 --  address is computed are called synonyms. Due to the existence of synonyms a
48 --  situation called collision may arise in which two items w1 and w2 have the
49 --  same address. Several schemes for resolving known. A perfect hash function
50 --  is an injection from the word set W to the integer interval I with k >= m.
51 --  If k = m, then h is a minimal perfect hash function. A hash function is
52 --  order preserving if it puts entries into the hash table in prespecified
53 --  order.
54
55 --  A minimal perfect hash function is defined by two properties:
56
57 --    Since no collisions occur each item can be retrieved from the table in
58 --    *one* probe. This represents the "perfect" property.
59
60 --    The hash table size corresponds to the exact size of W and *no larger*.
61 --    This represents the "minimal" property.
62
63 --  The functions generated by this package require the key set to be known in
64 --  advance (they are "static" hash functions). The hash functions are also
65 --  order preservering. If w2 is inserted after w1 in the generator, then (w1)
66 --  < f (w2). These hashing functions are convenient for use with realtime
67 --  applications.
68
69 package GNAT.Perfect_Hash_Generators is
70
71    Default_K_To_V : constant Float  := 2.05;
72    --  Default ratio for the algorithm. When K is the number of keys, V =
73    --  (K_To_V) * K is the size of the main table of the hash function. To
74    --  converge, the algorithm requires K_To_V to be stricly greater than 2.0.
75
76    Default_Pkg_Name : constant String := "Perfect_Hash";
77    --  Default package name in which the hash function is defined
78
79    Default_Position : constant String := "";
80    --  The generator allows selection of the character positions used in the
81    --  hash function. By default, all positions are selected.
82
83    Default_Tries : constant Positive := 20;
84    --  This algorithm may not succeed to find a possible mapping on the first
85    --  try and may have to iterate a number of times. This constant bounds the
86    --  number of tries.
87
88    type Optimization is (Memory_Space, CPU_Time);
89    Default_Optimization : constant Optimization := CPU_Time;
90    --  Optimize either the memory space or the execution time
91
92    Verbose : Boolean := False;
93    --  Output the status of the algorithm. For instance, the tables, the random
94    --  graph (edges, vertices) and selected char positions are output between
95    --  two iterations.
96
97    procedure Initialize
98      (Seed   : Natural;
99       K_To_V : Float        := Default_K_To_V;
100       Optim  : Optimization := CPU_Time;
101       Tries  : Positive     := Default_Tries);
102    --  Initialize the generator and its internal structures. Set the ratio of
103    --  vertices over keys in the random graphs. This value has to be greater
104    --  than 2.0 in order for the algorithm to succeed. The key set is not
105    --  modified (in particular when it is already set). For instance, it is
106    --  possible to run several times the generator with different settings on
107    --  the same key set.
108
109    procedure Finalize;
110    --  Deallocate the internal structures and the key table
111
112    procedure Insert (Value : String);
113    --  Insert a new key in the table
114
115    Too_Many_Tries : exception;
116    --  Raised after Tries unsuccessfull runs
117
118    procedure Compute (Position : String := Default_Position);
119    --  Compute the hash function. Position allows to define selection of
120    --  character positions used in the keywords hash function. Positions can be
121    --  separated by commas and range like x-y may be used. Character '$'
122    --  represents the final character of a key. With an empty position, the
123    --  generator automatically produces positions to reduce the memory usage.
124    --  Raise Too_Many_Tries in case that the algorithm does not succeed in less
125    --  than Tries attempts (see Initialize).
126
127    procedure Produce (Pkg_Name  : String := Default_Pkg_Name);
128    --  Generate the hash function package Pkg_Name. This package includes the
129    --  minimal perfect Hash function.
130
131    --  The routines and structures defined below allow producing the hash
132    --  function using a different way from the procedure above. The procedure
133    --  Define returns the lengths of an internal table and its item type size.
134    --  The function Value returns the value of each item in the table.
135
136    --  The hash function has the following form:
137
138    --             h (w) = (g (f1 (w)) + g (f2 (w))) mod m
139
140    --  G is a function based on a graph table [0,n-1] -> [0,m-1]. m is the
141    --  number of keys. n is an internally computed value and it can be obtained
142    --  as the length of vector G.
143
144    --  F1 and F2 are two functions based on two function tables T1 and T2.
145    --  Their definition depends on the chosen optimization mode.
146
147    --  Only some character positions are used in the keys because they are
148    --  significant. They are listed in a character position table (P in the
149    --  pseudo-code below). For instance, in {"jan", "feb", "mar", "apr", "jun",
150    --  "jul", "aug", "sep", "oct", "nov", "dec"}, only positions 2 and 3 are
151    --  significant (the first character can be ignored). In this example, P =
152    --  {2, 3}
153
154    --  When Optimization is CPU_Time, the first dimension of T1 and T2
155    --  corresponds to the character position in the key and the second to the
156    --  character set. As all the character set is not used, we define a used
157    --  character table which associates a distinct index to each used character
158    --  (unused characters are mapped to zero). In this case, the second
159    --  dimension of T1 and T2 is reduced to the used character set (C in the
160    --  pseudo-code below). Therefore, the hash function has the following:
161
162    --    function Hash (S : String) return Natural is
163    --       F      : constant Natural := S'First - 1;
164    --       L      : constant Natural := S'Length;
165    --       F1, F2 : Natural := 0;
166    --       J      : <t>;
167
168    --    begin
169    --       for K in P'Range loop
170    --          exit when L < P (K);
171    --          J  := C (S (P (K) + F));
172    --          F1 := (F1 + Natural (T1 (K, J))) mod <n>;
173    --          F2 := (F2 + Natural (T2 (K, J))) mod <n>;
174    --       end loop;
175
176    --       return (Natural (G (F1)) + Natural (G (F2))) mod <m>;
177    --    end Hash;
178
179    --  When Optimization is Memory_Space, the first dimension of T1 and T2
180    --  corresponds to the character position in the key and the second
181    --  dimension is ignored. T1 and T2 are no longer matrices but vectors.
182    --  Therefore, the used character table is not available. The hash function
183    --  has the following form:
184
185    --     function Hash (S : String) return Natural is
186    --        F      : constant Natural := S'First - 1;
187    --        L      : constant Natural := S'Length;
188    --        F1, F2 : Natural := 0;
189    --        J      : <t>;
190
191    --     begin
192    --        for K in P'Range loop
193    --           exit when L < P (K);
194    --           J  := Character'Pos (S (P (K) + F));
195    --           F1 := (F1 + Natural (T1 (K) * J)) mod <n>;
196    --           F2 := (F2 + Natural (T2 (K) * J)) mod <n>;
197    --        end loop;
198
199    --        return (Natural (G (F1)) + Natural (G (F2))) mod <m>;
200    --     end Hash;
201
202    type Table_Name is
203      (Character_Position,
204       Used_Character_Set,
205       Function_Table_1,
206       Function_Table_2,
207       Graph_Table);
208
209    procedure Define
210      (Name      : Table_Name;
211       Item_Size : out Natural;
212       Length_1  : out Natural;
213       Length_2  : out Natural);
214    --  Return the definition of the table Name. This includes the length of
215    --  dimensions 1 and 2 and the size of an unsigned integer item. When
216    --  Length_2 is zero, the table has only one dimension. All the ranges start
217    --  from zero.
218
219    function Value
220      (Name : Table_Name;
221       J    : Natural;
222       K    : Natural := 0) return Natural;
223    --  Return the value of the component (I, J) of the table Name. When the
224    --  table has only one dimension, J is ignored.
225
226 end GNAT.Perfect_Hash_Generators;