1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
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 --
9 -- Copyright (C) 2002-2007, AdaCore --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada
.IO_Exceptions
; use Ada
.IO_Exceptions
;
36 with GNAT
.Heap_Sort_G
;
37 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
40 package body GNAT
.Perfect_Hash_Generators
is
42 -- We are using the algorithm of J. Czech as described in Zbigniew J.
43 -- Czech, George Havas, and Bohdan S. Majewski ``An Optimal Algorithm for
44 -- Generating Minimal Perfect Hash Functions'', Information Processing
45 -- Letters, 43(1992) pp.257-264, Oct.1992
47 -- This minimal perfect hash function generator is based on random graphs
48 -- and produces a hash function of the form:
50 -- h (w) = (g (f1 (w)) + g (f2 (w))) mod m
52 -- where f1 and f2 are functions that map strings into integers, and g is a
53 -- function that maps integers into [0, m-1]. h can be order preserving.
54 -- For instance, let W = {w_0, ..., w_i, ...,
55 -- w_m-1}, h can be defined such that h (w_i) = i.
57 -- This algorithm defines two possible constructions of f1 and f2. Method
58 -- b) stores the hash function in less memory space at the expense of
61 -- a) fk (w) = sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n
63 -- size (Tk) = max (for w in W) (length (w)) * size (used char set)
65 -- b) fk (w) = sum (for i in 1 .. length (w)) (Tk (i) * w (i)) mod n
67 -- size (Tk) = max (for w in W) (length (w)) but the table lookups are
68 -- replaced by multiplications.
70 -- where Tk values are randomly generated. n is defined later on but the
71 -- algorithm recommends to use a value a little bit greater than 2m. Note
72 -- that for large values of m, the main memory space requirements comes
73 -- from the memory space for storing function g (>= 2m entries).
75 -- Random graphs are frequently used to solve difficult problems that do
76 -- not have polynomial solutions. This algorithm is based on a weighted
77 -- undirected graph. It comprises two steps: mapping and assignment.
79 -- In the mapping step, a graph G = (V, E) is constructed, where = {0, 1,
80 -- ..., n-1} and E = {(for w in W) (f1 (w), f2 (w))}. In order for the
81 -- assignment step to be successful, G has to be acyclic. To have a high
82 -- probability of generating an acyclic graph, n >= 2m. If it is not
83 -- acyclic, Tk have to be regenerated.
85 -- In the assignment step, the algorithm builds function g. As is acyclic,
86 -- there is a vertex v1 with only one neighbor v2. Let w_i be the word such
87 -- that v1 = f1 (w_i) and v2 = f2 (w_i). Let g (v1) = 0 by construction and
88 -- g (v2) = (i - g (v1)) mod n (or to be general, (h (i) - g (v1) mod n).
89 -- If word w_j is such that v2 = f1 (w_j) and v3 = f2 (w_j), g (v3) = (j -
90 -- g (v2)) mod (or to be general, (h (j) - g (v2)) mod n). If w_i has no
91 -- neighbor, then another vertex is selected. The algorithm traverses G to
92 -- assign values to all the vertices. It cannot assign a value to an
93 -- already assigned vertex as G is acyclic.
95 subtype Word_Id
is Integer;
96 subtype Key_Id
is Integer;
97 subtype Vertex_Id
is Integer;
98 subtype Edge_Id
is Integer;
99 subtype Table_Id
is Integer;
101 No_Vertex
: constant Vertex_Id
:= -1;
102 No_Edge
: constant Edge_Id
:= -1;
103 No_Table
: constant Table_Id
:= -1;
105 Max_Word_Length
: constant := 32;
106 subtype Word_Type
is String (1 .. Max_Word_Length
);
107 Null_Word
: constant Word_Type
:= (others => ASCII
.NUL
);
108 -- Store keyword in a word. Note that the length of word is limited to 32
111 type Key_Type
is record
114 -- A key corresponds to an edge in the algorithm graph
116 type Vertex_Type
is record
120 -- A vertex can be involved in several edges. First and Last are the bounds
121 -- of an array of edges stored in a global edge table.
123 type Edge_Type
is record
128 -- An edge is a peer of vertices. In the algorithm, a key is associated to
131 package WT
is new GNAT
.Table
(Word_Type
, Word_Id
, 0, 32, 32);
132 package IT
is new GNAT
.Table
(Integer, Integer, 0, 32, 32);
133 -- The two main tables. IT is used to store several tables of components
134 -- containing only integers.
136 function Image
(Int
: Integer; W
: Natural := 0) return String;
137 function Image
(Str
: String; W
: Natural := 0) return String;
138 -- Return a string which includes string Str or integer Int preceded by
139 -- leading spaces if required by width W.
141 Output
: File_Descriptor
renames GNAT
.OS_Lib
.Standout
;
144 EOL
: constant Character := ASCII
.LF
;
146 Max
: constant := 78;
148 Line
: String (1 .. Max
);
149 -- Use this line to provide buffered IO
151 procedure Add
(C
: Character);
152 procedure Add
(S
: String);
153 -- Add a character or a string in Line and update Last
156 (F
: File_Descriptor
;
164 -- Write string S into file F as a element of an array of one or two
165 -- dimensions. Fk (resp. Lk and Ck) indicates the first (resp last and
166 -- current) index in the k-th dimension. If F1 = L1 the array is considered
167 -- as a one dimension array. This dimension is described by F2 and L2. This
168 -- routine takes care of all the parenthesis, spaces and commas needed to
169 -- format correctly the array. Moreover, the array is well indented and is
170 -- wrapped to fit in a 80 col line. When the line is full, the routine
171 -- writes it into file F. When the array is completed, the routine adds
172 -- semi-colon and writes the line into file F.
174 procedure New_Line
(File
: File_Descriptor
);
175 -- Simulate Ada.Text_IO.New_Line with GNAT.OS_Lib
177 procedure Put
(File
: File_Descriptor
; Str
: String);
178 -- Simulate Ada.Text_IO.Put with GNAT.OS_Lib
180 procedure Put_Used_Char_Set
(File
: File_Descriptor
; Title
: String);
181 -- Output a title and a used character set
183 procedure Put_Int_Vector
184 (File
: File_Descriptor
;
188 -- Output a title and a vector
190 procedure Put_Int_Matrix
191 (File
: File_Descriptor
;
196 -- Output a title and a matrix. When the matrix has only one non-empty
197 -- dimension (Len_2 = 0), output a vector.
199 procedure Put_Edges
(File
: File_Descriptor
; Title
: String);
200 -- Output a title and an edge table
202 procedure Put_Initial_Keys
(File
: File_Descriptor
; Title
: String);
203 -- Output a title and a key table
205 procedure Put_Reduced_Keys
(File
: File_Descriptor
; Title
: String);
206 -- Output a title and a key table
208 procedure Put_Vertex_Table
(File
: File_Descriptor
; Title
: String);
209 -- Output a title and a vertex table
211 ----------------------------------
212 -- Character Position Selection --
213 ----------------------------------
215 -- We reduce the maximum key size by selecting representative positions
216 -- in these keys. We build a matrix with one word per line. We fill the
217 -- remaining space of a line with ASCII.NUL. The heuristic selects the
218 -- position that induces the minimum number of collisions. If there are
219 -- collisions, select another position on the reduced key set responsible
220 -- of the collisions. Apply the heuristic until there is no more collision.
222 procedure Apply_Position_Selection
;
223 -- Apply Position selection and build the reduced key table
225 procedure Parse_Position_Selection
(Argument
: String);
226 -- Parse Argument and compute the position set. Argument is list of
227 -- substrings separated by commas. Each substring represents a position
228 -- or a range of positions (like x-y).
230 procedure Select_Character_Set
;
231 -- Define an optimized used character set like Character'Pos in order not
232 -- to allocate tables of 256 entries.
234 procedure Select_Char_Position
;
235 -- Find a min char position set in order to reduce the max key length. The
236 -- heuristic selects the position that induces the minimum number of
237 -- collisions. If there are collisions, select another position on the
238 -- reduced key set responsible of the collisions. Apply the heuristic until
239 -- there is no collision.
241 -----------------------------
242 -- Random Graph Generation --
243 -----------------------------
245 procedure Random
(Seed
: in out Natural);
246 -- Simulate Ada.Discrete_Numerics.Random
248 procedure Generate_Mapping_Table
252 Seed
: in out Natural);
253 -- Random generation of the tables below. T is already allocated
255 procedure Generate_Mapping_Tables
257 Seed
: in out Natural);
258 -- Generate the mapping tables T1 and T2. They are used to define fk (w) =
259 -- sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n. Keys, NK and Chars
260 -- are used to compute the matrix size.
262 ---------------------------
263 -- Algorithm Computation --
264 ---------------------------
266 procedure Compute_Edges_And_Vertices
(Opt
: Optimization
);
267 -- Compute the edge and vertex tables. These are empty when a self loop is
268 -- detected (f1 (w) = f2 (w)). The edge table is sorted by X value and then
269 -- Y value. Keys is the key table and NK the number of keys. Chars is the
270 -- set of characters really used in Keys. NV is the number of vertices
271 -- recommended by the algorithm. T1 and T2 are the mapping tables needed to
272 -- compute f1 (w) and f2 (w).
274 function Acyclic
return Boolean;
275 -- Return True when the graph is acyclic. Vertices is the current vertex
276 -- table and Edges the current edge table.
278 procedure Assign_Values_To_Vertices
;
279 -- Execute the assignment step of the algorithm. Keys is the current key
280 -- table. Vertices and Edges represent the random graph. G is the result of
281 -- the assignment step such that:
282 -- h (w) = (g (f1 (w)) + g (f2 (w))) mod m
287 Opt
: Optimization
) return Natural;
288 -- For an optimization of CPU_Time return
289 -- fk (w) = sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n
290 -- For an optimization of Memory_Space return
291 -- fk (w) = sum (for i in 1 .. length (w)) (Tk (i) * w (i)) mod n
294 -------------------------------
295 -- Internal Table Management --
296 -------------------------------
298 function Allocate
(N
: Natural; S
: Natural := 1) return Table_Id
;
299 -- Allocate N * S ints from IT table
301 procedure Free_Tmp_Tables
;
302 -- Deallocate the tables used by the algorithm (but not the keys table)
308 Keys
: Table_Id
:= No_Table
;
310 -- NK : Number of Keys
312 function Initial
(K
: Key_Id
) return Word_Id
;
313 pragma Inline
(Initial
);
315 function Reduced
(K
: Key_Id
) return Word_Id
;
316 pragma Inline
(Reduced
);
318 function Get_Key
(N
: Key_Id
) return Key_Type
;
319 procedure Set_Key
(N
: Key_Id
; Item
: Key_Type
);
320 -- Get or Set Nth element of Keys table
326 Char_Pos_Set
: Table_Id
:= No_Table
;
327 Char_Pos_Set_Len
: Natural;
328 -- Character Selected Position Set
330 function Get_Char_Pos
(P
: Natural) return Natural;
331 procedure Set_Char_Pos
(P
: Natural; Item
: Natural);
332 -- Get or Set the string position of the Pth selected character
338 Used_Char_Set
: Table_Id
:= No_Table
;
339 Used_Char_Set_Len
: Natural;
340 -- Used Character Set : Define a new character mapping. When all the
341 -- characters are not present in the keys, in order to reduce the size
342 -- of some tables, we redefine the character mapping.
344 function Get_Used_Char
(C
: Character) return Natural;
345 procedure Set_Used_Char
(C
: Character; Item
: Natural);
351 T1
: Table_Id
:= No_Table
;
352 T2
: Table_Id
:= No_Table
;
355 -- T1 : Values table to compute F1
356 -- T2 : Values table to compute F2
358 function Get_Table
(T
: Integer; X
, Y
: Natural) return Natural;
359 procedure Set_Table
(T
: Integer; X
, Y
: Natural; Item
: Natural);
365 G
: Table_Id
:= No_Table
;
367 -- Values table to compute G
369 NT
: Natural := Default_Tries
;
370 -- Number of tries running the algorithm before raising an error
372 function Get_Graph
(N
: Natural) return Integer;
373 procedure Set_Graph
(N
: Natural; Item
: Integer);
374 -- Get or Set Nth element of graph
380 Edge_Size
: constant := 3;
381 Edges
: Table_Id
:= No_Table
;
383 -- Edges : Edge table of the random graph G
385 function Get_Edges
(F
: Natural) return Edge_Type
;
386 procedure Set_Edges
(F
: Natural; Item
: Edge_Type
);
392 Vertex_Size
: constant := 2;
394 Vertices
: Table_Id
:= No_Table
;
395 -- Vertex table of the random graph G
398 -- Number of Vertices
400 function Get_Vertices
(F
: Natural) return Vertex_Type
;
401 procedure Set_Vertices
(F
: Natural; Item
: Vertex_Type
);
402 -- Comments needed ???
405 -- Ratio between Keys and Vertices (parameter of Czech's algorithm)
408 -- Optimization mode (memory vs CPU)
410 Max_Key_Len
: Natural := 0;
411 Min_Key_Len
: Natural := Max_Word_Length
;
412 -- Maximum and minimum of all the word length
417 function Type_Size
(L
: Natural) return Natural;
418 -- Given the last L of an unsigned integer type T, return its size
424 function Acyclic
return Boolean is
425 Marks
: array (0 .. NV
- 1) of Vertex_Id
:= (others => No_Vertex
);
427 function Traverse
(Edge
: Edge_Id
; Mark
: Vertex_Id
) return Boolean;
428 -- Propagate Mark from X to Y. X is already marked. Mark Y and propagate
429 -- it to the edges of Y except the one representing the same key. Return
430 -- False when Y is marked with Mark.
436 function Traverse
(Edge
: Edge_Id
; Mark
: Vertex_Id
) return Boolean is
437 E
: constant Edge_Type
:= Get_Edges
(Edge
);
438 K
: constant Key_Id
:= E
.Key
;
439 Y
: constant Vertex_Id
:= E
.Y
;
440 M
: constant Vertex_Id
:= Marks
(E
.Y
);
447 elsif M
= No_Vertex
then
449 V
:= Get_Vertices
(Y
);
451 for J
in V
.First
.. V
.Last
loop
453 -- Do not propagate to the edge representing the same key
455 if Get_Edges
(J
).Key
/= K
456 and then not Traverse
(J
, Mark
)
468 -- Start of processing for Acyclic
471 -- Edges valid range is
473 for J
in 1 .. Edges_Len
- 1 loop
475 Edge
:= Get_Edges
(J
);
477 -- Mark X of E when it has not been already done
479 if Marks
(Edge
.X
) = No_Vertex
then
480 Marks
(Edge
.X
) := Edge
.X
;
483 -- Traverse E when this has not already been done
485 if Marks
(Edge
.Y
) = No_Vertex
486 and then not Traverse
(J
, Edge
.X
)
499 procedure Add
(C
: Character) is
501 Line
(Last
+ 1) := C
;
509 procedure Add
(S
: String) is
510 Len
: constant Natural := S
'Length;
512 Line
(Last
+ 1 .. Last
+ Len
) := S
;
520 function Allocate
(N
: Natural; S
: Natural := 1) return Table_Id
is
521 L
: constant Integer := IT
.Last
;
523 IT
.Set_Last
(L
+ N
* S
);
527 ------------------------------
528 -- Apply_Position_Selection --
529 ------------------------------
531 procedure Apply_Position_Selection
is
533 WT
.Set_Last
(2 * NK
);
534 for J
in 0 .. NK
- 1 loop
536 I_Word
: constant Word_Type
:= WT
.Table
(Initial
(J
));
537 R_Word
: Word_Type
:= Null_Word
;
538 Index
: Natural := I_Word
'First - 1;
541 -- Select the characters of Word included in the position
544 for C
in 0 .. Char_Pos_Set_Len
- 1 loop
545 exit when I_Word
(Get_Char_Pos
(C
)) = ASCII
.NUL
;
547 R_Word
(Index
) := I_Word
(Get_Char_Pos
(C
));
550 -- Build the new table with the reduced word
552 WT
.Table
(Reduced
(J
)) := R_Word
;
553 Set_Key
(J
, (Edge
=> No_Edge
));
556 end Apply_Position_Selection
;
558 -------------------------------
559 -- Assign_Values_To_Vertices --
560 -------------------------------
562 procedure Assign_Values_To_Vertices
is
565 procedure Assign
(X
: Vertex_Id
);
566 -- Execute assignment on X's neighbors except the vertex that we are
567 -- coming from which is already assigned.
573 procedure Assign
(X
: Vertex_Id
) is
575 V
: constant Vertex_Type
:= Get_Vertices
(X
);
578 for J
in V
.First
.. V
.Last
loop
581 if Get_Graph
(E
.Y
) = -1 then
582 Set_Graph
(E
.Y
, (E
.Key
- Get_Graph
(X
)) mod NK
);
588 -- Start of processing for Assign_Values_To_Vertices
591 -- Value -1 denotes an uninitialized value as it is supposed to
592 -- be in the range 0 .. NK.
596 G
:= Allocate
(G_Len
, 1);
599 for J
in 0 .. G_Len
- 1 loop
603 for K
in 0 .. NK
- 1 loop
604 X
:= Get_Edges
(Get_Key
(K
).Edge
).X
;
606 if Get_Graph
(X
) = -1 then
612 for J
in 0 .. G_Len
- 1 loop
613 if Get_Graph
(J
) = -1 then
619 Put_Int_Vector
(Output
, "Assign Values To Vertices", G
, G_Len
);
621 end Assign_Values_To_Vertices
;
627 procedure Compute
(Position
: String := Default_Position
) is
628 Success
: Boolean := False;
631 NV
:= Natural (K2V
* Float (NK
));
633 Keys
:= Allocate
(NK
);
636 Put_Initial_Keys
(Output
, "Initial Key Table");
639 if Position
'Length /= 0 then
640 Parse_Position_Selection
(Position
);
642 Select_Char_Position
;
647 (Output
, "Char Position Set", Char_Pos_Set
, Char_Pos_Set_Len
);
650 Apply_Position_Selection
;
653 Put_Reduced_Keys
(Output
, "Reduced Keys Table");
656 Select_Character_Set
;
659 Put_Used_Char_Set
(Output
, "Character Position Table");
662 -- Perform Czech's algorithm
664 for J
in 1 .. NT
loop
665 Generate_Mapping_Tables
(Opt
, S
);
666 Compute_Edges_And_Vertices
(Opt
);
668 -- When graph is not empty (no self-loop from previous operation) and
671 if 0 < Edges_Len
and then Acyclic
then
678 raise Too_Many_Tries
;
681 Assign_Values_To_Vertices
;
684 --------------------------------
685 -- Compute_Edges_And_Vertices --
686 --------------------------------
688 procedure Compute_Edges_And_Vertices
(Opt
: Optimization
) is
693 Vertex
: Vertex_Type
;
694 Not_Acyclic
: Boolean := False;
696 procedure Move
(From
: Natural; To
: Natural);
697 function Lt
(L
, R
: Natural) return Boolean;
698 -- Subprograms needed for GNAT.Heap_Sort_G
704 function Lt
(L
, R
: Natural) return Boolean is
705 EL
: constant Edge_Type
:= Get_Edges
(L
);
706 ER
: constant Edge_Type
:= Get_Edges
(R
);
708 return EL
.X
< ER
.X
or else (EL
.X
= ER
.X
and then EL
.Y
< ER
.Y
);
715 procedure Move
(From
: Natural; To
: Natural) is
717 Set_Edges
(To
, Get_Edges
(From
));
720 package Sorting
is new GNAT
.Heap_Sort_G
(Move
, Lt
);
722 -- Start of processing for Compute_Edges_And_Vertices
725 -- We store edges from 1 to 2 * NK and leave zero alone in order to use
728 Edges_Len
:= 2 * NK
+ 1;
730 if Edges
= No_Table
then
731 Edges
:= Allocate
(Edges_Len
, Edge_Size
);
734 if Vertices
= No_Table
then
735 Vertices
:= Allocate
(NV
, Vertex_Size
);
738 for J
in 0 .. NV
- 1 loop
739 Set_Vertices
(J
, (No_Vertex
, No_Vertex
- 1));
742 -- For each w, X = f1 (w) and Y = f2 (w)
744 for J
in 0 .. NK
- 1 loop
749 X
:= Sum
(WT
.Table
(Reduced
(J
)), T1
, Opt
);
750 Y
:= Sum
(WT
.Table
(Reduced
(J
)), T2
, Opt
);
752 -- Discard T1 and T2 as soon as we discover a self loop
759 -- We store (X, Y) and (Y, X) to ease assignment step
761 Set_Edges
(2 * J
+ 1, (X
, Y
, J
));
762 Set_Edges
(2 * J
+ 2, (Y
, X
, J
));
765 -- Return an empty graph when self loop detected
772 Put_Edges
(Output
, "Unsorted Edge Table");
773 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
775 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
779 -- Enforce consistency between edges and keys. Construct Vertices and
780 -- compute the list of neighbors of a vertex First .. Last as Edges
781 -- is sorted by X and then Y. To compute the neighbor list, sort the
784 Sorting
.Sort
(Edges_Len
- 1);
787 Put_Edges
(Output
, "Sorted Edge Table");
788 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
790 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
794 -- Edges valid range is 1 .. 2 * NK
796 for E
in 1 .. Edges_Len
- 1 loop
797 Edge
:= Get_Edges
(E
);
798 Key
:= Get_Key
(Edge
.Key
);
800 if Key
.Edge
= No_Edge
then
802 Set_Key
(Edge
.Key
, Key
);
805 Vertex
:= Get_Vertices
(Edge
.X
);
807 if Vertex
.First
= No_Edge
then
812 Set_Vertices
(Edge
.X
, Vertex
);
816 Put_Reduced_Keys
(Output
, "Key Table");
817 Put_Edges
(Output
, "Edge Table");
818 Put_Vertex_Table
(Output
, "Vertex Table");
821 end Compute_Edges_And_Vertices
;
829 Item_Size
: out Natural;
830 Length_1
: out Natural;
831 Length_2
: out Natural)
835 when Character_Position
=>
837 Length_1
:= Char_Pos_Set_Len
;
840 when Used_Character_Set
=>
845 when Function_Table_1
846 | Function_Table_2
=>
847 Item_Size
:= Type_Size
(NV
);
852 Item_Size
:= Type_Size
(NK
);
862 procedure Finalize
is
871 Min_Key_Len
:= Max_Word_Length
;
874 ---------------------
875 -- Free_Tmp_Tables --
876 ---------------------
878 procedure Free_Tmp_Tables
is
884 Char_Pos_Set
:= No_Table
;
885 Char_Pos_Set_Len
:= 0;
887 Used_Char_Set
:= No_Table
;
888 Used_Char_Set_Len
:= 0;
902 Vertices
:= No_Table
;
906 ----------------------------
907 -- Generate_Mapping_Table --
908 ----------------------------
910 procedure Generate_Mapping_Table
914 Seed
: in out Natural)
917 for J
in 0 .. L1
- 1 loop
918 for K
in 0 .. L2
- 1 loop
920 Set_Table
(Tab
, J
, K
, Seed
mod NV
);
923 end Generate_Mapping_Table
;
925 -----------------------------
926 -- Generate_Mapping_Tables --
927 -----------------------------
929 procedure Generate_Mapping_Tables
931 Seed
: in out Natural)
934 -- If T1 and T2 are already allocated no need to do it twice. Reuse them
935 -- as their size has not changed.
937 if T1
= No_Table
and then T2
= No_Table
then
939 Used_Char_Last
: Natural := 0;
943 if Opt
= CPU_Time
then
944 for P
in reverse Character'Range loop
945 Used_Char
:= Get_Used_Char
(P
);
946 if Used_Char
/= 0 then
947 Used_Char_Last
:= Used_Char
;
953 T1_Len
:= Char_Pos_Set_Len
;
954 T2_Len
:= Used_Char_Last
+ 1;
955 T1
:= Allocate
(T1_Len
* T2_Len
);
956 T2
:= Allocate
(T1_Len
* T2_Len
);
960 Generate_Mapping_Table
(T1
, T1_Len
, T2_Len
, Seed
);
961 Generate_Mapping_Table
(T2
, T1_Len
, T2_Len
, Seed
);
964 Put_Used_Char_Set
(Output
, "Used Character Set");
965 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
967 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
970 end Generate_Mapping_Tables
;
976 function Get_Char_Pos
(P
: Natural) return Natural is
977 N
: constant Natural := Char_Pos_Set
+ P
;
986 function Get_Edges
(F
: Natural) return Edge_Type
is
987 N
: constant Natural := Edges
+ (F
* Edge_Size
);
991 E
.Y
:= IT
.Table
(N
+ 1);
992 E
.Key
:= IT
.Table
(N
+ 2);
1000 function Get_Graph
(N
: Natural) return Integer is
1002 return IT
.Table
(G
+ N
);
1009 function Get_Key
(N
: Key_Id
) return Key_Type
is
1012 K
.Edge
:= IT
.Table
(Keys
+ N
);
1020 function Get_Table
(T
: Integer; X
, Y
: Natural) return Natural is
1021 N
: constant Natural := T
+ (Y
* T1_Len
) + X
;
1023 return IT
.Table
(N
);
1030 function Get_Used_Char
(C
: Character) return Natural is
1031 N
: constant Natural := Used_Char_Set
+ Character'Pos (C
);
1033 return IT
.Table
(N
);
1040 function Get_Vertices
(F
: Natural) return Vertex_Type
is
1041 N
: constant Natural := Vertices
+ (F
* Vertex_Size
);
1044 V
.First
:= IT
.Table
(N
);
1045 V
.Last
:= IT
.Table
(N
+ 1);
1053 function Image
(Int
: Integer; W
: Natural := 0) return String is
1054 B
: String (1 .. 32);
1057 procedure Img
(V
: Natural);
1058 -- Compute image of V into B, starting at B (L), incrementing L
1064 procedure Img
(V
: Natural) is
1071 B
(L
) := Character'Val ((V
mod 10) + Character'Pos ('0'));
1074 -- Start of processing for Image
1085 return Image
(B
(1 .. L
), W
);
1092 function Image
(Str
: String; W
: Natural := 0) return String is
1093 Len
: constant Natural := Str
'Length;
1094 Max
: Natural := Len
;
1102 Buf
: String (1 .. Max
) := (1 .. Max
=> ' ');
1105 for J
in 0 .. Len
- 1 loop
1106 Buf
(Max
- Len
+ 1 + J
) := Str
(Str
'First + J
);
1117 function Initial
(K
: Key_Id
) return Word_Id
is
1126 procedure Initialize
1128 K_To_V
: Float := Default_K_To_V
;
1129 Optim
: Optimization
:= CPU_Time
;
1130 Tries
: Positive := Default_Tries
)
1133 -- Free previous tables (the settings may have changed between two runs)
1137 if K_To_V
<= 2.0 then
1138 Put
(Output
, "K to V ratio cannot be lower than 2.0");
1140 raise Program_Error
;
1153 procedure Insert
(Value
: String) is
1154 Word
: Word_Type
:= Null_Word
;
1155 Len
: constant Natural := Value
'Length;
1158 Word
(1 .. Len
) := Value
(Value
'First .. Value
'First + Len
- 1);
1160 WT
.Table
(NK
) := Word
;
1162 NV
:= Natural (Float (NK
) * K2V
);
1164 -- Do not accept a value of K2V too close to 2.0 such that once rounded
1165 -- up, NV = 2 * NK because the algorithm would not converge.
1167 if NV
<= 2 * NK
then
1171 if Max_Key_Len
< Len
then
1175 if Len
< Min_Key_Len
then
1184 procedure New_Line
(File
: File_Descriptor
) is
1186 if Write
(File
, EOL
'Address, 1) /= 1 then
1187 raise Program_Error
;
1191 ------------------------------
1192 -- Parse_Position_Selection --
1193 ------------------------------
1195 procedure Parse_Position_Selection
(Argument
: String) is
1196 N
: Natural := Argument
'First;
1197 L
: constant Natural := Argument
'Last;
1198 M
: constant Natural := Max_Key_Len
;
1200 T
: array (1 .. M
) of Boolean := (others => False);
1202 function Parse_Index
return Natural;
1203 -- Parse argument starting at index N to find an index
1209 function Parse_Index
return Natural is
1210 C
: Character := Argument
(N
);
1219 if C
not in '0' .. '9' then
1220 raise Program_Error
with "cannot read position argument";
1223 while C
in '0' .. '9' loop
1224 V
:= V
* 10 + (Character'Pos (C
) - Character'Pos ('0'));
1233 -- Start of processing for Parse_Position_Selection
1236 -- Empty specification means all the positions
1239 Char_Pos_Set_Len
:= M
;
1240 Char_Pos_Set
:= Allocate
(Char_Pos_Set_Len
);
1242 for C
in 0 .. Char_Pos_Set_Len
- 1 loop
1243 Set_Char_Pos
(C
, C
+ 1);
1249 First
, Last
: Natural;
1252 First
:= Parse_Index
;
1257 if N
<= L
and then Argument
(N
) = '-' then
1259 Last
:= Parse_Index
;
1262 -- Include the positions in the selection
1264 for J
in First
.. Last
loop
1271 if Argument
(N
) /= ',' then
1272 raise Program_Error
with "cannot read position argument";
1278 -- Compute position selection length
1281 for J
in T
'Range loop
1287 -- Fill position selection
1289 Char_Pos_Set_Len
:= N
;
1290 Char_Pos_Set
:= Allocate
(Char_Pos_Set_Len
);
1293 for J
in T
'Range loop
1295 Set_Char_Pos
(N
, J
);
1300 end Parse_Position_Selection
;
1306 procedure Produce
(Pkg_Name
: String := Default_Pkg_Name
) is
1307 File
: File_Descriptor
;
1310 -- For call to Close
1312 function Array_Img
(N
, T
, R1
: String; R2
: String := "") return String;
1313 -- Return string "N : constant array (R1[, R2]) of T;"
1315 function Range_Img
(F
, L
: Natural; T
: String := "") return String;
1316 -- Return string "[T range ]F .. L"
1318 function Type_Img
(L
: Natural) return String;
1319 -- Return the larger unsigned type T such that T'Last < L
1327 R2
: String := "") return String
1333 Add
(" : constant array (");
1344 return Line
(1 .. Last
);
1351 function Range_Img
(F
, L
: Natural; T
: String := "") return String is
1352 FI
: constant String := Image
(F
);
1353 FL
: constant Natural := FI
'Length;
1354 LI
: constant String := Image
(L
);
1355 LL
: constant Natural := LI
'Length;
1356 TL
: constant Natural := T
'Length;
1357 RI
: String (1 .. TL
+ 7 + FL
+ 4 + LL
);
1362 RI
(Len
+ 1 .. Len
+ TL
) := T
;
1364 RI
(Len
+ 1 .. Len
+ 7) := " range ";
1368 RI
(Len
+ 1 .. Len
+ FL
) := FI
;
1370 RI
(Len
+ 1 .. Len
+ 4) := " .. ";
1372 RI
(Len
+ 1 .. Len
+ LL
) := LI
;
1374 return RI
(1 .. Len
);
1381 function Type_Img
(L
: Natural) return String is
1382 S
: constant String := Image
(Type_Size
(L
));
1383 U
: String := "Unsigned_ ";
1387 for J
in S
'Range loop
1399 PLen
: constant Natural := Pkg_Name
'Length;
1400 FName
: String (1 .. PLen
+ 4);
1402 -- Start of processing for Produce
1405 FName
(1 .. PLen
) := Pkg_Name
;
1406 for J
in 1 .. PLen
loop
1407 if FName
(J
) in 'A' .. 'Z' then
1408 FName
(J
) := Character'Val (Character'Pos (FName
(J
))
1409 - Character'Pos ('A')
1410 + Character'Pos ('a'));
1412 elsif FName
(J
) = '.' then
1417 FName
(PLen
+ 1 .. PLen
+ 4) := ".ads";
1419 File
:= Create_File
(FName
, Binary
);
1421 Put
(File
, "package ");
1422 Put
(File
, Pkg_Name
);
1425 Put
(File
, " function Hash (S : String) return Natural;");
1428 Put
(File
, Pkg_Name
);
1431 Close
(File
, Status
);
1437 FName
(PLen
+ 4) := 'b';
1439 File
:= Create_File
(FName
, Binary
);
1441 Put
(File
, "with Interfaces; use Interfaces;");
1444 Put
(File
, "package body ");
1445 Put
(File
, Pkg_Name
);
1450 if Opt
= CPU_Time
then
1451 Put
(File
, Array_Img
("C", Type_Img
(256), "Character"));
1454 F
:= Character'Pos (Character'First);
1455 L
:= Character'Pos (Character'Last);
1457 for J
in Character'Range loop
1458 P
:= Get_Used_Char
(J
);
1459 Put
(File
, Image
(P
), 1, 0, 1, F
, L
, Character'Pos (J
));
1466 L
:= Char_Pos_Set_Len
- 1;
1468 Put
(File
, Array_Img
("P", "Natural", Range_Img
(F
, L
)));
1471 for J
in F
.. L
loop
1472 Put
(File
, Image
(Get_Char_Pos
(J
)), 1, 0, 1, F
, L
, J
);
1477 if Opt
= CPU_Time
then
1480 Array_Img
("T1", Type_Img
(NV
),
1481 Range_Img
(0, T1_Len
- 1),
1482 Range_Img
(0, T2_Len
- 1, Type_Img
(256))),
1483 T1
, T1_Len
, T2_Len
);
1488 Array_Img
("T1", Type_Img
(NV
),
1489 Range_Img
(0, T1_Len
- 1)),
1495 if Opt
= CPU_Time
then
1498 Array_Img
("T2", Type_Img
(NV
),
1499 Range_Img
(0, T1_Len
- 1),
1500 Range_Img
(0, T2_Len
- 1, Type_Img
(256))),
1501 T2
, T1_Len
, T2_Len
);
1506 Array_Img
("T2", Type_Img
(NV
),
1507 Range_Img
(0, T1_Len
- 1)),
1515 Array_Img
("G", Type_Img
(NK
),
1516 Range_Img
(0, G_Len
- 1)),
1520 Put
(File
, " function Hash (S : String) return Natural is");
1522 Put
(File
, " F : constant Natural := S'First - 1;");
1524 Put
(File
, " L : constant Natural := S'Length;");
1526 Put
(File
, " F1, F2 : Natural := 0;");
1529 Put
(File
, " J : ");
1531 if Opt
= CPU_Time
then
1532 Put
(File
, Type_Img
(256));
1534 Put
(File
, "Natural");
1540 Put
(File
, " begin");
1542 Put
(File
, " for K in P'Range loop");
1544 Put
(File
, " exit when L < P (K);");
1546 Put
(File
, " J := ");
1548 if Opt
= CPU_Time
then
1551 Put
(File
, "Character'Pos");
1554 Put
(File
, " (S (P (K) + F));");
1557 Put
(File
, " F1 := (F1 + Natural (T1 (K");
1559 if Opt
= CPU_Time
then
1565 if Opt
= Memory_Space
then
1569 Put
(File
, ") mod ");
1570 Put
(File
, Image
(NV
));
1574 Put
(File
, " F2 := (F2 + Natural (T2 (K");
1576 if Opt
= CPU_Time
then
1582 if Opt
= Memory_Space
then
1586 Put
(File
, ") mod ");
1587 Put
(File
, Image
(NV
));
1591 Put
(File
, " end loop;");
1595 " return (Natural (G (F1)) + Natural (G (F2))) mod ");
1597 Put
(File
, Image
(NK
));
1600 Put
(File
, " end Hash;");
1604 Put
(File
, Pkg_Name
);
1607 Close
(File
, Status
);
1618 procedure Put
(File
: File_Descriptor
; Str
: String) is
1619 Len
: constant Natural := Str
'Length;
1621 if Write
(File
, Str
'Address, Len
) /= Len
then
1622 raise Program_Error
;
1631 (F
: File_Descriptor
;
1640 Len
: constant Natural := S
'Length;
1643 -- Write current line, followed by LF
1651 Put
(F
, Line
(1 .. Last
));
1656 -- Start of processing for Put
1659 if C1
= F1
and then C2
= F2
then
1663 if Last
+ Len
+ 3 > Max
then
1668 Line
(Last
+ 1 .. Last
+ 5) := " ";
1672 if C1
= F1
and then C2
= F2
then
1696 Line
(Last
+ 1 .. Last
+ Len
) := S
;
1725 procedure Put_Edges
(File
: File_Descriptor
; Title
: String) is
1727 F1
: constant Natural := 1;
1728 L1
: constant Natural := Edges_Len
- 1;
1729 M
: constant Natural := Max
/ 5;
1735 -- Edges valid range is 1 .. Edge_Len - 1
1737 for J
in F1
.. L1
loop
1739 Put
(File
, Image
(J
, M
), F1
, L1
, J
, 1, 4, 1);
1740 Put
(File
, Image
(E
.X
, M
), F1
, L1
, J
, 1, 4, 2);
1741 Put
(File
, Image
(E
.Y
, M
), F1
, L1
, J
, 1, 4, 3);
1742 Put
(File
, Image
(E
.Key
, M
), F1
, L1
, J
, 1, 4, 4);
1746 ----------------------
1747 -- Put_Initial_Keys --
1748 ----------------------
1750 procedure Put_Initial_Keys
(File
: File_Descriptor
; Title
: String) is
1751 F1
: constant Natural := 0;
1752 L1
: constant Natural := NK
- 1;
1753 M
: constant Natural := Max
/ 5;
1760 for J
in F1
.. L1
loop
1762 Put
(File
, Image
(J
, M
), F1
, L1
, J
, 1, 3, 1);
1763 Put
(File
, Image
(K
.Edge
, M
), F1
, L1
, J
, 1, 3, 2);
1764 Put
(File
, WT
.Table
(Initial
(J
)), F1
, L1
, J
, 1, 3, 3);
1766 end Put_Initial_Keys
;
1768 --------------------
1769 -- Put_Int_Matrix --
1770 --------------------
1772 procedure Put_Int_Matrix
1773 (File
: File_Descriptor
;
1779 F1
: constant Integer := 0;
1780 L1
: constant Integer := Len_1
- 1;
1781 F2
: constant Integer := 0;
1782 L2
: constant Integer := Len_2
- 1;
1790 for J
in F1
.. L1
loop
1791 Ix
:= IT
.Table
(Table
+ J
);
1792 Put
(File
, Image
(Ix
), 1, 0, 1, F1
, L1
, J
);
1796 for J
in F1
.. L1
loop
1797 for K
in F2
.. L2
loop
1798 Ix
:= IT
.Table
(Table
+ J
+ K
* Len_1
);
1799 Put
(File
, Image
(Ix
), F1
, L1
, J
, F2
, L2
, K
);
1805 --------------------
1806 -- Put_Int_Vector --
1807 --------------------
1809 procedure Put_Int_Vector
1810 (File
: File_Descriptor
;
1815 F2
: constant Natural := 0;
1816 L2
: constant Natural := Length
- 1;
1822 for J
in F2
.. L2
loop
1823 Put
(File
, Image
(IT
.Table
(Vector
+ J
)), 1, 0, 1, F2
, L2
, J
);
1827 ----------------------
1828 -- Put_Reduced_Keys --
1829 ----------------------
1831 procedure Put_Reduced_Keys
(File
: File_Descriptor
; Title
: String) is
1832 F1
: constant Natural := 0;
1833 L1
: constant Natural := NK
- 1;
1834 M
: constant Natural := Max
/ 5;
1841 for J
in F1
.. L1
loop
1843 Put
(File
, Image
(J
, M
), F1
, L1
, J
, 1, 3, 1);
1844 Put
(File
, Image
(K
.Edge
, M
), F1
, L1
, J
, 1, 3, 2);
1845 Put
(File
, WT
.Table
(Reduced
(J
)), F1
, L1
, J
, 1, 3, 3);
1847 end Put_Reduced_Keys
;
1849 -----------------------
1850 -- Put_Used_Char_Set --
1851 -----------------------
1853 procedure Put_Used_Char_Set
(File
: File_Descriptor
; Title
: String) is
1854 F
: constant Natural := Character'Pos (Character'First);
1855 L
: constant Natural := Character'Pos (Character'Last);
1861 for J
in Character'Range loop
1863 (File
, Image
(Get_Used_Char
(J
)), 1, 0, 1, F
, L
, Character'Pos (J
));
1865 end Put_Used_Char_Set
;
1867 ----------------------
1868 -- Put_Vertex_Table --
1869 ----------------------
1871 procedure Put_Vertex_Table
(File
: File_Descriptor
; Title
: String) is
1872 F1
: constant Natural := 0;
1873 L1
: constant Natural := NV
- 1;
1874 M
: constant Natural := Max
/ 4;
1881 for J
in F1
.. L1
loop
1882 V
:= Get_Vertices
(J
);
1883 Put
(File
, Image
(J
, M
), F1
, L1
, J
, 1, 3, 1);
1884 Put
(File
, Image
(V
.First
, M
), F1
, L1
, J
, 1, 3, 2);
1885 Put
(File
, Image
(V
.Last
, M
), F1
, L1
, J
, 1, 3, 3);
1887 end Put_Vertex_Table
;
1893 procedure Random
(Seed
: in out Natural) is
1895 -- Park & Miller Standard Minimal using Schrage's algorithm to avoid
1896 -- overflow: Xn+1 = 16807 * Xn mod (2 ** 31 - 1)
1903 R
:= Seed
mod 127773;
1905 X
:= 16807 * R
- 2836 * Q
;
1908 Seed
:= X
+ 2147483647;
1918 function Reduced
(K
: Key_Id
) return Word_Id
is
1923 --------------------------
1924 -- Select_Char_Position --
1925 --------------------------
1927 procedure Select_Char_Position
is
1929 type Vertex_Table_Type
is array (Natural range <>) of Vertex_Type
;
1931 procedure Build_Identical_Keys_Sets
1932 (Table
: in out Vertex_Table_Type
;
1933 Last
: in out Natural;
1935 -- Build a list of keys subsets that are identical with the current
1936 -- position selection plus Pos. Once this routine is called, reduced
1937 -- words are sorted by subsets and each item (First, Last) in Sets
1938 -- defines the range of identical keys.
1939 -- Need comment saying exactly what Last is ???
1941 function Count_Different_Keys
1942 (Table
: Vertex_Table_Type
;
1944 Pos
: Natural) return Natural;
1945 -- For each subset in Sets, count the number of different keys if we add
1946 -- Pos to the current position selection.
1948 Sel_Position
: IT
.Table_Type
(1 .. Max_Key_Len
);
1949 Last_Sel_Pos
: Natural := 0;
1950 Max_Sel_Pos
: Natural := 0;
1952 -------------------------------
1953 -- Build_Identical_Keys_Sets --
1954 -------------------------------
1956 procedure Build_Identical_Keys_Sets
1957 (Table
: in out Vertex_Table_Type
;
1958 Last
: in out Natural;
1961 S
: constant Vertex_Table_Type
:= Table
(Table
'First .. Last
);
1962 C
: constant Natural := Pos
;
1963 -- Shortcuts (why are these not renames ???)
1967 -- First and last words of a subset
1970 -- GNAT.Heap_Sort assumes that the first array index is 1. Offset
1971 -- defines the translation to operate.
1973 function Lt
(L
, R
: Natural) return Boolean;
1974 procedure Move
(From
: Natural; To
: Natural);
1975 -- Subprograms needed by GNAT.Heap_Sort_G
1981 function Lt
(L
, R
: Natural) return Boolean is
1982 C
: constant Natural := Pos
;
1988 Left
:= Reduced
(0) - 1;
1989 Right
:= Offset
+ R
;
1992 Right
:= Reduced
(0) - 1;
1995 Right
:= Offset
+ R
;
1998 return WT
.Table
(Left
)(C
) < WT
.Table
(Right
)(C
);
2005 procedure Move
(From
: Natural; To
: Natural) is
2006 Target
, Source
: Natural;
2010 Source
:= Reduced
(0) - 1;
2011 Target
:= Offset
+ To
;
2013 Source
:= Offset
+ From
;
2014 Target
:= Reduced
(0) - 1;
2016 Source
:= Offset
+ From
;
2017 Target
:= Offset
+ To
;
2020 WT
.Table
(Target
) := WT
.Table
(Source
);
2023 package Sorting
is new GNAT
.Heap_Sort_G
(Move
, Lt
);
2025 -- Start of processing for Build_Identical_Key_Sets
2030 -- For each subset in S, extract the new subsets we have by adding C
2031 -- in the position selection.
2033 for J
in S
'Range loop
2034 if S
(J
).First
= S
(J
).Last
then
2038 Table
(Last
) := (F
, L
);
2041 Offset
:= Reduced
(S
(J
).First
) - 1;
2042 Sorting
.Sort
(S
(J
).Last
- S
(J
).First
+ 1);
2046 for N
in S
(J
).First
.. S
(J
).Last
loop
2048 -- For the last item, close the last subset
2050 if N
= S
(J
).Last
then
2052 Table
(Last
) := (F
, N
);
2054 -- Two contiguous words are identical when they have the
2055 -- same Cth character.
2057 elsif WT
.Table
(Reduced
(N
))(C
) =
2058 WT
.Table
(Reduced
(N
+ 1))(C
)
2062 -- Find a new subset of identical keys. Store the current
2063 -- one and create a new subset.
2067 Table
(Last
) := (F
, L
);
2074 end Build_Identical_Keys_Sets
;
2076 --------------------------
2077 -- Count_Different_Keys --
2078 --------------------------
2080 function Count_Different_Keys
2081 (Table
: Vertex_Table_Type
;
2083 Pos
: Natural) return Natural
2085 N
: array (Character) of Natural;
2090 -- For each subset, count the number of words that are still
2091 -- different when we include Pos in the position selection. Only
2092 -- focus on this position as the other positions already produce
2095 for S
in 1 .. Last
loop
2097 -- Count the occurrences of the different characters
2100 for K
in Table
(S
).First
.. Table
(S
).Last
loop
2101 C
:= WT
.Table
(Reduced
(K
))(Pos
);
2105 -- Update the number of different keys. Each character used
2106 -- denotes a different key.
2108 for J
in N
'Range loop
2116 end Count_Different_Keys
;
2118 -- Start of processing for Select_Char_Position
2121 -- Initialize the reduced words set
2123 WT
.Set_Last
(2 * NK
);
2124 for K
in 0 .. NK
- 1 loop
2125 WT
.Table
(Reduced
(K
)) := WT
.Table
(Initial
(K
));
2129 Differences
: Natural;
2130 Max_Differences
: Natural := 0;
2131 Old_Differences
: Natural;
2132 Max_Diff_Sel_Pos
: Natural := 0; -- init to kill warning
2133 Max_Diff_Sel_Pos_Idx
: Natural := 0; -- init to kill warning
2134 Same_Keys_Sets_Table
: Vertex_Table_Type
(1 .. NK
);
2135 Same_Keys_Sets_Last
: Natural := 1;
2138 for C
in Sel_Position
'Range loop
2139 Sel_Position
(C
) := C
;
2142 Same_Keys_Sets_Table
(1) := (0, NK
- 1);
2145 -- Preserve maximum number of different keys and check later on
2146 -- that this value is strictly incrementing. Otherwise, it means
2147 -- that two keys are strictly identical.
2149 Old_Differences
:= Max_Differences
;
2151 -- The first position should not exceed the minimum key length.
2152 -- Otherwise, we may end up with an empty word once reduced.
2154 if Last_Sel_Pos
= 0 then
2155 Max_Sel_Pos
:= Min_Key_Len
;
2157 Max_Sel_Pos
:= Max_Key_Len
;
2160 -- Find which position increases more the number of differences
2162 for J
in Last_Sel_Pos
+ 1 .. Max_Sel_Pos
loop
2163 Differences
:= Count_Different_Keys
2164 (Same_Keys_Sets_Table
,
2165 Same_Keys_Sets_Last
,
2170 "Selecting position" & Sel_Position
(J
)'Img &
2171 " results in" & Differences
'Img &
2176 if Differences
> Max_Differences
then
2177 Max_Differences
:= Differences
;
2178 Max_Diff_Sel_Pos
:= Sel_Position
(J
);
2179 Max_Diff_Sel_Pos_Idx
:= J
;
2183 if Old_Differences
= Max_Differences
then
2184 raise Program_Error
with "some keys are identical";
2187 -- Insert selected position and sort Sel_Position table
2189 Last_Sel_Pos
:= Last_Sel_Pos
+ 1;
2190 Sel_Position
(Last_Sel_Pos
+ 1 .. Max_Diff_Sel_Pos_Idx
) :=
2191 Sel_Position
(Last_Sel_Pos
.. Max_Diff_Sel_Pos_Idx
- 1);
2192 Sel_Position
(Last_Sel_Pos
) := Max_Diff_Sel_Pos
;
2194 for P
in 1 .. Last_Sel_Pos
- 1 loop
2195 if Max_Diff_Sel_Pos
< Sel_Position
(P
) then
2196 Sel_Position
(P
+ 1 .. Last_Sel_Pos
) :=
2197 Sel_Position
(P
.. Last_Sel_Pos
- 1);
2198 Sel_Position
(P
) := Max_Diff_Sel_Pos
;
2203 exit when Max_Differences
= NK
;
2205 Build_Identical_Keys_Sets
2206 (Same_Keys_Sets_Table
,
2207 Same_Keys_Sets_Last
,
2212 "Selecting position" & Max_Diff_Sel_Pos
'Img &
2213 " results in" & Max_Differences
'Img &
2218 for J
in 1 .. Same_Keys_Sets_Last
loop
2220 Same_Keys_Sets_Table
(J
).First
..
2221 Same_Keys_Sets_Table
(J
).Last
2223 Put
(Output
, WT
.Table
(Reduced
(K
)));
2233 Char_Pos_Set_Len
:= Last_Sel_Pos
;
2234 Char_Pos_Set
:= Allocate
(Char_Pos_Set_Len
);
2236 for C
in 1 .. Last_Sel_Pos
loop
2237 Set_Char_Pos
(C
- 1, Sel_Position
(C
));
2239 end Select_Char_Position
;
2241 --------------------------
2242 -- Select_Character_Set --
2243 --------------------------
2245 procedure Select_Character_Set
is
2246 Last
: Natural := 0;
2247 Used
: array (Character) of Boolean := (others => False);
2251 for J
in 0 .. NK
- 1 loop
2252 for K
in 0 .. Char_Pos_Set_Len
- 1 loop
2253 Char
:= WT
.Table
(Initial
(J
))(Get_Char_Pos
(K
));
2254 exit when Char
= ASCII
.NUL
;
2255 Used
(Char
) := True;
2259 Used_Char_Set_Len
:= 256;
2260 Used_Char_Set
:= Allocate
(Used_Char_Set_Len
);
2262 for J
in Used
'Range loop
2264 Set_Used_Char
(J
, Last
);
2267 Set_Used_Char
(J
, 0);
2270 end Select_Character_Set
;
2276 procedure Set_Char_Pos
(P
: Natural; Item
: Natural) is
2277 N
: constant Natural := Char_Pos_Set
+ P
;
2279 IT
.Table
(N
) := Item
;
2286 procedure Set_Edges
(F
: Natural; Item
: Edge_Type
) is
2287 N
: constant Natural := Edges
+ (F
* Edge_Size
);
2289 IT
.Table
(N
) := Item
.X
;
2290 IT
.Table
(N
+ 1) := Item
.Y
;
2291 IT
.Table
(N
+ 2) := Item
.Key
;
2298 procedure Set_Graph
(N
: Natural; Item
: Integer) is
2300 IT
.Table
(G
+ N
) := Item
;
2307 procedure Set_Key
(N
: Key_Id
; Item
: Key_Type
) is
2309 IT
.Table
(Keys
+ N
) := Item
.Edge
;
2316 procedure Set_Table
(T
: Integer; X
, Y
: Natural; Item
: Natural) is
2317 N
: constant Natural := T
+ ((Y
* T1_Len
) + X
);
2319 IT
.Table
(N
) := Item
;
2326 procedure Set_Used_Char
(C
: Character; Item
: Natural) is
2327 N
: constant Natural := Used_Char_Set
+ Character'Pos (C
);
2329 IT
.Table
(N
) := Item
;
2336 procedure Set_Vertices
(F
: Natural; Item
: Vertex_Type
) is
2337 N
: constant Natural := Vertices
+ (F
* Vertex_Size
);
2339 IT
.Table
(N
) := Item
.First
;
2340 IT
.Table
(N
+ 1) := Item
.Last
;
2350 Opt
: Optimization
) return Natural
2356 if Opt
= CPU_Time
then
2357 for J
in 0 .. T1_Len
- 1 loop
2358 exit when Word
(J
+ 1) = ASCII
.NUL
;
2359 R
:= Get_Table
(Table
, J
, Get_Used_Char
(Word
(J
+ 1)));
2360 S
:= (S
+ R
) mod NV
;
2364 for J
in 0 .. T1_Len
- 1 loop
2365 exit when Word
(J
+ 1) = ASCII
.NUL
;
2366 R
:= Get_Table
(Table
, J
, 0);
2367 S
:= (S
+ R
* Character'Pos (Word
(J
+ 1))) mod NV
;
2378 function Type_Size
(L
: Natural) return Natural is
2382 elsif L
<= 2 ** 16 then
2396 K
: Natural := 0) return Natural
2400 when Character_Position
=>
2401 return Get_Char_Pos
(J
);
2403 when Used_Character_Set
=>
2404 return Get_Used_Char
(Character'Val (J
));
2406 when Function_Table_1
=>
2407 return Get_Table
(T1
, J
, K
);
2409 when Function_Table_2
=>
2410 return Get_Table
(T2
, J
, K
);
2413 return Get_Graph
(J
);
2418 end GNAT
.Perfect_Hash_Generators
;