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-2008, 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
53 -- a function that maps integers into [0, m-1]. h can be order preserving.
54 -- For instance, let W = {w_0, ..., w_i, ..., w_m-1}, h can be defined
55 -- 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 G is
86 -- acyclic, there is a vertex v1 with only one neighbor v2. Let w_i be
87 -- the word such that v1 = f1 (w_i) and v2 = f2 (w_i). Let g (v1) = 0 by
88 -- construction and g (v2) = (i - g (v1)) mod n (or 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 type Word_Type
is new String_Access
;
106 procedure Free_Word
(W
: in out Word_Type
);
107 function New_Word
(S
: String) return Word_Type
;
109 procedure Resize_Word
(W
: in out Word_Type
; Len
: Natural);
110 -- Resize string W to have a length Len
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. WT is used to store the words in their initial
135 -- version and in their reduced version (that is words reduced to their
136 -- significant characters). As an instance of GNAT.Table, WT does not
137 -- initialize string pointers to null. This initialization has to be done
138 -- manually when the table is allocated. IT is used to store several
139 -- tables of components containing only integers.
141 function Image
(Int
: Integer; W
: Natural := 0) return String;
142 function Image
(Str
: String; W
: Natural := 0) return String;
143 -- Return a string which includes string Str or integer Int preceded by
144 -- leading spaces if required by width W.
146 Output
: File_Descriptor
renames GNAT
.OS_Lib
.Standout
;
149 EOL
: constant Character := ASCII
.LF
;
151 Max
: constant := 78;
153 Line
: String (1 .. Max
);
154 -- Use this line to provide buffered IO
156 procedure Add
(C
: Character);
157 procedure Add
(S
: String);
158 -- Add a character or a string in Line and update Last
161 (F
: File_Descriptor
;
169 -- Write string S into file F as a element of an array of one or two
170 -- dimensions. Fk (resp. Lk and Ck) indicates the first (resp last and
171 -- current) index in the k-th dimension. If F1 = L1 the array is considered
172 -- as a one dimension array. This dimension is described by F2 and L2. This
173 -- routine takes care of all the parenthesis, spaces and commas needed to
174 -- format correctly the array. Moreover, the array is well indented and is
175 -- wrapped to fit in a 80 col line. When the line is full, the routine
176 -- writes it into file F. When the array is completed, the routine adds
177 -- semi-colon and writes the line into file F.
179 procedure New_Line
(File
: File_Descriptor
);
180 -- Simulate Ada.Text_IO.New_Line with GNAT.OS_Lib
182 procedure Put
(File
: File_Descriptor
; Str
: String);
183 -- Simulate Ada.Text_IO.Put with GNAT.OS_Lib
185 procedure Put_Used_Char_Set
(File
: File_Descriptor
; Title
: String);
186 -- Output a title and a used character set
188 procedure Put_Int_Vector
189 (File
: File_Descriptor
;
193 -- Output a title and a vector
195 procedure Put_Int_Matrix
196 (File
: File_Descriptor
;
201 -- Output a title and a matrix. When the matrix has only one non-empty
202 -- dimension (Len_2 = 0), output a vector.
204 procedure Put_Edges
(File
: File_Descriptor
; Title
: String);
205 -- Output a title and an edge table
207 procedure Put_Initial_Keys
(File
: File_Descriptor
; Title
: String);
208 -- Output a title and a key table
210 procedure Put_Reduced_Keys
(File
: File_Descriptor
; Title
: String);
211 -- Output a title and a key table
213 procedure Put_Vertex_Table
(File
: File_Descriptor
; Title
: String);
214 -- Output a title and a vertex table
216 ----------------------------------
217 -- Character Position Selection --
218 ----------------------------------
220 -- We reduce the maximum key size by selecting representative positions
221 -- in these keys. We build a matrix with one word per line. We fill the
222 -- remaining space of a line with ASCII.NUL. The heuristic selects the
223 -- position that induces the minimum number of collisions. If there are
224 -- collisions, select another position on the reduced key set responsible
225 -- of the collisions. Apply the heuristic until there is no more collision.
227 procedure Apply_Position_Selection
;
228 -- Apply Position selection and build the reduced key table
230 procedure Parse_Position_Selection
(Argument
: String);
231 -- Parse Argument and compute the position set. Argument is list of
232 -- substrings separated by commas. Each substring represents a position
233 -- or a range of positions (like x-y).
235 procedure Select_Character_Set
;
236 -- Define an optimized used character set like Character'Pos in order not
237 -- to allocate tables of 256 entries.
239 procedure Select_Char_Position
;
240 -- Find a min char position set in order to reduce the max key length. The
241 -- heuristic selects the position that induces the minimum number of
242 -- collisions. If there are collisions, select another position on the
243 -- reduced key set responsible of the collisions. Apply the heuristic until
244 -- there is no collision.
246 -----------------------------
247 -- Random Graph Generation --
248 -----------------------------
250 procedure Random
(Seed
: in out Natural);
251 -- Simulate Ada.Discrete_Numerics.Random
253 procedure Generate_Mapping_Table
257 Seed
: in out Natural);
258 -- Random generation of the tables below. T is already allocated
260 procedure Generate_Mapping_Tables
262 Seed
: in out Natural);
263 -- Generate the mapping tables T1 and T2. They are used to define fk (w) =
264 -- sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n. Keys, NK and Chars
265 -- are used to compute the matrix size.
267 ---------------------------
268 -- Algorithm Computation --
269 ---------------------------
271 procedure Compute_Edges_And_Vertices
(Opt
: Optimization
);
272 -- Compute the edge and vertex tables. These are empty when a self loop is
273 -- detected (f1 (w) = f2 (w)). The edge table is sorted by X value and then
274 -- Y value. Keys is the key table and NK the number of keys. Chars is the
275 -- set of characters really used in Keys. NV is the number of vertices
276 -- recommended by the algorithm. T1 and T2 are the mapping tables needed to
277 -- compute f1 (w) and f2 (w).
279 function Acyclic
return Boolean;
280 -- Return True when the graph is acyclic. Vertices is the current vertex
281 -- table and Edges the current edge table.
283 procedure Assign_Values_To_Vertices
;
284 -- Execute the assignment step of the algorithm. Keys is the current key
285 -- table. Vertices and Edges represent the random graph. G is the result of
286 -- the assignment step such that:
287 -- h (w) = (g (f1 (w)) + g (f2 (w))) mod m
292 Opt
: Optimization
) return Natural;
293 -- For an optimization of CPU_Time return
294 -- fk (w) = sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n
295 -- For an optimization of Memory_Space return
296 -- fk (w) = sum (for i in 1 .. length (w)) (Tk (i) * w (i)) mod n
299 -------------------------------
300 -- Internal Table Management --
301 -------------------------------
303 function Allocate
(N
: Natural; S
: Natural := 1) return Table_Id
;
304 -- Allocate N * S ints from IT table
310 Keys
: Table_Id
:= No_Table
;
312 -- NK : Number of Keys
314 function Initial
(K
: Key_Id
) return Word_Id
;
315 pragma Inline
(Initial
);
317 function Reduced
(K
: Key_Id
) return Word_Id
;
318 pragma Inline
(Reduced
);
320 function Get_Key
(N
: Key_Id
) return Key_Type
;
321 procedure Set_Key
(N
: Key_Id
; Item
: Key_Type
);
322 -- Get or Set Nth element of Keys table
328 Char_Pos_Set
: Table_Id
:= No_Table
;
329 Char_Pos_Set_Len
: Natural;
330 -- Character Selected Position Set
332 function Get_Char_Pos
(P
: Natural) return Natural;
333 procedure Set_Char_Pos
(P
: Natural; Item
: Natural);
334 -- Get or Set the string position of the Pth selected character
340 Used_Char_Set
: Table_Id
:= No_Table
;
341 Used_Char_Set_Len
: Natural;
342 -- Used Character Set : Define a new character mapping. When all the
343 -- characters are not present in the keys, in order to reduce the size
344 -- of some tables, we redefine the character mapping.
346 function Get_Used_Char
(C
: Character) return Natural;
347 procedure Set_Used_Char
(C
: Character; Item
: Natural);
353 T1
: Table_Id
:= No_Table
;
354 T2
: Table_Id
:= No_Table
;
357 -- T1 : Values table to compute F1
358 -- T2 : Values table to compute F2
360 function Get_Table
(T
: Integer; X
, Y
: Natural) return Natural;
361 procedure Set_Table
(T
: Integer; X
, Y
: Natural; Item
: Natural);
367 G
: Table_Id
:= No_Table
;
369 -- Values table to compute G
371 NT
: Natural := Default_Tries
;
372 -- Number of tries running the algorithm before raising an error
374 function Get_Graph
(N
: Natural) return Integer;
375 procedure Set_Graph
(N
: Natural; Item
: Integer);
376 -- Get or Set Nth element of graph
382 Edge_Size
: constant := 3;
383 Edges
: Table_Id
:= No_Table
;
385 -- Edges : Edge table of the random graph G
387 function Get_Edges
(F
: Natural) return Edge_Type
;
388 procedure Set_Edges
(F
: Natural; Item
: Edge_Type
);
394 Vertex_Size
: constant := 2;
396 Vertices
: Table_Id
:= No_Table
;
397 -- Vertex table of the random graph G
400 -- Number of Vertices
402 function Get_Vertices
(F
: Natural) return Vertex_Type
;
403 procedure Set_Vertices
(F
: Natural; Item
: Vertex_Type
);
404 -- Comments needed ???
407 -- Ratio between Keys and Vertices (parameter of Czech's algorithm)
410 -- Optimization mode (memory vs CPU)
412 Max_Key_Len
: Natural := 0;
413 Min_Key_Len
: Natural := 0;
414 -- Maximum and minimum of all the word length
419 function Type_Size
(L
: Natural) return Natural;
420 -- Given the last L of an unsigned integer type T, return its size
426 function Acyclic
return Boolean is
427 Marks
: array (0 .. NV
- 1) of Vertex_Id
:= (others => No_Vertex
);
429 function Traverse
(Edge
: Edge_Id
; Mark
: Vertex_Id
) return Boolean;
430 -- Propagate Mark from X to Y. X is already marked. Mark Y and propagate
431 -- it to the edges of Y except the one representing the same key. Return
432 -- False when Y is marked with Mark.
438 function Traverse
(Edge
: Edge_Id
; Mark
: Vertex_Id
) return Boolean is
439 E
: constant Edge_Type
:= Get_Edges
(Edge
);
440 K
: constant Key_Id
:= E
.Key
;
441 Y
: constant Vertex_Id
:= E
.Y
;
442 M
: constant Vertex_Id
:= Marks
(E
.Y
);
449 elsif M
= No_Vertex
then
451 V
:= Get_Vertices
(Y
);
453 for J
in V
.First
.. V
.Last
loop
455 -- Do not propagate to the edge representing the same key
457 if Get_Edges
(J
).Key
/= K
458 and then not Traverse
(J
, Mark
)
470 -- Start of processing for Acyclic
473 -- Edges valid range is
475 for J
in 1 .. Edges_Len
- 1 loop
477 Edge
:= Get_Edges
(J
);
479 -- Mark X of E when it has not been already done
481 if Marks
(Edge
.X
) = No_Vertex
then
482 Marks
(Edge
.X
) := Edge
.X
;
485 -- Traverse E when this has not already been done
487 if Marks
(Edge
.Y
) = No_Vertex
488 and then not Traverse
(J
, Edge
.X
)
501 procedure Add
(C
: Character) is
503 Line
(Last
+ 1) := C
;
511 procedure Add
(S
: String) is
512 Len
: constant Natural := S
'Length;
514 Line
(Last
+ 1 .. Last
+ Len
) := S
;
522 function Allocate
(N
: Natural; S
: Natural := 1) return Table_Id
is
523 L
: constant Integer := IT
.Last
;
525 IT
.Set_Last
(L
+ N
* S
);
529 ------------------------------
530 -- Apply_Position_Selection --
531 ------------------------------
533 procedure Apply_Position_Selection
is
535 for J
in 0 .. NK
- 1 loop
537 IW
: constant String := WT
.Table
(Initial
(J
)).all;
538 RW
: String (1 .. IW
'Length) := (others => ASCII
.NUL
);
539 N
: Natural := IW
'First - 1;
542 -- Select the characters of Word included in the position
545 for C
in 0 .. Char_Pos_Set_Len
- 1 loop
546 exit when IW
(Get_Char_Pos
(C
)) = ASCII
.NUL
;
548 RW
(N
) := IW
(Get_Char_Pos
(C
));
551 -- Build the new table with the reduced word. Be careful
552 -- to deallocate the old version to avoid memory leaks.
554 Free_Word
(WT
.Table
(Reduced
(J
)));
555 WT
.Table
(Reduced
(J
)) := New_Word
(RW
);
556 Set_Key
(J
, (Edge
=> No_Edge
));
559 end Apply_Position_Selection
;
561 -------------------------------
562 -- Assign_Values_To_Vertices --
563 -------------------------------
565 procedure Assign_Values_To_Vertices
is
568 procedure Assign
(X
: Vertex_Id
);
569 -- Execute assignment on X's neighbors except the vertex that we are
570 -- coming from which is already assigned.
576 procedure Assign
(X
: Vertex_Id
) is
578 V
: constant Vertex_Type
:= Get_Vertices
(X
);
581 for J
in V
.First
.. V
.Last
loop
584 if Get_Graph
(E
.Y
) = -1 then
585 Set_Graph
(E
.Y
, (E
.Key
- Get_Graph
(X
)) mod NK
);
591 -- Start of processing for Assign_Values_To_Vertices
594 -- Value -1 denotes an uninitialized value as it is supposed to
595 -- be in the range 0 .. NK.
599 G
:= Allocate
(G_Len
, 1);
602 for J
in 0 .. G_Len
- 1 loop
606 for K
in 0 .. NK
- 1 loop
607 X
:= Get_Edges
(Get_Key
(K
).Edge
).X
;
609 if Get_Graph
(X
) = -1 then
615 for J
in 0 .. G_Len
- 1 loop
616 if Get_Graph
(J
) = -1 then
622 Put_Int_Vector
(Output
, "Assign Values To Vertices", G
, G_Len
);
624 end Assign_Values_To_Vertices
;
630 procedure Compute
(Position
: String := Default_Position
) is
631 Success
: Boolean := False;
635 raise Program_Error
with "keywords set cannot be empty";
639 Put_Initial_Keys
(Output
, "Initial Key Table");
642 if Position
'Length /= 0 then
643 Parse_Position_Selection
(Position
);
645 Select_Char_Position
;
650 (Output
, "Char Position Set", Char_Pos_Set
, Char_Pos_Set_Len
);
653 Apply_Position_Selection
;
656 Put_Reduced_Keys
(Output
, "Reduced Keys Table");
659 Select_Character_Set
;
662 Put_Used_Char_Set
(Output
, "Character Position Table");
665 -- Perform Czech's algorithm
667 for J
in 1 .. NT
loop
668 Generate_Mapping_Tables
(Opt
, S
);
669 Compute_Edges_And_Vertices
(Opt
);
671 -- When graph is not empty (no self-loop from previous operation) and
674 if 0 < Edges_Len
and then Acyclic
then
681 raise Too_Many_Tries
;
684 Assign_Values_To_Vertices
;
687 --------------------------------
688 -- Compute_Edges_And_Vertices --
689 --------------------------------
691 procedure Compute_Edges_And_Vertices
(Opt
: Optimization
) is
696 Vertex
: Vertex_Type
;
697 Not_Acyclic
: Boolean := False;
699 procedure Move
(From
: Natural; To
: Natural);
700 function Lt
(L
, R
: Natural) return Boolean;
701 -- Subprograms needed for GNAT.Heap_Sort_G
707 function Lt
(L
, R
: Natural) return Boolean is
708 EL
: constant Edge_Type
:= Get_Edges
(L
);
709 ER
: constant Edge_Type
:= Get_Edges
(R
);
711 return EL
.X
< ER
.X
or else (EL
.X
= ER
.X
and then EL
.Y
< ER
.Y
);
718 procedure Move
(From
: Natural; To
: Natural) is
720 Set_Edges
(To
, Get_Edges
(From
));
723 package Sorting
is new GNAT
.Heap_Sort_G
(Move
, Lt
);
725 -- Start of processing for Compute_Edges_And_Vertices
728 -- We store edges from 1 to 2 * NK and leave zero alone in order to use
731 Edges_Len
:= 2 * NK
+ 1;
733 if Edges
= No_Table
then
734 Edges
:= Allocate
(Edges_Len
, Edge_Size
);
737 if Vertices
= No_Table
then
738 Vertices
:= Allocate
(NV
, Vertex_Size
);
741 for J
in 0 .. NV
- 1 loop
742 Set_Vertices
(J
, (No_Vertex
, No_Vertex
- 1));
745 -- For each w, X = f1 (w) and Y = f2 (w)
747 for J
in 0 .. NK
- 1 loop
752 X
:= Sum
(WT
.Table
(Reduced
(J
)), T1
, Opt
);
753 Y
:= Sum
(WT
.Table
(Reduced
(J
)), T2
, Opt
);
755 -- Discard T1 and T2 as soon as we discover a self loop
762 -- We store (X, Y) and (Y, X) to ease assignment step
764 Set_Edges
(2 * J
+ 1, (X
, Y
, J
));
765 Set_Edges
(2 * J
+ 2, (Y
, X
, J
));
768 -- Return an empty graph when self loop detected
775 Put_Edges
(Output
, "Unsorted Edge Table");
776 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
778 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
782 -- Enforce consistency between edges and keys. Construct Vertices and
783 -- compute the list of neighbors of a vertex First .. Last as Edges
784 -- is sorted by X and then Y. To compute the neighbor list, sort the
787 Sorting
.Sort
(Edges_Len
- 1);
790 Put_Edges
(Output
, "Sorted Edge Table");
791 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
793 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
797 -- Edges valid range is 1 .. 2 * NK
799 for E
in 1 .. Edges_Len
- 1 loop
800 Edge
:= Get_Edges
(E
);
801 Key
:= Get_Key
(Edge
.Key
);
803 if Key
.Edge
= No_Edge
then
805 Set_Key
(Edge
.Key
, Key
);
808 Vertex
:= Get_Vertices
(Edge
.X
);
810 if Vertex
.First
= No_Edge
then
815 Set_Vertices
(Edge
.X
, Vertex
);
819 Put_Reduced_Keys
(Output
, "Key Table");
820 Put_Edges
(Output
, "Edge Table");
821 Put_Vertex_Table
(Output
, "Vertex Table");
824 end Compute_Edges_And_Vertices
;
832 Item_Size
: out Natural;
833 Length_1
: out Natural;
834 Length_2
: out Natural)
838 when Character_Position
=>
840 Length_1
:= Char_Pos_Set_Len
;
843 when Used_Character_Set
=>
848 when Function_Table_1
849 | Function_Table_2
=>
850 Item_Size
:= Type_Size
(NV
);
855 Item_Size
:= Type_Size
(NK
);
865 procedure Finalize
is
867 -- Deallocate all the WT components (both initial and reduced
868 -- ones) to avoid memory leaks.
870 for W
in 0 .. WT
.Last
loop
871 Free_Word
(WT
.Table
(W
));
876 -- Reset all variables for next usage
880 Char_Pos_Set
:= No_Table
;
881 Char_Pos_Set_Len
:= 0;
883 Used_Char_Set
:= No_Table
;
884 Used_Char_Set_Len
:= 0;
898 Vertices
:= No_Table
;
910 procedure Free_Word
(W
: in out Word_Type
) is
917 ----------------------------
918 -- Generate_Mapping_Table --
919 ----------------------------
921 procedure Generate_Mapping_Table
925 Seed
: in out Natural)
928 for J
in 0 .. L1
- 1 loop
929 for K
in 0 .. L2
- 1 loop
931 Set_Table
(Tab
, J
, K
, Seed
mod NV
);
934 end Generate_Mapping_Table
;
936 -----------------------------
937 -- Generate_Mapping_Tables --
938 -----------------------------
940 procedure Generate_Mapping_Tables
942 Seed
: in out Natural)
945 -- If T1 and T2 are already allocated no need to do it twice. Reuse them
946 -- as their size has not changed.
948 if T1
= No_Table
and then T2
= No_Table
then
950 Used_Char_Last
: Natural := 0;
954 if Opt
= CPU_Time
then
955 for P
in reverse Character'Range loop
956 Used_Char
:= Get_Used_Char
(P
);
957 if Used_Char
/= 0 then
958 Used_Char_Last
:= Used_Char
;
964 T1_Len
:= Char_Pos_Set_Len
;
965 T2_Len
:= Used_Char_Last
+ 1;
966 T1
:= Allocate
(T1_Len
* T2_Len
);
967 T2
:= Allocate
(T1_Len
* T2_Len
);
971 Generate_Mapping_Table
(T1
, T1_Len
, T2_Len
, Seed
);
972 Generate_Mapping_Table
(T2
, T1_Len
, T2_Len
, Seed
);
975 Put_Used_Char_Set
(Output
, "Used Character Set");
976 Put_Int_Matrix
(Output
, "Function Table 1", T1
,
978 Put_Int_Matrix
(Output
, "Function Table 2", T2
,
981 end Generate_Mapping_Tables
;
987 function Get_Char_Pos
(P
: Natural) return Natural is
988 N
: constant Natural := Char_Pos_Set
+ P
;
997 function Get_Edges
(F
: Natural) return Edge_Type
is
998 N
: constant Natural := Edges
+ (F
* Edge_Size
);
1001 E
.X
:= IT
.Table
(N
);
1002 E
.Y
:= IT
.Table
(N
+ 1);
1003 E
.Key
:= IT
.Table
(N
+ 2);
1011 function Get_Graph
(N
: Natural) return Integer is
1013 return IT
.Table
(G
+ N
);
1020 function Get_Key
(N
: Key_Id
) return Key_Type
is
1023 K
.Edge
:= IT
.Table
(Keys
+ N
);
1031 function Get_Table
(T
: Integer; X
, Y
: Natural) return Natural is
1032 N
: constant Natural := T
+ (Y
* T1_Len
) + X
;
1034 return IT
.Table
(N
);
1041 function Get_Used_Char
(C
: Character) return Natural is
1042 N
: constant Natural := Used_Char_Set
+ Character'Pos (C
);
1044 return IT
.Table
(N
);
1051 function Get_Vertices
(F
: Natural) return Vertex_Type
is
1052 N
: constant Natural := Vertices
+ (F
* Vertex_Size
);
1055 V
.First
:= IT
.Table
(N
);
1056 V
.Last
:= IT
.Table
(N
+ 1);
1064 function Image
(Int
: Integer; W
: Natural := 0) return String is
1065 B
: String (1 .. 32);
1068 procedure Img
(V
: Natural);
1069 -- Compute image of V into B, starting at B (L), incrementing L
1075 procedure Img
(V
: Natural) is
1082 B
(L
) := Character'Val ((V
mod 10) + Character'Pos ('0'));
1085 -- Start of processing for Image
1096 return Image
(B
(1 .. L
), W
);
1103 function Image
(Str
: String; W
: Natural := 0) return String is
1104 Len
: constant Natural := Str
'Length;
1105 Max
: Natural := Len
;
1113 Buf
: String (1 .. Max
) := (1 .. Max
=> ' ');
1116 for J
in 0 .. Len
- 1 loop
1117 Buf
(Max
- Len
+ 1 + J
) := Str
(Str
'First + J
);
1128 function Initial
(K
: Key_Id
) return Word_Id
is
1137 procedure Initialize
1139 K_To_V
: Float := Default_K_To_V
;
1140 Optim
: Optimization
:= CPU_Time
;
1141 Tries
: Positive := Default_Tries
)
1144 -- Deallocate the part of the table concerning the reduced words.
1145 -- Initial words are already present in the table. We may have reduced
1146 -- words already there because a previous computation failed. We are
1147 -- currently retrying and the reduced words have to be deallocated.
1149 for W
in NK
.. WT
.Last
loop
1150 Free_Word
(WT
.Table
(W
));
1154 -- Initialize of computation variables
1158 Char_Pos_Set
:= No_Table
;
1159 Char_Pos_Set_Len
:= 0;
1161 Used_Char_Set
:= No_Table
;
1162 Used_Char_Set_Len
:= 0;
1176 Vertices
:= No_Table
;
1185 raise Program_Error
with "K to V ratio cannot be lower than 2.0";
1188 -- Do not accept a value of K2V too close to 2.0 such that once
1189 -- rounded up, NV = 2 * NK because the algorithm would not converge.
1191 NV
:= Natural (Float (NK
) * K2V
);
1192 if NV
<= 2 * NK
then
1196 Keys
:= Allocate
(NK
);
1198 -- Resize initial words to have all of them at the same size
1199 -- (so the size of the largest one).
1201 for K
in 0 .. NK
- 1 loop
1202 Resize_Word
(WT
.Table
(Initial
(K
)), Max_Key_Len
);
1205 -- Allocated the table to store the reduced words. As WT is a
1206 -- GNAT.Table (using C memory management), pointers have to be
1207 -- explicitly initialized to null.
1209 WT
.Set_Last
(Reduced
(NK
- 1));
1210 for W
in 0 .. NK
- 1 loop
1211 WT
.Table
(Reduced
(W
)) := null;
1219 procedure Insert
(Value
: String) is
1220 Len
: constant Natural := Value
'Length;
1224 WT
.Table
(NK
) := New_Word
(Value
);
1227 if Max_Key_Len
< Len
then
1231 if Min_Key_Len
= 0 or else Len
< Min_Key_Len
then
1240 procedure New_Line
(File
: File_Descriptor
) is
1242 if Write
(File
, EOL
'Address, 1) /= 1 then
1243 raise Program_Error
;
1251 function New_Word
(S
: String) return Word_Type
is
1253 return new String'(S);
1256 ------------------------------
1257 -- Parse_Position_Selection --
1258 ------------------------------
1260 procedure Parse_Position_Selection (Argument : String) is
1261 N : Natural := Argument'First;
1262 L : constant Natural := Argument'Last;
1263 M : constant Natural := Max_Key_Len;
1265 T : array (1 .. M) of Boolean := (others => False);
1267 function Parse_Index return Natural;
1268 -- Parse argument starting at index N to find an index
1274 function Parse_Index return Natural is
1275 C : Character := Argument (N);
1284 if C not in '0' .. '9' then
1285 raise Program_Error with "cannot read position argument";
1288 while C in '0' .. '9' loop
1289 V := V * 10 + (Character'Pos (C) - Character'Pos ('0'));
1298 -- Start of processing for Parse_Position_Selection
1301 -- Empty specification means all the positions
1304 Char_Pos_Set_Len := M;
1305 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
1307 for C in 0 .. Char_Pos_Set_Len - 1 loop
1308 Set_Char_Pos (C, C + 1);
1314 First, Last : Natural;
1317 First := Parse_Index;
1322 if N <= L and then Argument (N) = '-' then
1324 Last := Parse_Index;
1327 -- Include the positions in the selection
1329 for J in First .. Last loop
1336 if Argument (N) /= ',' then
1337 raise Program_Error with "cannot read position argument";
1343 -- Compute position selection length
1346 for J in T'Range loop
1352 -- Fill position selection
1354 Char_Pos_Set_Len := N;
1355 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
1358 for J in T'Range loop
1360 Set_Char_Pos (N, J);
1365 end Parse_Position_Selection;
1371 procedure Produce (Pkg_Name : String := Default_Pkg_Name) is
1372 File : File_Descriptor;
1375 -- For call to Close
1377 function Array_Img (N, T, R1 : String; R2 : String := "") return String;
1378 -- Return string "N : constant array (R1[, R2]) of T;"
1380 function Range_Img (F, L : Natural; T : String := "") return String;
1381 -- Return string "[T range ]F .. L"
1383 function Type_Img (L : Natural) return String;
1384 -- Return the larger unsigned type T such that T'Last < L
1392 R2 : String := "") return String
1398 Add (" : constant array (");
1409 return Line (1 .. Last);
1416 function Range_Img (F, L : Natural; T : String := "") return String is
1417 FI : constant String := Image (F);
1418 FL : constant Natural := FI'Length;
1419 LI : constant String := Image (L);
1420 LL : constant Natural := LI'Length;
1421 TL : constant Natural := T'Length;
1422 RI : String (1 .. TL + 7 + FL + 4 + LL);
1427 RI (Len + 1 .. Len + TL) := T;
1429 RI (Len + 1 .. Len + 7) := " range ";
1433 RI (Len + 1 .. Len + FL) := FI;
1435 RI (Len + 1 .. Len + 4) := " .. ";
1437 RI (Len + 1 .. Len + LL) := LI;
1439 return RI (1 .. Len);
1446 function Type_Img (L : Natural) return String is
1447 S : constant String := Image (Type_Size (L));
1448 U : String := "Unsigned_ ";
1452 for J in S'Range loop
1464 PLen : constant Natural := Pkg_Name'Length;
1465 FName : String (1 .. PLen + 4);
1467 -- Start of processing for Produce
1470 FName (1 .. PLen) := Pkg_Name;
1471 for J in 1 .. PLen loop
1472 if FName (J) in 'A
' .. 'Z
' then
1473 FName (J) := Character'Val (Character'Pos (FName (J))
1474 - Character'Pos ('A
')
1475 + Character'Pos ('a
'));
1477 elsif FName (J) = '.' then
1482 FName (PLen + 1 .. PLen + 4) := ".ads";
1484 File := Create_File (FName, Binary);
1486 Put (File, "package ");
1487 Put (File, Pkg_Name);
1490 Put (File, " function Hash (S : String) return Natural;");
1493 Put (File, Pkg_Name);
1496 Close (File, Status);
1502 FName (PLen + 4) := 'b
';
1504 File := Create_File (FName, Binary);
1506 Put (File, "with Interfaces; use Interfaces;");
1509 Put (File, "package body ");
1510 Put (File, Pkg_Name);
1515 if Opt = CPU_Time then
1516 Put (File, Array_Img ("C", Type_Img (256), "Character"));
1519 F := Character'Pos (Character'First);
1520 L := Character'Pos (Character'Last);
1522 for J in Character'Range loop
1523 P := Get_Used_Char (J);
1524 Put (File, Image (P), 1, 0, 1, F, L, Character'Pos (J));
1531 L := Char_Pos_Set_Len - 1;
1533 Put (File, Array_Img ("P", "Natural", Range_Img (F, L)));
1536 for J in F .. L loop
1537 Put (File, Image (Get_Char_Pos (J)), 1, 0, 1, F, L, J);
1542 if Opt = CPU_Time then
1545 Array_Img ("T1", Type_Img (NV),
1546 Range_Img (0, T1_Len - 1),
1547 Range_Img (0, T2_Len - 1, Type_Img (256))),
1548 T1, T1_Len, T2_Len);
1553 Array_Img ("T1", Type_Img (NV),
1554 Range_Img (0, T1_Len - 1)),
1560 if Opt = CPU_Time then
1563 Array_Img ("T2", Type_Img (NV),
1564 Range_Img (0, T1_Len - 1),
1565 Range_Img (0, T2_Len - 1, Type_Img (256))),
1566 T2, T1_Len, T2_Len);
1571 Array_Img ("T2", Type_Img (NV),
1572 Range_Img (0, T1_Len - 1)),
1580 Array_Img ("G", Type_Img (NK),
1581 Range_Img (0, G_Len - 1)),
1585 Put (File, " function Hash (S : String) return Natural is");
1587 Put (File, " F : constant Natural := S'First - 1;");
1589 Put (File, " L : constant Natural := S'Length;");
1591 Put (File, " F1, F2 : Natural := 0;");
1594 Put (File, " J : ");
1596 if Opt = CPU_Time then
1597 Put (File, Type_Img (256));
1599 Put (File, "Natural");
1605 Put (File, " begin");
1607 Put (File, " for K in P'Range loop");
1609 Put (File, " exit when L < P (K);");
1611 Put (File, " J := ");
1613 if Opt = CPU_Time then
1616 Put (File, "Character'Pos");
1619 Put (File, " (S (P (K) + F));");
1622 Put (File, " F1 := (F1 + Natural (T1 (K");
1624 if Opt = CPU_Time then
1630 if Opt = Memory_Space then
1634 Put (File, ") mod ");
1635 Put (File, Image (NV));
1639 Put (File, " F2 := (F2 + Natural (T2 (K");
1641 if Opt = CPU_Time then
1647 if Opt = Memory_Space then
1651 Put (File, ") mod ");
1652 Put (File, Image (NV));
1656 Put (File, " end loop;");
1660 " return (Natural (G (F1)) + Natural (G (F2))) mod ");
1662 Put (File, Image (NK));
1665 Put (File, " end Hash;");
1669 Put (File, Pkg_Name);
1672 Close (File, Status);
1683 procedure Put (File : File_Descriptor; Str : String) is
1684 Len : constant Natural := Str'Length;
1686 if Write (File, Str'Address, Len) /= Len then
1687 raise Program_Error;
1696 (F : File_Descriptor;
1705 Len : constant Natural := S'Length;
1708 -- Write current line, followed by LF
1716 Put (F, Line (1 .. Last));
1721 -- Start of processing for Put
1724 if C1 = F1 and then C2 = F2 then
1728 if Last + Len + 3 > Max then
1733 Line (Last + 1 .. Last + 5) := " ";
1737 if C1 = F1 and then C2 = F2 then
1761 Line (Last + 1 .. Last + Len) := S;
1790 procedure Put_Edges (File : File_Descriptor; Title : String) is
1792 F1 : constant Natural := 1;
1793 L1 : constant Natural := Edges_Len - 1;
1794 M : constant Natural := Max / 5;
1800 -- Edges valid range is 1 .. Edge_Len - 1
1802 for J in F1 .. L1 loop
1804 Put (File, Image (J, M), F1, L1, J, 1, 4, 1);
1805 Put (File, Image (E.X, M), F1, L1, J, 1, 4, 2);
1806 Put (File, Image (E.Y, M), F1, L1, J, 1, 4, 3);
1807 Put (File, Image (E.Key, M), F1, L1, J, 1, 4, 4);
1811 ----------------------
1812 -- Put_Initial_Keys --
1813 ----------------------
1815 procedure Put_Initial_Keys (File : File_Descriptor; Title : String) is
1816 F1 : constant Natural := 0;
1817 L1 : constant Natural := NK - 1;
1818 M : constant Natural := Max / 5;
1825 for J in F1 .. L1 loop
1827 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
1828 Put (File, Image (K.Edge, M), F1, L1, J, 1, 3, 2);
1829 Put (File, WT.Table (Initial (J)).all, F1, L1, J, 1, 3, 3);
1831 end Put_Initial_Keys;
1833 --------------------
1834 -- Put_Int_Matrix --
1835 --------------------
1837 procedure Put_Int_Matrix
1838 (File : File_Descriptor;
1844 F1 : constant Integer := 0;
1845 L1 : constant Integer := Len_1 - 1;
1846 F2 : constant Integer := 0;
1847 L2 : constant Integer := Len_2 - 1;
1855 for J in F1 .. L1 loop
1856 Ix := IT.Table (Table + J);
1857 Put (File, Image (Ix), 1, 0, 1, F1, L1, J);
1861 for J in F1 .. L1 loop
1862 for K in F2 .. L2 loop
1863 Ix := IT.Table (Table + J + K * Len_1);
1864 Put (File, Image (Ix), F1, L1, J, F2, L2, K);
1870 --------------------
1871 -- Put_Int_Vector --
1872 --------------------
1874 procedure Put_Int_Vector
1875 (File : File_Descriptor;
1880 F2 : constant Natural := 0;
1881 L2 : constant Natural := Length - 1;
1887 for J in F2 .. L2 loop
1888 Put (File, Image (IT.Table (Vector + J)), 1, 0, 1, F2, L2, J);
1892 ----------------------
1893 -- Put_Reduced_Keys --
1894 ----------------------
1896 procedure Put_Reduced_Keys (File : File_Descriptor; Title : String) is
1897 F1 : constant Natural := 0;
1898 L1 : constant Natural := NK - 1;
1899 M : constant Natural := Max / 5;
1906 for J in F1 .. L1 loop
1908 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
1909 Put (File, Image (K.Edge, M), F1, L1, J, 1, 3, 2);
1910 Put (File, WT.Table (Reduced (J)).all, F1, L1, J, 1, 3, 3);
1912 end Put_Reduced_Keys;
1914 -----------------------
1915 -- Put_Used_Char_Set --
1916 -----------------------
1918 procedure Put_Used_Char_Set (File : File_Descriptor; Title : String) is
1919 F : constant Natural := Character'Pos (Character'First);
1920 L : constant Natural := Character'Pos (Character'Last);
1926 for J in Character'Range loop
1928 (File, Image (Get_Used_Char (J)), 1, 0, 1, F, L, Character'Pos (J));
1930 end Put_Used_Char_Set;
1932 ----------------------
1933 -- Put_Vertex_Table --
1934 ----------------------
1936 procedure Put_Vertex_Table (File : File_Descriptor; Title : String) is
1937 F1 : constant Natural := 0;
1938 L1 : constant Natural := NV - 1;
1939 M : constant Natural := Max / 4;
1946 for J in F1 .. L1 loop
1947 V := Get_Vertices (J);
1948 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
1949 Put (File, Image (V.First, M), F1, L1, J, 1, 3, 2);
1950 Put (File, Image (V.Last, M), F1, L1, J, 1, 3, 3);
1952 end Put_Vertex_Table;
1958 procedure Random (Seed : in out Natural) is
1960 -- Park & Miller Standard Minimal using Schrage's algorithm to avoid
1961 -- overflow: Xn+1 = 16807 * Xn mod (2 ** 31 - 1)
1968 R := Seed mod 127773;
1970 X := 16807 * R - 2836 * Q;
1973 Seed := X + 2147483647;
1983 function Reduced (K : Key_Id) return Word_Id is
1992 procedure Resize_Word (W : in out Word_Type; Len : Natural) is
1993 S1 : constant String := W.all;
1994 S2 : String (1 .. Len) := (others => ASCII.NUL);
1995 L : constant Natural := S1'Length;
2004 --------------------------
2005 -- Select_Char_Position --
2006 --------------------------
2008 procedure Select_Char_Position is
2010 type Vertex_Table_Type is array (Natural range <>) of Vertex_Type;
2012 procedure Build_Identical_Keys_Sets
2013 (Table : in out Vertex_Table_Type;
2014 Last : in out Natural;
2016 -- Build a list of keys subsets that are identical with the current
2017 -- position selection plus Pos. Once this routine is called, reduced
2018 -- words are sorted by subsets and each item (First, Last) in Sets
2019 -- defines the range of identical keys.
2020 -- Need comment saying exactly what Last is ???
2022 function Count_Different_Keys
2023 (Table : Vertex_Table_Type;
2025 Pos : Natural) return Natural;
2026 -- For each subset in Sets, count the number of different keys if we add
2027 -- Pos to the current position selection.
2029 Sel_Position : IT.Table_Type (1 .. Max_Key_Len);
2030 Last_Sel_Pos : Natural := 0;
2031 Max_Sel_Pos : Natural := 0;
2033 -------------------------------
2034 -- Build_Identical_Keys_Sets --
2035 -------------------------------
2037 procedure Build_Identical_Keys_Sets
2038 (Table : in out Vertex_Table_Type;
2039 Last : in out Natural;
2042 S : constant Vertex_Table_Type := Table (Table'First .. Last);
2043 C : constant Natural := Pos;
2044 -- Shortcuts (why are these not renames ???)
2048 -- First and last words of a subset
2051 -- GNAT.Heap_Sort assumes that the first array index is 1. Offset
2052 -- defines the translation to operate.
2054 function Lt (L, R : Natural) return Boolean;
2055 procedure Move (From : Natural; To : Natural);
2056 -- Subprograms needed by GNAT.Heap_Sort_G
2062 function Lt (L, R : Natural) return Boolean is
2063 C : constant Natural := Pos;
2070 Right := Offset + R;
2076 Right := Offset + R;
2079 return WT.Table (Left)(C) < WT.Table (Right)(C);
2086 procedure Move (From : Natural; To : Natural) is
2087 Target, Source : Natural;
2092 Target := Offset + To;
2094 Source := Offset + From;
2097 Source := Offset + From;
2098 Target := Offset + To;
2101 WT.Table (Target) := WT.Table (Source);
2102 WT.Table (Source) := null;
2105 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
2107 -- Start of processing for Build_Identical_Key_Sets
2112 -- For each subset in S, extract the new subsets we have by adding C
2113 -- in the position selection.
2115 for J in S'Range loop
2116 if S (J).First = S (J).Last then
2120 Table (Last) := (F, L);
2123 Offset := Reduced (S (J).First) - 1;
2124 Sorting.Sort (S (J).Last - S (J).First + 1);
2128 for N in S (J).First .. S (J).Last loop
2130 -- For the last item, close the last subset
2132 if N = S (J).Last then
2134 Table (Last) := (F, N);
2136 -- Two contiguous words are identical when they have the
2137 -- same Cth character.
2139 elsif WT.Table (Reduced (N))(C) =
2140 WT.Table (Reduced (N + 1))(C)
2144 -- Find a new subset of identical keys. Store the current
2145 -- one and create a new subset.
2149 Table (Last) := (F, L);
2156 end Build_Identical_Keys_Sets;
2158 --------------------------
2159 -- Count_Different_Keys --
2160 --------------------------
2162 function Count_Different_Keys
2163 (Table : Vertex_Table_Type;
2165 Pos : Natural) return Natural
2167 N : array (Character) of Natural;
2172 -- For each subset, count the number of words that are still
2173 -- different when we include Pos in the position selection. Only
2174 -- focus on this position as the other positions already produce
2177 for S in 1 .. Last loop
2179 -- Count the occurrences of the different characters
2182 for K in Table (S).First .. Table (S).Last loop
2183 C := WT.Table (Reduced (K))(Pos);
2187 -- Update the number of different keys. Each character used
2188 -- denotes a different key.
2190 for J in N'Range loop
2198 end Count_Different_Keys;
2200 -- Start of processing for Select_Char_Position
2203 -- Initialize the reduced words set
2205 for K in 0 .. NK - 1 loop
2206 WT.Table (Reduced (K)) := New_Word (WT.Table (Initial (K)).all);
2210 Differences : Natural;
2211 Max_Differences : Natural := 0;
2212 Old_Differences : Natural;
2213 Max_Diff_Sel_Pos : Natural := 0; -- init to kill warning
2214 Max_Diff_Sel_Pos_Idx : Natural := 0; -- init to kill warning
2215 Same_Keys_Sets_Table : Vertex_Table_Type (1 .. NK);
2216 Same_Keys_Sets_Last : Natural := 1;
2219 for C in Sel_Position'Range loop
2220 Sel_Position (C) := C;
2223 Same_Keys_Sets_Table (1) := (0, NK - 1);
2226 -- Preserve maximum number of different keys and check later on
2227 -- that this value is strictly incrementing. Otherwise, it means
2228 -- that two keys are strictly identical.
2230 Old_Differences := Max_Differences;
2232 -- The first position should not exceed the minimum key length.
2233 -- Otherwise, we may end up with an empty word once reduced.
2235 if Last_Sel_Pos = 0 then
2236 Max_Sel_Pos := Min_Key_Len;
2238 Max_Sel_Pos := Max_Key_Len;
2241 -- Find which position increases more the number of differences
2243 for J in Last_Sel_Pos + 1 .. Max_Sel_Pos loop
2244 Differences := Count_Different_Keys
2245 (Same_Keys_Sets_Table,
2246 Same_Keys_Sets_Last,
2251 "Selecting position" & Sel_Position (J)'Img &
2252 " results in" & Differences'Img &
2257 if Differences > Max_Differences then
2258 Max_Differences := Differences;
2259 Max_Diff_Sel_Pos := Sel_Position (J);
2260 Max_Diff_Sel_Pos_Idx := J;
2264 if Old_Differences = Max_Differences then
2265 raise Program_Error with "some keys are identical";
2268 -- Insert selected position and sort Sel_Position table
2270 Last_Sel_Pos := Last_Sel_Pos + 1;
2271 Sel_Position (Last_Sel_Pos + 1 .. Max_Diff_Sel_Pos_Idx) :=
2272 Sel_Position (Last_Sel_Pos .. Max_Diff_Sel_Pos_Idx - 1);
2273 Sel_Position (Last_Sel_Pos) := Max_Diff_Sel_Pos;
2275 for P in 1 .. Last_Sel_Pos - 1 loop
2276 if Max_Diff_Sel_Pos < Sel_Position (P) then
2277 Sel_Position (P + 1 .. Last_Sel_Pos) :=
2278 Sel_Position (P .. Last_Sel_Pos - 1);
2279 Sel_Position (P) := Max_Diff_Sel_Pos;
2284 exit when Max_Differences = NK;
2286 Build_Identical_Keys_Sets
2287 (Same_Keys_Sets_Table,
2288 Same_Keys_Sets_Last,
2293 "Selecting position" & Max_Diff_Sel_Pos'Img &
2294 " results in" & Max_Differences'Img &
2299 for J in 1 .. Same_Keys_Sets_Last loop
2301 Same_Keys_Sets_Table (J).First ..
2302 Same_Keys_Sets_Table (J).Last
2304 Put (Output, WT.Table (Reduced (K)).all);
2314 Char_Pos_Set_Len := Last_Sel_Pos;
2315 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
2317 for C in 1 .. Last_Sel_Pos loop
2318 Set_Char_Pos (C - 1, Sel_Position (C));
2320 end Select_Char_Position;
2322 --------------------------
2323 -- Select_Character_Set --
2324 --------------------------
2326 procedure Select_Character_Set is
2327 Last : Natural := 0;
2328 Used : array (Character) of Boolean := (others => False);
2332 for J in 0 .. NK - 1 loop
2333 for K in 0 .. Char_Pos_Set_Len - 1 loop
2334 Char := WT.Table (Initial (J))(Get_Char_Pos (K));
2335 exit when Char = ASCII.NUL;
2336 Used (Char) := True;
2340 Used_Char_Set_Len := 256;
2341 Used_Char_Set := Allocate (Used_Char_Set_Len);
2343 for J in Used'Range loop
2345 Set_Used_Char (J, Last);
2348 Set_Used_Char (J, 0);
2351 end Select_Character_Set;
2357 procedure Set_Char_Pos (P : Natural; Item : Natural) is
2358 N : constant Natural := Char_Pos_Set + P;
2360 IT.Table (N) := Item;
2367 procedure Set_Edges (F : Natural; Item : Edge_Type) is
2368 N : constant Natural := Edges + (F * Edge_Size);
2370 IT.Table (N) := Item.X;
2371 IT.Table (N + 1) := Item.Y;
2372 IT.Table (N + 2) := Item.Key;
2379 procedure Set_Graph (N : Natural; Item : Integer) is
2381 IT.Table (G + N) := Item;
2388 procedure Set_Key (N : Key_Id; Item : Key_Type) is
2390 IT.Table (Keys + N) := Item.Edge;
2397 procedure Set_Table (T : Integer; X, Y : Natural; Item : Natural) is
2398 N : constant Natural := T + ((Y * T1_Len) + X);
2400 IT.Table (N) := Item;
2407 procedure Set_Used_Char (C : Character; Item : Natural) is
2408 N : constant Natural := Used_Char_Set + Character'Pos (C);
2410 IT.Table (N) := Item;
2417 procedure Set_Vertices (F : Natural; Item : Vertex_Type) is
2418 N : constant Natural := Vertices + (F * Vertex_Size);
2420 IT.Table (N) := Item.First;
2421 IT.Table (N + 1) := Item.Last;
2431 Opt : Optimization) return Natural
2437 if Opt = CPU_Time then
2438 for J in 0 .. T1_Len - 1 loop
2439 exit when Word (J + 1) = ASCII.NUL;
2440 R := Get_Table (Table, J, Get_Used_Char (Word (J + 1)));
2441 S := (S + R) mod NV;
2445 for J in 0 .. T1_Len - 1 loop
2446 exit when Word (J + 1) = ASCII.NUL;
2447 R := Get_Table (Table, J, 0);
2448 S := (S + R * Character'Pos (Word (J + 1))) mod NV;
2459 function Type_Size (L : Natural) return Natural is
2463 elsif L <= 2 ** 16 then
2477 K : Natural := 0) return Natural
2481 when Character_Position =>
2482 return Get_Char_Pos (J);
2484 when Used_Character_Set =>
2485 return Get_Used_Char (Character'Val (J));
2487 when Function_Table_1 =>
2488 return Get_Table (T1, J, K);
2490 when Function_Table_2 =>
2491 return Get_Table (T2, J, K);
2494 return Get_Graph (J);
2499 end GNAT.Perfect_Hash_Generators;