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-2005 Ada Core Technologies, Inc. --
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
.Exceptions
; use Ada
.Exceptions
;
35 with Ada
.IO_Exceptions
; use Ada
.IO_Exceptions
;
37 with GNAT
.Heap_Sort_A
; use GNAT
.Heap_Sort_A
;
38 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
41 package body GNAT
.Perfect_Hash_Generators
is
43 -- We are using the algorithm of J. Czech as described in Zbigniew J.
44 -- Czech, George Havas, and Bohdan S. Majewski ``An Optimal Algorithm for
45 -- Generating Minimal Perfect Hash Functions'', Information Processing
46 -- Letters, 43(1992) pp.257-264, Oct.1992
48 -- This minimal perfect hash function generator is based on random graphs
49 -- and produces a hash function of the form:
51 -- h (w) = (g (f1 (w)) + g (f2 (w))) mod m
53 -- where f1 and f2 are functions that map strings into integers, and g is a
54 -- function that maps integers into [0, m-1]. h can be order preserving.
55 -- For instance, let W = {w_0, ..., w_i, ...,
56 -- w_m-1}, h can be defined such that h (w_i) = i.
58 -- This algorithm defines two possible constructions of f1 and f2. Method
59 -- b) stores the hash function in less memory space at the expense of
62 -- a) fk (w) = sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n
64 -- size (Tk) = max (for w in W) (length (w)) * size (used char set)
66 -- b) fk (w) = sum (for i in 1 .. length (w)) (Tk (i) * w (i)) mod n
68 -- size (Tk) = max (for w in W) (length (w)) but the table lookups are
69 -- replaced by multiplications.
71 -- where Tk values are randomly generated. n is defined later on but the
72 -- algorithm recommends to use a value a little bit greater than 2m. Note
73 -- that for large values of m, the main memory space requirements comes
74 -- from the memory space for storing function g (>= 2m entries).
76 -- Random graphs are frequently used to solve difficult problems that do
77 -- not have polynomial solutions. This algorithm is based on a weighted
78 -- undirected graph. It comprises two steps: mapping and assigment.
80 -- In the mapping step, a graph G = (V, E) is constructed, where = {0, 1,
81 -- ..., n-1} and E = {(for w in W) (f1 (w), f2 (w))}. In order for the
82 -- assignment step to be successful, G has to be acyclic. To have a high
83 -- probability of generating an acyclic graph, n >= 2m. If it is not
84 -- acyclic, Tk have to be regenerated.
86 -- In the assignment step, the algorithm builds function g. As is acyclic,
87 -- there is a vertex v1 with only one neighbor v2. Let w_i be the word such
88 -- that v1 = f1 (w_i) and v2 = f2 (w_i). Let g (v1) = 0 by construction and
89 -- g (v2) = (i - g (v1)) mod n (or to be general, (h (i) - g (v1) mod n).
90 -- If word w_j is such that v2 = f1 (w_j) and v3 = f2 (w_j), g (v3) = (j -
91 -- g (v2)) mod (or to be general, (h (j) - g (v2)) mod n). If w_i has no
92 -- neighbor, then another vertex is selected. The algorithm traverses G to
93 -- assign values to all the vertices. It cannot assign a value to an
94 -- already assigned vertex as G is acyclic.
96 subtype Word_Id
is Integer;
97 subtype Key_Id
is Integer;
98 subtype Vertex_Id
is Integer;
99 subtype Edge_Id
is Integer;
100 subtype Table_Id
is Integer;
102 No_Vertex
: constant Vertex_Id
:= -1;
103 No_Edge
: constant Edge_Id
:= -1;
104 No_Table
: constant Table_Id
:= -1;
106 Max_Word_Length
: constant := 32;
107 subtype Word_Type
is String (1 .. Max_Word_Length
);
108 Null_Word
: constant Word_Type
:= (others => ASCII
.NUL
);
109 -- Store keyword in a word. Note that the length of word is limited to 32
112 type Key_Type
is record
115 -- A key corresponds to an edge in the algorithm graph
117 type Vertex_Type
is record
121 -- A vertex can be involved in several edges. First and Last are the bounds
122 -- of an array of edges stored in a global edge table.
124 type Edge_Type
is record
129 -- An edge is a peer of vertices. In the algorithm, a key is associated to
132 package WT
is new GNAT
.Table
(Word_Type
, Word_Id
, 0, 32, 32);
133 package IT
is new GNAT
.Table
(Integer, Integer, 0, 32, 32);
134 -- The two main tables. IT is used to store several tables of components
135 -- containing only integers.
137 function Image
(Int
: Integer; W
: Natural := 0) return String;
138 function Image
(Str
: String; W
: Natural := 0) return String;
139 -- Return a string which includes string Str or integer Int preceded by
140 -- leading spaces if required by width W.
142 Output
: File_Descriptor
renames GNAT
.OS_Lib
.Standout
;
145 EOL
: constant Character := ASCII
.LF
;
147 Max
: constant := 78;
149 Line
: String (1 .. Max
);
150 -- Use this line to provide buffered IO
152 procedure Add
(C
: Character);
153 procedure Add
(S
: String);
154 -- Add a character or a string in Line and update Last
157 (F
: File_Descriptor
;
165 -- Write string S into file F as a element of an array of one or two
166 -- dimensions. Fk (resp. Lk and Ck) indicates the first (resp last and
167 -- current) index in the k-th dimension. If F1 = L1 the array is considered
168 -- as a one dimension array. This dimension is described by F2 and L2. This
169 -- routine takes care of all the parenthesis, spaces and commas needed to
170 -- format correctly the array. Moreover, the array is well indented and is
171 -- wrapped to fit in a 80 col line. When the line is full, the routine
172 -- writes it into file F. When the array is completed, the routine adds
173 -- semi-colon and writes the line into file F.
176 (File
: File_Descriptor
);
177 -- Simulate Ada.Text_IO.New_Line with GNAT.OS_Lib
180 (File
: File_Descriptor
;
182 -- Simulate Ada.Text_IO.Put with GNAT.OS_Lib
184 procedure Put_Used_Char_Set
185 (File
: File_Descriptor
;
187 -- Output a title and a used character set
189 procedure Put_Int_Vector
190 (File
: File_Descriptor
;
194 -- Output a title and a vector
196 procedure Put_Int_Matrix
197 (File
: File_Descriptor
;
202 -- Output a title and a matrix. When the matrix has only one non-empty
203 -- dimension (Len_2 = 0), output a vector.
206 (File
: File_Descriptor
;
208 -- Output a title and an edge table
210 procedure Put_Initial_Keys
211 (File
: File_Descriptor
;
213 -- Output a title and a key table
215 procedure Put_Reduced_Keys
216 (File
: File_Descriptor
;
218 -- Output a title and a key table
220 procedure Put_Vertex_Table
221 (File
: File_Descriptor
;
223 -- Output a title and a vertex table
225 ----------------------------------
226 -- Character Position Selection --
227 ----------------------------------
229 -- We reduce the maximum key size by selecting representative positions
230 -- in these keys. We build a matrix with one word per line. We fill the
231 -- remaining space of a line with ASCII.NUL. The heuristic selects the
232 -- position that induces the minimum number of collisions. If there are
233 -- collisions, select another position on the reduced key set responsible
234 -- of the collisions. Apply the heuristic until there is no more collision.
236 procedure Apply_Position_Selection
;
237 -- Apply Position selection and build the reduced key table
239 procedure Parse_Position_Selection
(Argument
: String);
240 -- Parse Argument and compute the position set. Argument is list of
241 -- substrings separated by commas. Each substring represents a position
242 -- or a range of positions (like x-y).
244 procedure Select_Character_Set
;
245 -- Define an optimized used character set like Character'Pos in order not
246 -- to allocate tables of 256 entries.
248 procedure Select_Char_Position
;
249 -- Find a min char position set in order to reduce the max key length. The
250 -- heuristic selects the position that induces the minimum number of
251 -- collisions. If there are collisions, select another position on the
252 -- reduced key set responsible of the collisions. Apply the heuristic until
253 -- there is no collision.
255 -----------------------------
256 -- Random Graph Generation --
257 -----------------------------
259 procedure Random
(Seed
: in out Natural);
260 -- Simulate Ada.Discrete_Numerics.Random
262 procedure Generate_Mapping_Table
266 Seed
: in out Natural);
267 -- Random generation of the tables below. T is already allocated
269 procedure Generate_Mapping_Tables
271 Seed
: in out Natural);
272 -- Generate the mapping tables T1 and T2. They are used to define fk (w) =
273 -- sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n. Keys, NK and Chars
274 -- are used to compute the matrix size.
276 ---------------------------
277 -- Algorithm Computation --
278 ---------------------------
280 procedure Compute_Edges_And_Vertices
(Opt
: Optimization
);
281 -- Compute the edge and vertex tables. These are empty when a self loop is
282 -- detected (f1 (w) = f2 (w)). The edge table is sorted by X value and then
283 -- Y value. Keys is the key table and NK the number of keys. Chars is the
284 -- set of characters really used in Keys. NV is the number of vertices
285 -- recommended by the algorithm. T1 and T2 are the mapping tables needed to
286 -- compute f1 (w) and f2 (w).
288 function Acyclic
return Boolean;
289 -- Return True when the graph is acyclic. Vertices is the current vertex
290 -- table and Edges the current edge table.
292 procedure Assign_Values_To_Vertices
;
293 -- Execute the assignment step of the algorithm. Keys is the current key
294 -- table. Vertices and Edges represent the random graph. G is the result of
295 -- the assignment step such that:
296 -- h (w) = (g (f1 (w)) + g (f2 (w))) mod m
301 Opt
: Optimization
) return Natural;
302 -- For an optimization of CPU_Time return
303 -- fk (w) = sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n
304 -- For an optimization of Memory_Space return
305 -- fk (w) = sum (for i in 1 .. length (w)) (Tk (i) * w (i)) mod n
308 -------------------------------
309 -- Internal Table Management --
310 -------------------------------
312 function Allocate
(N
: Natural; S
: Natural := 1) return Table_Id
;
313 -- Allocate N * S ints from IT table
315 procedure Free_Tmp_Tables
;
316 -- Deallocate the tables used by the algorithm (but not the keys table)
322 Keys
: Table_Id
:= No_Table
;
324 -- NK : Number of Keys
326 function Initial
(K
: Key_Id
) return Word_Id
;
327 pragma Inline
(Initial
);
329 function Reduced
(K
: Key_Id
) return Word_Id
;
330 pragma Inline
(Reduced
);
332 function Get_Key
(N
: Key_Id
) return Key_Type
;
333 procedure Set_Key
(N
: Key_Id
; Item
: Key_Type
);
334 -- Get or Set Nth element of Keys table
340 Char_Pos_Set
: Table_Id
:= No_Table
;
341 Char_Pos_Set_Len
: Natural;
342 -- Character Selected Position Set
344 function Get_Char_Pos
(P
: Natural) return Natural;
345 procedure Set_Char_Pos
(P
: Natural; Item
: Natural);
346 -- Get or Set the string position of the Pth selected character
352 Used_Char_Set
: Table_Id
:= No_Table
;
353 Used_Char_Set_Len
: Natural;
354 -- Used Character Set : Define a new character mapping. When all the
355 -- characters are not present in the keys, in order to reduce the size
356 -- of some tables, we redefine the character mapping.
358 function Get_Used_Char
(C
: Character) return Natural;
359 procedure Set_Used_Char
(C
: Character; Item
: Natural);
365 T1
: Table_Id
:= No_Table
;
366 T2
: Table_Id
:= No_Table
;
369 -- T1 : Values table to compute F1
370 -- T2 : Values table to compute F2
372 function Get_Table
(T
: Integer; X
, Y
: Natural) return Natural;
373 procedure Set_Table
(T
: Integer; X
, Y
: Natural; Item
: Natural);
379 G
: Table_Id
:= No_Table
;
381 -- Values table to compute G
383 NT
: Natural := Default_Tries
;
384 -- Number of tries running the algorithm before raising an error
386 function Get_Graph
(N
: Natural) return Integer;
387 procedure Set_Graph
(N
: Natural; Item
: Integer);
388 -- Get or Set Nth element of graph
394 Edge_Size
: constant := 3;
395 Edges
: Table_Id
:= No_Table
;
397 -- Edges : Edge table of the random graph G
399 function Get_Edges
(F
: Natural) return Edge_Type
;
400 procedure Set_Edges
(F
: Natural; Item
: Edge_Type
);
406 Vertex_Size
: constant := 2;
408 Vertices
: Table_Id
:= No_Table
;
409 -- Vertex table of the random graph G
412 -- Number of Vertices
414 function Get_Vertices
(F
: Natural) return Vertex_Type
;
415 procedure Set_Vertices
(F
: Natural; Item
: Vertex_Type
);
416 -- Comments needed ???
419 -- Ratio between Keys and Vertices (parameter of Czech's algorithm)
422 -- Optimization mode (memory vs CPU)
424 Max_Key_Len
: Natural := 0;
425 Min_Key_Len
: Natural := Max_Word_Length
;
426 -- Maximum and minimum of all the word length
431 function Type_Size
(L
: Natural) return Natural;
432 -- Given the last L of an unsigned integer type T, return its size
438 function Acyclic
return Boolean is
439 Marks
: array (0 .. NV
- 1) of Vertex_Id
:= (others => No_Vertex
);
443 Mark
: Vertex_Id
) return Boolean;
444 -- Propagate Mark from X to Y. X is already marked. Mark Y and propagate
445 -- it to the edges of Y except the one representing the same key. Return
446 -- False when Y is marked with Mark.
454 Mark
: Vertex_Id
) return Boolean
456 E
: constant Edge_Type
:= Get_Edges
(Edge
);
457 K
: constant Key_Id
:= E
.Key
;
458 Y
: constant Vertex_Id
:= E
.Y
;
459 M
: constant Vertex_Id
:= Marks
(E
.Y
);
466 elsif M
= No_Vertex
then
468 V
:= Get_Vertices
(Y
);
470 for J
in V
.First
.. V
.Last
loop
472 -- Do not propagate to the edge representing the same key
474 if Get_Edges
(J
).Key
/= K
475 and then not Traverse
(J
, Mark
)
487 -- Start of processing for Acyclic
490 -- Edges valid range is
492 for J
in 1 .. Edges_Len
- 1 loop
494 Edge
:= Get_Edges
(J
);
496 -- Mark X of E when it has not been already done
498 if Marks
(Edge
.X
) = No_Vertex
then
499 Marks
(Edge
.X
) := Edge
.X
;
502 -- Traverse E when this has not already been done
504 if Marks
(Edge
.Y
) = No_Vertex
505 and then not Traverse
(J
, Edge
.X
)
518 procedure Add
(C
: Character) is
520 Line
(Last
+ 1) := C
;
528 procedure Add
(S
: String) is
529 Len
: constant Natural := S
'Length;
531 Line
(Last
+ 1 .. Last
+ Len
) := S
;
539 function Allocate
(N
: Natural; S
: Natural := 1) return Table_Id
is
540 L
: constant Integer := IT
.Last
;
542 IT
.Set_Last
(L
+ N
* S
);
546 ------------------------------
547 -- Apply_Position_Selection --
548 ------------------------------
550 procedure Apply_Position_Selection
is
552 WT
.Set_Last
(2 * NK
);
553 for J
in 0 .. NK
- 1 loop
555 I_Word
: constant Word_Type
:= WT
.Table
(Initial
(J
));
556 R_Word
: Word_Type
:= Null_Word
;
557 Index
: Natural := I_Word
'First - 1;
560 -- Select the characters of Word included in the position
563 for C
in 0 .. Char_Pos_Set_Len
- 1 loop
564 exit when I_Word
(Get_Char_Pos
(C
)) = ASCII
.NUL
;
566 R_Word
(Index
) := I_Word
(Get_Char_Pos
(C
));
569 -- Build the new table with the reduced word
571 WT
.Table
(Reduced
(J
)) := R_Word
;
572 Set_Key
(J
, (Edge
=> No_Edge
));
575 end Apply_Position_Selection
;
577 -------------------------------
578 -- Assign_Values_To_Vertices --
579 -------------------------------
581 procedure Assign_Values_To_Vertices
is
584 procedure Assign
(X
: Vertex_Id
);
585 -- Execute assignment on X's neighbors except the vertex that we are
586 -- coming from which is already assigned.
592 procedure Assign
(X
: Vertex_Id
)
595 V
: constant Vertex_Type
:= Get_Vertices
(X
);
597 for J
in V
.First
.. V
.Last
loop
599 if Get_Graph
(E
.Y
) = -1 then
600 Set_Graph
(E
.Y
, (E
.Key
- Get_Graph
(X
)) mod NK
);
606 -- Start of processing for Assign_Values_To_Vertices
609 -- Value -1 denotes an unitialized value as it is supposed to
610 -- be in the range 0 .. NK.
614 G
:= Allocate
(G_Len
, 1);
617 for J
in 0 .. G_Len
- 1 loop
621 for K
in 0 .. NK
- 1 loop
622 X
:= Get_Edges
(Get_Key
(K
).Edge
).X
;
624 if Get_Graph
(X
) = -1 then
630 for J
in 0 .. G_Len
- 1 loop
631 if Get_Graph
(J
) = -1 then
637 Put_Int_Vector
(Output
, "Assign Values To Vertices", G
, G_Len
);
639 end Assign_Values_To_Vertices
;
646 (Position
: String := Default_Position
)
648 Success
: Boolean := False;
651 NV
:= Natural (K2V
* Float (NK
));
653 Keys
:= Allocate
(NK
);
656 Put_Initial_Keys
(Output
, "Initial Key Table");
659 if Position
'Length /= 0 then
660 Parse_Position_Selection
(Position
);
662 Select_Char_Position
;
667 (Output
, "Char Position Set", Char_Pos_Set
, Char_Pos_Set_Len
);
670 Apply_Position_Selection
;
673 Put_Reduced_Keys
(Output
, "Reduced Keys Table");
676 Select_Character_Set
;
679 Put_Used_Char_Set
(Output
, "Character Position Table");
682 -- Perform Czech's algorithm
684 for J
in 1 .. NT
loop
685 Generate_Mapping_Tables
(Opt
, S
);
686 Compute_Edges_And_Vertices
(Opt
);
688 -- When graph is not empty (no self-loop from previous operation) and
691 if 0 < Edges_Len
and then Acyclic
then
698 raise Too_Many_Tries
;
701 Assign_Values_To_Vertices
;
704 --------------------------------
705 -- Compute_Edges_And_Vertices --
706 --------------------------------
708 procedure Compute_Edges_And_Vertices
(Opt
: Optimization
) is
713 Vertex
: Vertex_Type
;
714 Not_Acyclic
: Boolean := False;
716 procedure Move
(From
: Natural; To
: Natural);
717 function Lt
(L
, R
: Natural) return Boolean;
718 -- Subprograms needed for GNAT.Heap_Sort_A
724 function Lt
(L
, R
: Natural) return Boolean is
725 EL
: constant Edge_Type
:= Get_Edges
(L
);
726 ER
: constant Edge_Type
:= Get_Edges
(R
);
728 return EL
.X
< ER
.X
or else (EL
.X
= ER
.X
and then EL
.Y
< ER
.Y
);
735 procedure Move
(From
: Natural; To
: Natural) is
737 Set_Edges
(To
, Get_Edges
(From
));
740 -- Start of processing for Compute_Edges_And_Vertices
743 -- We store edges from 1 to 2 * NK and leave zero alone in order to use
746 Edges_Len
:= 2 * NK
+ 1;
748 if Edges
= No_Table
then
749 Edges
:= Allocate
(Edges_Len
, Edge_Size
);
752 if Vertices
= No_Table
then
753 Vertices
:= Allocate
(NV
, Vertex_Size
);
756 for J
in 0 .. NV
- 1 loop
757 Set_Vertices
(J
, (No_Vertex
, No_Vertex
- 1));
760 -- For each w, X = f1 (w) and Y = f2 (w)
762 for J
in 0 .. NK
- 1 loop
767 X
:= Sum
(WT
.Table
(Reduced
(J
)), T1
, Opt
);
768 Y
:= Sum
(WT
.Table
(Reduced
(J
)), T2
, Opt
);
770 -- Discard T1 and T2 as soon as we discover a self loop
777 -- We store (X, Y) and (Y, X) to ease assignment step
779 Set_Edges
(2 * J
+ 1, (X
, Y
, J
));
780 Set_Edges
(2 * J
+ 2, (Y
, X
, J
));
783 -- Return an empty graph when self loop detected
790 Put_Edges
(Output
, "Unsorted Edge Table");
791 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
793 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
797 -- Enforce consistency between edges and keys. Construct Vertices and
798 -- compute the list of neighbors of a vertex First .. Last as Edges
799 -- is sorted by X and then Y. To compute the neighbor list, sort the
804 Move
'Unrestricted_Access,
805 Lt
'Unrestricted_Access);
808 Put_Edges
(Output
, "Sorted Edge Table");
809 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
811 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
815 -- Edges valid range is 1 .. 2 * NK
817 for E
in 1 .. Edges_Len
- 1 loop
818 Edge
:= Get_Edges
(E
);
819 Key
:= Get_Key
(Edge
.Key
);
821 if Key
.Edge
= No_Edge
then
823 Set_Key
(Edge
.Key
, Key
);
826 Vertex
:= Get_Vertices
(Edge
.X
);
828 if Vertex
.First
= No_Edge
then
833 Set_Vertices
(Edge
.X
, Vertex
);
837 Put_Reduced_Keys
(Output
, "Key Table");
838 Put_Edges
(Output
, "Edge Table");
839 Put_Vertex_Table
(Output
, "Vertex Table");
842 end Compute_Edges_And_Vertices
;
850 Item_Size
: out Natural;
851 Length_1
: out Natural;
852 Length_2
: out Natural)
856 when Character_Position
=>
858 Length_1
:= Char_Pos_Set_Len
;
861 when Used_Character_Set
=>
866 when Function_Table_1
867 | Function_Table_2
=>
868 Item_Size
:= Type_Size
(NV
);
873 Item_Size
:= Type_Size
(NK
);
883 procedure Finalize
is
892 Min_Key_Len
:= Max_Word_Length
;
895 ---------------------
896 -- Free_Tmp_Tables --
897 ---------------------
899 procedure Free_Tmp_Tables
is
905 Char_Pos_Set
:= No_Table
;
906 Char_Pos_Set_Len
:= 0;
908 Used_Char_Set
:= No_Table
;
909 Used_Char_Set_Len
:= 0;
923 Vertices
:= No_Table
;
927 ----------------------------
928 -- Generate_Mapping_Table --
929 ----------------------------
931 procedure Generate_Mapping_Table
935 Seed
: in out Natural)
938 for J
in 0 .. L1
- 1 loop
939 for K
in 0 .. L2
- 1 loop
941 Set_Table
(Tab
, J
, K
, Seed
mod NV
);
944 end Generate_Mapping_Table
;
946 -----------------------------
947 -- Generate_Mapping_Tables --
948 -----------------------------
950 procedure Generate_Mapping_Tables
952 Seed
: in out Natural)
955 -- If T1 and T2 are already allocated no need to do it twice. Reuse them
956 -- as their size has not changed.
958 if T1
= No_Table
and then T2
= No_Table
then
960 Used_Char_Last
: Natural := 0;
964 if Opt
= CPU_Time
then
965 for P
in reverse Character'Range loop
966 Used_Char
:= Get_Used_Char
(P
);
967 if Used_Char
/= 0 then
968 Used_Char_Last
:= Used_Char
;
974 T1_Len
:= Char_Pos_Set_Len
;
975 T2_Len
:= Used_Char_Last
+ 1;
976 T1
:= Allocate
(T1_Len
* T2_Len
);
977 T2
:= Allocate
(T1_Len
* T2_Len
);
981 Generate_Mapping_Table
(T1
, T1_Len
, T2_Len
, Seed
);
982 Generate_Mapping_Table
(T2
, T1_Len
, T2_Len
, Seed
);
985 Put_Used_Char_Set
(Output
, "Used Character Set");
986 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
988 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
991 end Generate_Mapping_Tables
;
997 function Get_Char_Pos
(P
: Natural) return Natural is
998 N
: constant Natural := Char_Pos_Set
+ P
;
1000 return IT
.Table
(N
);
1007 function Get_Edges
(F
: Natural) return Edge_Type
is
1008 N
: constant Natural := Edges
+ (F
* Edge_Size
);
1011 E
.X
:= IT
.Table
(N
);
1012 E
.Y
:= IT
.Table
(N
+ 1);
1013 E
.Key
:= IT
.Table
(N
+ 2);
1021 function Get_Graph
(N
: Natural) return Integer is
1023 return IT
.Table
(G
+ N
);
1030 function Get_Key
(N
: Key_Id
) return Key_Type
is
1033 K
.Edge
:= IT
.Table
(Keys
+ N
);
1041 function Get_Table
(T
: Integer; X
, Y
: Natural) return Natural is
1042 N
: constant Natural := T
+ (Y
* T1_Len
) + X
;
1044 return IT
.Table
(N
);
1051 function Get_Used_Char
(C
: Character) return Natural is
1052 N
: constant Natural := Used_Char_Set
+ Character'Pos (C
);
1054 return IT
.Table
(N
);
1061 function Get_Vertices
(F
: Natural) return Vertex_Type
is
1062 N
: constant Natural := Vertices
+ (F
* Vertex_Size
);
1065 V
.First
:= IT
.Table
(N
);
1066 V
.Last
:= IT
.Table
(N
+ 1);
1074 function Image
(Int
: Integer; W
: Natural := 0) return String is
1075 B
: String (1 .. 32);
1078 procedure Img
(V
: Natural);
1079 -- Compute image of V into B, starting at B (L), incrementing L
1085 procedure Img
(V
: Natural) is
1092 B
(L
) := Character'Val ((V
mod 10) + Character'Pos ('0'));
1095 -- Start of processing for Image
1106 return Image
(B
(1 .. L
), W
);
1113 function Image
(Str
: String; W
: Natural := 0) return String is
1114 Len
: constant Natural := Str
'Length;
1115 Max
: Natural := Len
;
1123 Buf
: String (1 .. Max
) := (1 .. Max
=> ' ');
1126 for J
in 0 .. Len
- 1 loop
1127 Buf
(Max
- Len
+ 1 + J
) := Str
(Str
'First + J
);
1138 function Initial
(K
: Key_Id
) return Word_Id
is
1147 procedure Initialize
1149 K_To_V
: Float := Default_K_To_V
;
1150 Optim
: Optimization
:= CPU_Time
;
1151 Tries
: Positive := Default_Tries
)
1154 -- Free previous tables (the settings may have changed between two runs)
1158 if K_To_V
<= 2.0 then
1159 Put
(Output
, "K to V ratio cannot be lower than 2.0");
1161 raise Program_Error
;
1177 Word
: Word_Type
:= Null_Word
;
1178 Len
: constant Natural := Value
'Length;
1181 Word
(1 .. Len
) := Value
(Value
'First .. Value
'First + Len
- 1);
1183 WT
.Table
(NK
) := Word
;
1185 NV
:= Natural (Float (NK
) * K2V
);
1187 -- Do not accept a value of K2V too close to 2.0 such that once rounded
1188 -- up, NV = 2 * NK because the algorithm would not converge.
1190 if NV
<= 2 * NK
then
1194 if Max_Key_Len
< Len
then
1198 if Len
< Min_Key_Len
then
1207 procedure New_Line
(File
: File_Descriptor
) is
1209 if Write
(File
, EOL
'Address, 1) /= 1 then
1210 raise Program_Error
;
1214 ------------------------------
1215 -- Parse_Position_Selection --
1216 ------------------------------
1218 procedure Parse_Position_Selection
(Argument
: String) is
1219 N
: Natural := Argument
'First;
1220 L
: constant Natural := Argument
'Last;
1221 M
: constant Natural := Max_Key_Len
;
1223 T
: array (1 .. M
) of Boolean := (others => False);
1225 function Parse_Index
return Natural;
1226 -- Parse argument starting at index N to find an index
1232 function Parse_Index
return Natural is
1233 C
: Character := Argument
(N
);
1242 if C
not in '0' .. '9' then
1244 (Program_Error
'Identity, "cannot read position argument");
1247 while C
in '0' .. '9' loop
1248 V
:= V
* 10 + (Character'Pos (C
) - Character'Pos ('0'));
1257 -- Start of processing for Parse_Position_Selection
1261 -- Empty specification means all the positions
1264 Char_Pos_Set_Len
:= M
;
1265 Char_Pos_Set
:= Allocate
(Char_Pos_Set_Len
);
1267 for C
in 0 .. Char_Pos_Set_Len
- 1 loop
1268 Set_Char_Pos
(C
, C
+ 1);
1274 First
, Last
: Natural;
1277 First
:= Parse_Index
;
1282 if N
<= L
and then Argument
(N
) = '-' then
1284 Last
:= Parse_Index
;
1287 -- Include the positions in the selection
1289 for J
in First
.. Last
loop
1296 if Argument
(N
) /= ',' then
1298 (Program_Error
'Identity, "cannot read position argument");
1304 -- Compute position selection length
1307 for J
in T
'Range loop
1313 -- Fill position selection
1315 Char_Pos_Set_Len
:= N
;
1316 Char_Pos_Set
:= Allocate
(Char_Pos_Set_Len
);
1319 for J
in T
'Range loop
1321 Set_Char_Pos
(N
, J
);
1326 end Parse_Position_Selection
;
1332 procedure Produce
(Pkg_Name
: String := Default_Pkg_Name
) is
1333 File
: File_Descriptor
;
1336 -- For call to Close
1338 function Array_Img
(N
, T
, R1
: String; R2
: String := "") return String;
1339 -- Return string "N : constant array (R1[, R2]) of T;"
1341 function Range_Img
(F
, L
: Natural; T
: String := "") return String;
1342 -- Return string "[T range ]F .. L"
1344 function Type_Img
(L
: Natural) return String;
1345 -- Return the larger unsigned type T such that T'Last < L
1353 R2
: String := "") return String
1359 Add
(" : constant array (");
1370 return Line
(1 .. Last
);
1377 function Range_Img
(F
, L
: Natural; T
: String := "") return String is
1378 FI
: constant String := Image
(F
);
1379 FL
: constant Natural := FI
'Length;
1380 LI
: constant String := Image
(L
);
1381 LL
: constant Natural := LI
'Length;
1382 TL
: constant Natural := T
'Length;
1383 RI
: String (1 .. TL
+ 7 + FL
+ 4 + LL
);
1388 RI
(Len
+ 1 .. Len
+ TL
) := T
;
1390 RI
(Len
+ 1 .. Len
+ 7) := " range ";
1394 RI
(Len
+ 1 .. Len
+ FL
) := FI
;
1396 RI
(Len
+ 1 .. Len
+ 4) := " .. ";
1398 RI
(Len
+ 1 .. Len
+ LL
) := LI
;
1400 return RI
(1 .. Len
);
1407 function Type_Img
(L
: Natural) return String is
1408 S
: constant String := Image
(Type_Size
(L
));
1409 U
: String := "Unsigned_ ";
1413 for J
in S
'Range loop
1425 PLen
: constant Natural := Pkg_Name
'Length;
1426 FName
: String (1 .. PLen
+ 4);
1428 -- Start of processing for Produce
1431 FName
(1 .. PLen
) := Pkg_Name
;
1432 for J
in 1 .. PLen
loop
1433 if FName
(J
) in 'A' .. 'Z' then
1434 FName
(J
) := Character'Val (Character'Pos (FName
(J
))
1435 - Character'Pos ('A')
1436 + Character'Pos ('a'));
1438 elsif FName
(J
) = '.' then
1443 FName
(PLen
+ 1 .. PLen
+ 4) := ".ads";
1445 File
:= Create_File
(FName
, Text
);
1446 Put
(File
, "package ");
1447 Put
(File
, Pkg_Name
);
1450 Put
(File
, " function Hash (S : String) return Natural;");
1453 Put
(File
, Pkg_Name
);
1456 Close
(File
, Status
);
1462 FName
(PLen
+ 4) := 'b';
1464 File
:= Create_File
(FName
, Text
);
1465 Put
(File
, "with Interfaces; use Interfaces;");
1468 Put
(File
, "package body ");
1469 Put
(File
, Pkg_Name
);
1474 if Opt
= CPU_Time
then
1475 Put
(File
, Array_Img
("C", Type_Img
(256), "Character"));
1478 F
:= Character'Pos (Character'First);
1479 L
:= Character'Pos (Character'Last);
1481 for J
in Character'Range loop
1482 P
:= Get_Used_Char
(J
);
1483 Put
(File
, Image
(P
), 1, 0, 1, F
, L
, Character'Pos (J
));
1490 L
:= Char_Pos_Set_Len
- 1;
1492 Put
(File
, Array_Img
("P", "Natural", Range_Img
(F
, L
)));
1495 for J
in F
.. L
loop
1496 Put
(File
, Image
(Get_Char_Pos
(J
)), 1, 0, 1, F
, L
, J
);
1501 if Opt
= CPU_Time
then
1504 Array_Img
("T1", Type_Img
(NV
),
1505 Range_Img
(0, T1_Len
- 1),
1506 Range_Img
(0, T2_Len
- 1, Type_Img
(256))),
1507 T1
, T1_Len
, T2_Len
);
1512 Array_Img
("T1", Type_Img
(NV
),
1513 Range_Img
(0, T1_Len
- 1)),
1519 if Opt
= CPU_Time
then
1522 Array_Img
("T2", Type_Img
(NV
),
1523 Range_Img
(0, T1_Len
- 1),
1524 Range_Img
(0, T2_Len
- 1, Type_Img
(256))),
1525 T2
, T1_Len
, T2_Len
);
1530 Array_Img
("T2", Type_Img
(NV
),
1531 Range_Img
(0, T1_Len
- 1)),
1539 Array_Img
("G", Type_Img
(NK
),
1540 Range_Img
(0, G_Len
- 1)),
1544 Put
(File
, " function Hash (S : String) return Natural is");
1546 Put
(File
, " F : constant Natural := S'First - 1;");
1548 Put
(File
, " L : constant Natural := S'Length;");
1550 Put
(File
, " F1, F2 : Natural := 0;");
1553 Put
(File
, " J : ");
1555 if Opt
= CPU_Time
then
1556 Put
(File
, Type_Img
(256));
1558 Put
(File
, "Natural");
1564 Put
(File
, " begin");
1566 Put
(File
, " for K in P'Range loop");
1568 Put
(File
, " exit when L < P (K);");
1570 Put
(File
, " J := ");
1572 if Opt
= CPU_Time
then
1575 Put
(File
, "Character'Pos");
1578 Put
(File
, " (S (P (K) + F));");
1581 Put
(File
, " F1 := (F1 + Natural (T1 (K");
1583 if Opt
= CPU_Time
then
1589 if Opt
= Memory_Space
then
1593 Put
(File
, ") mod ");
1594 Put
(File
, Image
(NV
));
1598 Put
(File
, " F2 := (F2 + Natural (T2 (K");
1600 if Opt
= CPU_Time
then
1606 if Opt
= Memory_Space
then
1610 Put
(File
, ") mod ");
1611 Put
(File
, Image
(NV
));
1615 Put
(File
, " end loop;");
1619 " return (Natural (G (F1)) + Natural (G (F2))) mod ");
1621 Put
(File
, Image
(NK
));
1624 Put
(File
, " end Hash;");
1628 Put
(File
, Pkg_Name
);
1631 Close
(File
, Status
);
1642 procedure Put
(File
: File_Descriptor
; Str
: String) is
1643 Len
: constant Natural := Str
'Length;
1646 if Write
(File
, Str
'Address, Len
) /= Len
then
1647 raise Program_Error
;
1656 (F
: File_Descriptor
;
1665 Len
: constant Natural := S
'Length;
1668 -- Write current line, followed by LF
1676 Put
(F
, Line
(1 .. Last
));
1681 -- Start of processing for Put
1684 if C1
= F1
and then C2
= F2
then
1688 if Last
+ Len
+ 3 > Max
then
1693 Line
(Last
+ 1 .. Last
+ 5) := " ";
1697 if C1
= F1
and then C2
= F2
then
1717 Line
(Last
+ 1 .. Last
+ Len
) := S
;
1745 (File
: File_Descriptor
;
1749 F1
: constant Natural := 1;
1750 L1
: constant Natural := Edges_Len
- 1;
1751 M
: constant Natural := Max
/ 5;
1757 -- Edges valid range is 1 .. Edge_Len - 1
1759 for J
in F1
.. L1
loop
1761 Put
(File
, Image
(J
, M
), F1
, L1
, J
, 1, 4, 1);
1762 Put
(File
, Image
(E
.X
, M
), F1
, L1
, J
, 1, 4, 2);
1763 Put
(File
, Image
(E
.Y
, M
), F1
, L1
, J
, 1, 4, 3);
1764 Put
(File
, Image
(E
.Key
, M
), F1
, L1
, J
, 1, 4, 4);
1768 ----------------------
1769 -- Put_Initial_Keys --
1770 ----------------------
1772 procedure Put_Initial_Keys
1773 (File
: File_Descriptor
;
1776 F1
: constant Natural := 0;
1777 L1
: constant Natural := NK
- 1;
1778 M
: constant Natural := Max
/ 5;
1785 for J
in F1
.. L1
loop
1787 Put
(File
, Image
(J
, M
), F1
, L1
, J
, 1, 3, 1);
1788 Put
(File
, Image
(K
.Edge
, M
), F1
, L1
, J
, 1, 3, 2);
1789 Put
(File
, WT
.Table
(Initial
(J
)), F1
, L1
, J
, 1, 3, 3);
1791 end Put_Initial_Keys
;
1793 --------------------
1794 -- Put_Int_Matrix --
1795 --------------------
1797 procedure Put_Int_Matrix
1798 (File
: File_Descriptor
;
1804 F1
: constant Integer := 0;
1805 L1
: constant Integer := Len_1
- 1;
1806 F2
: constant Integer := 0;
1807 L2
: constant Integer := Len_2
- 1;
1815 for J
in F1
.. L1
loop
1816 I
:= IT
.Table
(Table
+ J
);
1817 Put
(File
, Image
(I
), 1, 0, 1, F1
, L1
, J
);
1821 for J
in F1
.. L1
loop
1822 for K
in F2
.. L2
loop
1823 I
:= IT
.Table
(Table
+ J
+ K
* Len_1
);
1824 Put
(File
, Image
(I
), F1
, L1
, J
, F2
, L2
, K
);
1830 --------------------
1831 -- Put_Int_Vector --
1832 --------------------
1834 procedure Put_Int_Vector
1835 (File
: File_Descriptor
;
1840 F2
: constant Natural := 0;
1841 L2
: constant Natural := Length
- 1;
1847 for J
in F2
.. L2
loop
1848 Put
(File
, Image
(IT
.Table
(Vector
+ J
)), 1, 0, 1, F2
, L2
, J
);
1852 ----------------------
1853 -- Put_Reduced_Keys --
1854 ----------------------
1856 procedure Put_Reduced_Keys
1857 (File
: File_Descriptor
;
1860 F1
: constant Natural := 0;
1861 L1
: constant Natural := NK
- 1;
1862 M
: constant Natural := Max
/ 5;
1869 for J
in F1
.. L1
loop
1871 Put
(File
, Image
(J
, M
), F1
, L1
, J
, 1, 3, 1);
1872 Put
(File
, Image
(K
.Edge
, M
), F1
, L1
, J
, 1, 3, 2);
1873 Put
(File
, WT
.Table
(Reduced
(J
)), F1
, L1
, J
, 1, 3, 3);
1875 end Put_Reduced_Keys
;
1877 -----------------------
1878 -- Put_Used_Char_Set --
1879 -----------------------
1881 procedure Put_Used_Char_Set
1882 (File
: File_Descriptor
;
1885 F
: constant Natural := Character'Pos (Character'First);
1886 L
: constant Natural := Character'Pos (Character'Last);
1892 for J
in Character'Range loop
1894 (File
, Image
(Get_Used_Char
(J
)), 1, 0, 1, F
, L
, Character'Pos (J
));
1896 end Put_Used_Char_Set
;
1898 ----------------------
1899 -- Put_Vertex_Table --
1900 ----------------------
1902 procedure Put_Vertex_Table
1903 (File
: File_Descriptor
;
1906 F1
: constant Natural := 0;
1907 L1
: constant Natural := NV
- 1;
1908 M
: constant Natural := Max
/ 4;
1915 for J
in F1
.. L1
loop
1916 V
:= Get_Vertices
(J
);
1917 Put
(File
, Image
(J
, M
), F1
, L1
, J
, 1, 3, 1);
1918 Put
(File
, Image
(V
.First
, M
), F1
, L1
, J
, 1, 3, 2);
1919 Put
(File
, Image
(V
.Last
, M
), F1
, L1
, J
, 1, 3, 3);
1921 end Put_Vertex_Table
;
1927 procedure Random
(Seed
: in out Natural)
1929 -- Park & Miller Standard Minimal using Schrage's algorithm to avoid
1930 -- overflow: Xn+1 = 16807 * Xn mod (2 ** 31 - 1)
1937 R
:= Seed
mod 127773;
1939 X
:= 16807 * R
- 2836 * Q
;
1942 Seed
:= X
+ 2147483647;
1952 function Reduced
(K
: Key_Id
) return Word_Id
is
1957 --------------------------
1958 -- Select_Char_Position --
1959 --------------------------
1961 procedure Select_Char_Position
is
1963 type Vertex_Table_Type
is array (Natural range <>) of Vertex_Type
;
1965 procedure Build_Identical_Keys_Sets
1966 (Table
: in out Vertex_Table_Type
;
1967 Last
: in out Natural;
1969 -- Build a list of keys subsets that are identical with the current
1970 -- position selection plus Pos. Once this routine is called, reduced
1971 -- words are sorted by subsets and each item (First, Last) in Sets
1972 -- defines the range of identical keys.
1974 function Count_Different_Keys
1975 (Table
: Vertex_Table_Type
;
1977 Pos
: Natural) return Natural;
1978 -- For each subset in Sets, count the number of different keys if we add
1979 -- Pos to the current position selection.
1981 Sel_Position
: IT
.Table_Type
(1 .. Max_Key_Len
);
1982 Last_Sel_Pos
: Natural := 0;
1983 Max_Sel_Pos
: Natural := 0;
1985 -------------------------------
1986 -- Build_Identical_Keys_Sets --
1987 -------------------------------
1989 procedure Build_Identical_Keys_Sets
1990 (Table
: in out Vertex_Table_Type
;
1991 Last
: in out Natural;
1994 S
: constant Vertex_Table_Type
:= Table
(1 .. Last
);
1995 C
: constant Natural := Pos
;
2000 -- First and last words of a subset
2003 -- GNAT.Heap_Sort assumes that the first array index is 1. Offset
2004 -- defines the translation to operate.
2006 function Lt
(L
, R
: Natural) return Boolean;
2007 procedure Move
(From
: Natural; To
: Natural);
2008 -- Subprograms needed by GNAT.Heap_Sort_A
2014 function Lt
(L
, R
: Natural) return Boolean is
2015 C
: constant Natural := Pos
;
2021 Left
:= Reduced
(0) - 1;
2022 Right
:= Offset
+ R
;
2025 Right
:= Reduced
(0) - 1;
2028 Right
:= Offset
+ R
;
2031 return WT
.Table
(Left
)(C
) < WT
.Table
(Right
)(C
);
2038 procedure Move
(From
: Natural; To
: Natural) is
2039 Target
, Source
: Natural;
2043 Source
:= Reduced
(0) - 1;
2044 Target
:= Offset
+ To
;
2046 Source
:= Offset
+ From
;
2047 Target
:= Reduced
(0) - 1;
2049 Source
:= Offset
+ From
;
2050 Target
:= Offset
+ To
;
2053 WT
.Table
(Target
) := WT
.Table
(Source
);
2056 -- Start of processing for Build_Identical_Key_Sets
2061 -- For each subset in S, extract the new subsets we have by adding C
2062 -- in the position selection.
2064 for J
in S
'Range loop
2065 if S
(J
).First
= S
(J
).Last
then
2069 Table
(Last
) := (F
, L
);
2072 Offset
:= Reduced
(S
(J
).First
) - 1;
2074 (S
(J
).Last
- S
(J
).First
+ 1,
2075 Move
'Unrestricted_Access,
2076 Lt
'Unrestricted_Access);
2080 for N
in S
(J
).First
.. S
(J
).Last
loop
2082 -- For the last item, close the last subset
2084 if N
= S
(J
).Last
then
2086 Table
(Last
) := (F
, N
);
2088 -- Two contiguous words are identical when they have the
2089 -- same Cth character.
2091 elsif WT
.Table
(Reduced
(N
))(C
) =
2092 WT
.Table
(Reduced
(N
+ 1))(C
)
2096 -- Find a new subset of identical keys. Store the current
2097 -- one and create a new subset.
2101 Table
(Last
) := (F
, L
);
2108 end Build_Identical_Keys_Sets
;
2110 --------------------------
2111 -- Count_Different_Keys --
2112 --------------------------
2114 function Count_Different_Keys
2115 (Table
: Vertex_Table_Type
;
2117 Pos
: Natural) return Natural
2119 N
: array (Character) of Natural;
2124 -- For each subset, count the number of words that are still
2125 -- different when we include Pos in the position selection. Only
2126 -- focus on this position as the other positions already produce
2129 for S
in 1 .. Last
loop
2131 -- Count the occurrences of the different characters
2134 for K
in Table
(S
).First
.. Table
(S
).Last
loop
2135 C
:= WT
.Table
(Reduced
(K
))(Pos
);
2139 -- Update the number of different keys. Each character used
2140 -- denotes a different key.
2142 for J
in N
'Range loop
2150 end Count_Different_Keys
;
2152 -- Start of processing for Select_Char_Position
2155 -- Initialize the reduced words set
2157 WT
.Set_Last
(2 * NK
);
2158 for K
in 0 .. NK
- 1 loop
2159 WT
.Table
(Reduced
(K
)) := WT
.Table
(Initial
(K
));
2163 Differences
: Natural;
2164 Max_Differences
: Natural := 0;
2165 Old_Differences
: Natural;
2166 Max_Diff_Sel_Pos
: Natural := 0; -- init to kill warning
2167 Max_Diff_Sel_Pos_Idx
: Natural := 0; -- init to kill warning
2168 Same_Keys_Sets_Table
: Vertex_Table_Type
(1 .. NK
);
2169 Same_Keys_Sets_Last
: Natural := 1;
2172 for C
in Sel_Position
'Range loop
2173 Sel_Position
(C
) := C
;
2176 Same_Keys_Sets_Table
(1) := (0, NK
- 1);
2179 -- Preserve maximum number of different keys and check later on
2180 -- that this value is strictly incrementing. Otherwise, it means
2181 -- that two keys are stricly identical.
2183 Old_Differences
:= Max_Differences
;
2185 -- The first position should not exceed the minimum key length.
2186 -- Otherwise, we may end up with an empty word once reduced.
2188 if Last_Sel_Pos
= 0 then
2189 Max_Sel_Pos
:= Min_Key_Len
;
2191 Max_Sel_Pos
:= Max_Key_Len
;
2194 -- Find which position increases more the number of differences
2196 for J
in Last_Sel_Pos
+ 1 .. Max_Sel_Pos
loop
2197 Differences
:= Count_Different_Keys
2198 (Same_Keys_Sets_Table
,
2199 Same_Keys_Sets_Last
,
2204 "Selecting position" & Sel_Position
(J
)'Img &
2205 " results in" & Differences
'Img &
2210 if Differences
> Max_Differences
then
2211 Max_Differences
:= Differences
;
2212 Max_Diff_Sel_Pos
:= Sel_Position
(J
);
2213 Max_Diff_Sel_Pos_Idx
:= J
;
2217 if Old_Differences
= Max_Differences
then
2219 (Program_Error
'Identity, "some keys are identical");
2222 -- Insert selected position and sort Sel_Position table
2224 Last_Sel_Pos
:= Last_Sel_Pos
+ 1;
2225 Sel_Position
(Last_Sel_Pos
+ 1 .. Max_Diff_Sel_Pos_Idx
) :=
2226 Sel_Position
(Last_Sel_Pos
.. Max_Diff_Sel_Pos_Idx
- 1);
2227 Sel_Position
(Last_Sel_Pos
) := Max_Diff_Sel_Pos
;
2229 for P
in 1 .. Last_Sel_Pos
- 1 loop
2230 if Max_Diff_Sel_Pos
< Sel_Position
(P
) then
2231 Sel_Position
(P
+ 1 .. Last_Sel_Pos
) :=
2232 Sel_Position
(P
.. Last_Sel_Pos
- 1);
2233 Sel_Position
(P
) := Max_Diff_Sel_Pos
;
2238 exit when Max_Differences
= NK
;
2240 Build_Identical_Keys_Sets
2241 (Same_Keys_Sets_Table
,
2242 Same_Keys_Sets_Last
,
2247 "Selecting position" & Max_Diff_Sel_Pos
'Img &
2248 " results in" & Max_Differences
'Img &
2253 for J
in 1 .. Same_Keys_Sets_Last
loop
2255 Same_Keys_Sets_Table
(J
).First
..
2256 Same_Keys_Sets_Table
(J
).Last
2258 Put
(Output
, WT
.Table
(Reduced
(K
)));
2268 Char_Pos_Set_Len
:= Last_Sel_Pos
;
2269 Char_Pos_Set
:= Allocate
(Char_Pos_Set_Len
);
2271 for C
in 1 .. Last_Sel_Pos
loop
2272 Set_Char_Pos
(C
- 1, Sel_Position
(C
));
2274 end Select_Char_Position
;
2276 --------------------------
2277 -- Select_Character_Set --
2278 --------------------------
2280 procedure Select_Character_Set
2282 Last
: Natural := 0;
2283 Used
: array (Character) of Boolean := (others => False);
2287 for J
in 0 .. NK
- 1 loop
2288 for K
in 0 .. Char_Pos_Set_Len
- 1 loop
2289 Char
:= WT
.Table
(Initial
(J
))(Get_Char_Pos
(K
));
2290 exit when Char
= ASCII
.NUL
;
2291 Used
(Char
) := True;
2295 Used_Char_Set_Len
:= 256;
2296 Used_Char_Set
:= Allocate
(Used_Char_Set_Len
);
2298 for J
in Used
'Range loop
2300 Set_Used_Char
(J
, Last
);
2303 Set_Used_Char
(J
, 0);
2306 end Select_Character_Set
;
2312 procedure Set_Char_Pos
(P
: Natural; Item
: Natural) is
2313 N
: constant Natural := Char_Pos_Set
+ P
;
2315 IT
.Table
(N
) := Item
;
2322 procedure Set_Edges
(F
: Natural; Item
: Edge_Type
) is
2323 N
: constant Natural := Edges
+ (F
* Edge_Size
);
2325 IT
.Table
(N
) := Item
.X
;
2326 IT
.Table
(N
+ 1) := Item
.Y
;
2327 IT
.Table
(N
+ 2) := Item
.Key
;
2334 procedure Set_Graph
(N
: Natural; Item
: Integer) is
2336 IT
.Table
(G
+ N
) := Item
;
2343 procedure Set_Key
(N
: Key_Id
; Item
: Key_Type
) is
2345 IT
.Table
(Keys
+ N
) := Item
.Edge
;
2352 procedure Set_Table
(T
: Integer; X
, Y
: Natural; Item
: Natural) is
2353 N
: constant Natural := T
+ ((Y
* T1_Len
) + X
);
2355 IT
.Table
(N
) := Item
;
2362 procedure Set_Used_Char
(C
: Character; Item
: Natural) is
2363 N
: constant Natural := Used_Char_Set
+ Character'Pos (C
);
2365 IT
.Table
(N
) := Item
;
2372 procedure Set_Vertices
(F
: Natural; Item
: Vertex_Type
) is
2373 N
: constant Natural := Vertices
+ (F
* Vertex_Size
);
2375 IT
.Table
(N
) := Item
.First
;
2376 IT
.Table
(N
+ 1) := Item
.Last
;
2386 Opt
: Optimization
) return Natural
2392 if Opt
= CPU_Time
then
2393 for J
in 0 .. T1_Len
- 1 loop
2394 exit when Word
(J
+ 1) = ASCII
.NUL
;
2395 R
:= Get_Table
(Table
, J
, Get_Used_Char
(Word
(J
+ 1)));
2396 S
:= (S
+ R
) mod NV
;
2400 for J
in 0 .. T1_Len
- 1 loop
2401 exit when Word
(J
+ 1) = ASCII
.NUL
;
2402 R
:= Get_Table
(Table
, J
, 0);
2403 S
:= (S
+ R
* Character'Pos (Word
(J
+ 1))) mod NV
;
2414 function Type_Size
(L
: Natural) return Natural is
2418 elsif L
<= 2 ** 16 then
2432 K
: Natural := 0) return Natural
2436 when Character_Position
=>
2437 return Get_Char_Pos
(J
);
2439 when Used_Character_Set
=>
2440 return Get_Used_Char
(Character'Val (J
));
2442 when Function_Table_1
=>
2443 return Get_Table
(T1
, J
, K
);
2445 when Function_Table_2
=>
2446 return Get_Table
(T2
, J
, K
);
2449 return Get_Graph
(J
);
2454 end GNAT
.Perfect_Hash_Generators
;