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-2011, 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 3, 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada.IO_Exceptions; use Ada.IO_Exceptions;
33 with Ada.Characters.Handling; use Ada.Characters.Handling;
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) renames Free;
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 function Trim_Trailing_Nuls (Str : String) return String;
147 -- Return Str with trailing NUL characters removed
149 Output : File_Descriptor renames GNAT.OS_Lib.Standout;
152 EOL : constant Character := ASCII.LF;
154 Max : constant := 78;
156 Line : String (1 .. Max);
157 -- Use this line to provide buffered IO
159 procedure Add (C : Character);
160 procedure Add (S : String);
161 -- Add a character or a string in Line and update Last
164 (F : File_Descriptor;
172 -- Write string S into file F as a element of an array of one or two
173 -- dimensions. Fk (resp. Lk and Ck) indicates the first (resp last and
174 -- current) index in the k-th dimension. If F1 = L1 the array is considered
175 -- as a one dimension array. This dimension is described by F2 and L2. This
176 -- routine takes care of all the parenthesis, spaces and commas needed to
177 -- format correctly the array. Moreover, the array is well indented and is
178 -- wrapped to fit in a 80 col line. When the line is full, the routine
179 -- writes it into file F. When the array is completed, the routine adds
180 -- semi-colon and writes the line into file F.
182 procedure New_Line (File : File_Descriptor);
183 -- Simulate Ada.Text_IO.New_Line with GNAT.OS_Lib
185 procedure Put (File : File_Descriptor; Str : String);
186 -- Simulate Ada.Text_IO.Put with GNAT.OS_Lib
188 procedure Put_Used_Char_Set (File : File_Descriptor; Title : String);
189 -- Output a title and a used character set
191 procedure Put_Int_Vector
192 (File : File_Descriptor;
196 -- Output a title and a vector
198 procedure Put_Int_Matrix
199 (File : File_Descriptor;
204 -- Output a title and a matrix. When the matrix has only one non-empty
205 -- dimension (Len_2 = 0), output a vector.
207 procedure Put_Edges (File : File_Descriptor; Title : String);
208 -- Output a title and an edge table
210 procedure Put_Initial_Keys (File : File_Descriptor; Title : String);
211 -- Output a title and a key table
213 procedure Put_Reduced_Keys (File : File_Descriptor; Title : String);
214 -- Output a title and a key table
216 procedure Put_Vertex_Table (File : File_Descriptor; Title : String);
217 -- Output a title and a vertex table
219 function Ada_File_Base_Name (Pkg_Name : String) return String;
220 -- Return the base file name (i.e. without .ads/.adb extension) for an
221 -- Ada source file containing the named package, using the standard GNAT
222 -- file-naming convention. For example, if Pkg_Name is "Parent.Child", we
223 -- return "parent-child".
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
319 Keys : Table_Id := No_Table;
321 -- NK : Number of Keys
323 function Initial (K : Key_Id) return Word_Id;
324 pragma Inline (Initial);
326 function Reduced (K : Key_Id) return Word_Id;
327 pragma Inline (Reduced);
329 function Get_Key (N : Key_Id) return Key_Type;
330 procedure Set_Key (N : Key_Id; Item : Key_Type);
331 -- Get or Set Nth element of Keys table
337 Char_Pos_Set : Table_Id := No_Table;
338 Char_Pos_Set_Len : Natural;
339 -- Character Selected Position Set
341 function Get_Char_Pos (P : Natural) return Natural;
342 procedure Set_Char_Pos (P : Natural; Item : Natural);
343 -- Get or Set the string position of the Pth selected character
349 Used_Char_Set : Table_Id := No_Table;
350 Used_Char_Set_Len : Natural;
351 -- Used Character Set : Define a new character mapping. When all the
352 -- characters are not present in the keys, in order to reduce the size
353 -- of some tables, we redefine the character mapping.
355 function Get_Used_Char (C : Character) return Natural;
356 procedure Set_Used_Char (C : Character; Item : Natural);
362 T1 : Table_Id := No_Table;
363 T2 : Table_Id := No_Table;
366 -- T1 : Values table to compute F1
367 -- T2 : Values table to compute F2
369 function Get_Table (T : Integer; X, Y : Natural) return Natural;
370 procedure Set_Table (T : Integer; X, Y : Natural; Item : Natural);
376 G : Table_Id := No_Table;
378 -- Values table to compute G
380 NT : Natural := Default_Tries;
381 -- Number of tries running the algorithm before raising an error
383 function Get_Graph (N : Natural) return Integer;
384 procedure Set_Graph (N : Natural; Item : Integer);
385 -- Get or Set Nth element of graph
391 Edge_Size : constant := 3;
392 Edges : Table_Id := No_Table;
394 -- Edges : Edge table of the random graph G
396 function Get_Edges (F : Natural) return Edge_Type;
397 procedure Set_Edges (F : Natural; Item : Edge_Type);
403 Vertex_Size : constant := 2;
405 Vertices : Table_Id := No_Table;
406 -- Vertex table of the random graph G
409 -- Number of Vertices
411 function Get_Vertices (F : Natural) return Vertex_Type;
412 procedure Set_Vertices (F : Natural; Item : Vertex_Type);
413 -- Comments needed ???
416 -- Ratio between Keys and Vertices (parameter of Czech's algorithm)
419 -- Optimization mode (memory vs CPU)
421 Max_Key_Len : Natural := 0;
422 Min_Key_Len : Natural := 0;
423 -- Maximum and minimum of all the word length
428 function Type_Size (L : Natural) return Natural;
429 -- Given the last L of an unsigned integer type T, return its size
435 function Acyclic return Boolean is
436 Marks : array (0 .. NV - 1) of Vertex_Id := (others => No_Vertex);
438 function Traverse (Edge : Edge_Id; Mark : Vertex_Id) return Boolean;
439 -- Propagate Mark from X to Y. X is already marked. Mark Y and propagate
440 -- it to the edges of Y except the one representing the same key. Return
441 -- False when Y is marked with Mark.
447 function Traverse (Edge : Edge_Id; Mark : Vertex_Id) return Boolean is
448 E : constant Edge_Type := Get_Edges (Edge);
449 K : constant Key_Id := E.Key;
450 Y : constant Vertex_Id := E.Y;
451 M : constant Vertex_Id := Marks (E.Y);
458 elsif M = No_Vertex then
460 V := Get_Vertices (Y);
462 for J in V.First .. V.Last loop
464 -- Do not propagate to the edge representing the same key
466 if Get_Edges (J).Key /= K
467 and then not Traverse (J, Mark)
479 -- Start of processing for Acyclic
482 -- Edges valid range is
484 for J in 1 .. Edges_Len - 1 loop
486 Edge := Get_Edges (J);
488 -- Mark X of E when it has not been already done
490 if Marks (Edge.X) = No_Vertex then
491 Marks (Edge.X) := Edge.X;
494 -- Traverse E when this has not already been done
496 if Marks (Edge.Y) = No_Vertex
497 and then not Traverse (J, Edge.X)
506 ------------------------
507 -- Ada_File_Base_Name --
508 ------------------------
510 function Ada_File_Base_Name (Pkg_Name : String) return String is
512 -- Convert to lower case, then replace '.' with '-'
514 return Result : String := To_Lower (Pkg_Name) do
515 for J in Result'Range loop
516 if Result (J) = '.' then
521 end Ada_File_Base_Name;
527 procedure Add (C : Character) is
528 pragma Assert (C /= ASCII.NUL);
530 Line (Last + 1) := C;
538 procedure Add (S : String) is
539 Len : constant Natural := S'Length;
541 for J in S'Range loop
542 pragma Assert (S (J) /= ASCII.NUL);
546 Line (Last + 1 .. Last + Len) := S;
554 function Allocate (N : Natural; S : Natural := 1) return Table_Id is
555 L : constant Integer := IT.Last;
557 IT.Set_Last (L + N * S);
559 -- Initialize, so debugging printouts don't trip over uninitialized
562 for J in L + 1 .. IT.Last loop
569 ------------------------------
570 -- Apply_Position_Selection --
571 ------------------------------
573 procedure Apply_Position_Selection is
575 for J in 0 .. NK - 1 loop
577 IW : constant String := WT.Table (Initial (J)).all;
578 RW : String (1 .. IW'Length) := (others => ASCII.NUL);
579 N : Natural := IW'First - 1;
582 -- Select the characters of Word included in the position
585 for C in 0 .. Char_Pos_Set_Len - 1 loop
586 exit when IW (Get_Char_Pos (C)) = ASCII.NUL;
588 RW (N) := IW (Get_Char_Pos (C));
591 -- Build the new table with the reduced word. Be careful
592 -- to deallocate the old version to avoid memory leaks.
594 Free_Word (WT.Table (Reduced (J)));
595 WT.Table (Reduced (J)) := New_Word (RW);
596 Set_Key (J, (Edge => No_Edge));
599 end Apply_Position_Selection;
601 -------------------------------
602 -- Assign_Values_To_Vertices --
603 -------------------------------
605 procedure Assign_Values_To_Vertices is
608 procedure Assign (X : Vertex_Id);
609 -- Execute assignment on X's neighbors except the vertex that we are
610 -- coming from which is already assigned.
616 procedure Assign (X : Vertex_Id) is
618 V : constant Vertex_Type := Get_Vertices (X);
621 for J in V.First .. V.Last loop
624 if Get_Graph (E.Y) = -1 then
625 Set_Graph (E.Y, (E.Key - Get_Graph (X)) mod NK);
631 -- Start of processing for Assign_Values_To_Vertices
634 -- Value -1 denotes an uninitialized value as it is supposed to
635 -- be in the range 0 .. NK.
639 G := Allocate (G_Len, 1);
642 for J in 0 .. G_Len - 1 loop
646 for K in 0 .. NK - 1 loop
647 X := Get_Edges (Get_Key (K).Edge).X;
649 if Get_Graph (X) = -1 then
655 for J in 0 .. G_Len - 1 loop
656 if Get_Graph (J) = -1 then
662 Put_Int_Vector (Output, "Assign Values To Vertices", G, G_Len);
664 end Assign_Values_To_Vertices;
670 procedure Compute (Position : String := Default_Position) is
671 Success : Boolean := False;
675 raise Program_Error with "keywords set cannot be empty";
679 Put_Initial_Keys (Output, "Initial Key Table");
682 if Position'Length /= 0 then
683 Parse_Position_Selection (Position);
685 Select_Char_Position;
690 (Output, "Char Position Set", Char_Pos_Set, Char_Pos_Set_Len);
693 Apply_Position_Selection;
696 Put_Reduced_Keys (Output, "Reduced Keys Table");
699 Select_Character_Set;
702 Put_Used_Char_Set (Output, "Character Position Table");
705 -- Perform Czech's algorithm
707 for J in 1 .. NT loop
708 Generate_Mapping_Tables (Opt, S);
709 Compute_Edges_And_Vertices (Opt);
711 -- When graph is not empty (no self-loop from previous operation) and
714 if 0 < Edges_Len and then Acyclic then
721 raise Too_Many_Tries;
724 Assign_Values_To_Vertices;
727 --------------------------------
728 -- Compute_Edges_And_Vertices --
729 --------------------------------
731 procedure Compute_Edges_And_Vertices (Opt : Optimization) is
736 Vertex : Vertex_Type;
737 Not_Acyclic : Boolean := False;
739 procedure Move (From : Natural; To : Natural);
740 function Lt (L, R : Natural) return Boolean;
741 -- Subprograms needed for GNAT.Heap_Sort_G
747 function Lt (L, R : Natural) return Boolean is
748 EL : constant Edge_Type := Get_Edges (L);
749 ER : constant Edge_Type := Get_Edges (R);
751 return EL.X < ER.X or else (EL.X = ER.X and then EL.Y < ER.Y);
758 procedure Move (From : Natural; To : Natural) is
760 Set_Edges (To, Get_Edges (From));
763 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
765 -- Start of processing for Compute_Edges_And_Vertices
768 -- We store edges from 1 to 2 * NK and leave zero alone in order to use
771 Edges_Len := 2 * NK + 1;
773 if Edges = No_Table then
774 Edges := Allocate (Edges_Len, Edge_Size);
777 if Vertices = No_Table then
778 Vertices := Allocate (NV, Vertex_Size);
781 for J in 0 .. NV - 1 loop
782 Set_Vertices (J, (No_Vertex, No_Vertex - 1));
785 -- For each w, X = f1 (w) and Y = f2 (w)
787 for J in 0 .. NK - 1 loop
792 X := Sum (WT.Table (Reduced (J)), T1, Opt);
793 Y := Sum (WT.Table (Reduced (J)), T2, Opt);
795 -- Discard T1 and T2 as soon as we discover a self loop
802 -- We store (X, Y) and (Y, X) to ease assignment step
804 Set_Edges (2 * J + 1, (X, Y, J));
805 Set_Edges (2 * J + 2, (Y, X, J));
808 -- Return an empty graph when self loop detected
815 Put_Edges (Output, "Unsorted Edge Table");
816 Put_Int_Matrix (Output, "Function Table 1", T1,
818 Put_Int_Matrix (Output, "Function Table 2", T2,
822 -- Enforce consistency between edges and keys. Construct Vertices and
823 -- compute the list of neighbors of a vertex First .. Last as Edges
824 -- is sorted by X and then Y. To compute the neighbor list, sort the
827 Sorting.Sort (Edges_Len - 1);
830 Put_Edges (Output, "Sorted Edge Table");
831 Put_Int_Matrix (Output, "Function Table 1", T1,
833 Put_Int_Matrix (Output, "Function Table 2", T2,
837 -- Edges valid range is 1 .. 2 * NK
839 for E in 1 .. Edges_Len - 1 loop
840 Edge := Get_Edges (E);
841 Key := Get_Key (Edge.Key);
843 if Key.Edge = No_Edge then
845 Set_Key (Edge.Key, Key);
848 Vertex := Get_Vertices (Edge.X);
850 if Vertex.First = No_Edge then
855 Set_Vertices (Edge.X, Vertex);
859 Put_Reduced_Keys (Output, "Key Table");
860 Put_Edges (Output, "Edge Table");
861 Put_Vertex_Table (Output, "Vertex Table");
864 end Compute_Edges_And_Vertices;
872 Item_Size : out Natural;
873 Length_1 : out Natural;
874 Length_2 : out Natural)
878 when Character_Position =>
880 Length_1 := Char_Pos_Set_Len;
883 when Used_Character_Set =>
888 when Function_Table_1
889 | Function_Table_2 =>
890 Item_Size := Type_Size (NV);
895 Item_Size := Type_Size (NK);
905 procedure Finalize is
908 Put (Output, "Finalize");
912 -- Deallocate all the WT components (both initial and reduced ones) to
913 -- avoid memory leaks.
915 for W in 0 .. WT.Last loop
917 -- Note: WT.Table (NK) is a temporary variable, do not free it since
918 -- this would cause a double free.
921 Free_Word (WT.Table (W));
928 -- Reset all variables for next usage
932 Char_Pos_Set := No_Table;
933 Char_Pos_Set_Len := 0;
935 Used_Char_Set := No_Table;
936 Used_Char_Set_Len := 0;
950 Vertices := No_Table;
958 ----------------------------
959 -- Generate_Mapping_Table --
960 ----------------------------
962 procedure Generate_Mapping_Table
966 Seed : in out Natural)
969 for J in 0 .. L1 - 1 loop
970 for K in 0 .. L2 - 1 loop
972 Set_Table (Tab, J, K, Seed mod NV);
975 end Generate_Mapping_Table;
977 -----------------------------
978 -- Generate_Mapping_Tables --
979 -----------------------------
981 procedure Generate_Mapping_Tables
983 Seed : in out Natural)
986 -- If T1 and T2 are already allocated no need to do it twice. Reuse them
987 -- as their size has not changed.
989 if T1 = No_Table and then T2 = No_Table then
991 Used_Char_Last : Natural := 0;
995 if Opt = CPU_Time then
996 for P in reverse Character'Range loop
997 Used_Char := Get_Used_Char (P);
998 if Used_Char /= 0 then
999 Used_Char_Last := Used_Char;
1005 T1_Len := Char_Pos_Set_Len;
1006 T2_Len := Used_Char_Last + 1;
1007 T1 := Allocate (T1_Len * T2_Len);
1008 T2 := Allocate (T1_Len * T2_Len);
1012 Generate_Mapping_Table (T1, T1_Len, T2_Len, Seed);
1013 Generate_Mapping_Table (T2, T1_Len, T2_Len, Seed);
1016 Put_Used_Char_Set (Output, "Used Character Set");
1017 Put_Int_Matrix (Output, "Function Table 1", T1,
1019 Put_Int_Matrix (Output, "Function Table 2", T2,
1022 end Generate_Mapping_Tables;
1028 function Get_Char_Pos (P : Natural) return Natural is
1029 N : constant Natural := Char_Pos_Set + P;
1031 return IT.Table (N);
1038 function Get_Edges (F : Natural) return Edge_Type is
1039 N : constant Natural := Edges + (F * Edge_Size);
1042 E.X := IT.Table (N);
1043 E.Y := IT.Table (N + 1);
1044 E.Key := IT.Table (N + 2);
1052 function Get_Graph (N : Natural) return Integer is
1054 return IT.Table (G + N);
1061 function Get_Key (N : Key_Id) return Key_Type is
1064 K.Edge := IT.Table (Keys + N);
1072 function Get_Table (T : Integer; X, Y : Natural) return Natural is
1073 N : constant Natural := T + (Y * T1_Len) + X;
1075 return IT.Table (N);
1082 function Get_Used_Char (C : Character) return Natural is
1083 N : constant Natural := Used_Char_Set + Character'Pos (C);
1085 return IT.Table (N);
1092 function Get_Vertices (F : Natural) return Vertex_Type is
1093 N : constant Natural := Vertices + (F * Vertex_Size);
1096 V.First := IT.Table (N);
1097 V.Last := IT.Table (N + 1);
1105 function Image (Int : Integer; W : Natural := 0) return String is
1106 B : String (1 .. 32);
1109 procedure Img (V : Natural);
1110 -- Compute image of V into B, starting at B (L), incrementing L
1116 procedure Img (V : Natural) is
1123 B (L) := Character'Val ((V mod 10) + Character'Pos ('0'));
1126 -- Start of processing for Image
1137 return Image (B (1 .. L), W);
1144 function Image (Str : String; W : Natural := 0) return String is
1145 Len : constant Natural := Str'Length;
1146 Max : Natural := Len;
1154 Buf : String (1 .. Max) := (1 .. Max => ' ');
1157 for J in 0 .. Len - 1 loop
1158 Buf (Max - Len + 1 + J) := Str (Str'First + J);
1169 function Initial (K : Key_Id) return Word_Id is
1178 procedure Initialize
1180 K_To_V : Float := Default_K_To_V;
1181 Optim : Optimization := Memory_Space;
1182 Tries : Positive := Default_Tries)
1186 Put (Output, "Initialize");
1190 -- Deallocate the part of the table concerning the reduced words.
1191 -- Initial words are already present in the table. We may have reduced
1192 -- words already there because a previous computation failed. We are
1193 -- currently retrying and the reduced words have to be deallocated.
1195 for W in Reduced (0) .. WT.Last loop
1196 Free_Word (WT.Table (W));
1201 -- Initialize of computation variables
1205 Char_Pos_Set := No_Table;
1206 Char_Pos_Set_Len := 0;
1208 Used_Char_Set := No_Table;
1209 Used_Char_Set_Len := 0;
1223 Vertices := No_Table;
1232 raise Program_Error with "K to V ratio cannot be lower than 2.0";
1235 -- Do not accept a value of K2V too close to 2.0 such that once
1236 -- rounded up, NV = 2 * NK because the algorithm would not converge.
1238 NV := Natural (Float (NK) * K2V);
1239 if NV <= 2 * NK then
1243 Keys := Allocate (NK);
1245 -- Resize initial words to have all of them at the same size
1246 -- (so the size of the largest one).
1248 for K in 0 .. NK - 1 loop
1249 Resize_Word (WT.Table (Initial (K)), Max_Key_Len);
1252 -- Allocated the table to store the reduced words. As WT is a
1253 -- GNAT.Table (using C memory management), pointers have to be
1254 -- explicitly initialized to null.
1256 WT.Set_Last (Reduced (NK - 1));
1258 -- Note: Reduced (0) = NK + 1
1260 WT.Table (NK) := null;
1262 for W in 0 .. NK - 1 loop
1263 WT.Table (Reduced (W)) := null;
1271 procedure Insert (Value : String) is
1272 Len : constant Natural := Value'Length;
1276 Put (Output, "Inserting """ & Value & """");
1280 for J in Value'Range loop
1281 pragma Assert (Value (J) /= ASCII.NUL);
1286 WT.Table (NK) := New_Word (Value);
1289 if Max_Key_Len < Len then
1293 if Min_Key_Len = 0 or else Len < Min_Key_Len then
1302 procedure New_Line (File : File_Descriptor) is
1304 if Write (File, EOL'Address, 1) /= 1 then
1305 raise Program_Error;
1313 function New_Word (S : String) return Word_Type is
1315 return new String'(S);
1318 ------------------------------
1319 -- Parse_Position_Selection --
1320 ------------------------------
1322 procedure Parse_Position_Selection (Argument : String) is
1323 N : Natural := Argument'First;
1324 L : constant Natural := Argument'Last;
1325 M : constant Natural := Max_Key_Len;
1327 T : array (1 .. M) of Boolean := (others => False);
1329 function Parse_Index return Natural;
1330 -- Parse argument starting at index N to find an index
1336 function Parse_Index return Natural is
1337 C : Character := Argument (N);
1346 if C not in '0' .. '9' then
1347 raise Program_Error with "cannot read position argument";
1350 while C in '0' .. '9' loop
1351 V := V * 10 + (Character'Pos (C) - Character'Pos ('0'));
1360 -- Start of processing for Parse_Position_Selection
1363 -- Empty specification means all the positions
1366 Char_Pos_Set_Len := M;
1367 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
1369 for C in 0 .. Char_Pos_Set_Len - 1 loop
1370 Set_Char_Pos (C, C + 1);
1376 First, Last : Natural;
1379 First := Parse_Index;
1384 if N <= L and then Argument (N) = '-' then
1386 Last := Parse_Index;
1389 -- Include the positions in the selection
1391 for J in First .. Last loop
1398 if Argument (N) /= ',' then
1399 raise Program_Error with "cannot read position argument";
1405 -- Compute position selection length
1408 for J in T'Range loop
1414 -- Fill position selection
1416 Char_Pos_Set_Len := N;
1417 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
1420 for J in T'Range loop
1422 Set_Char_Pos (N, J);
1427 end Parse_Position_Selection;
1434 (Pkg_Name : String := Default_Pkg_Name;
1435 Use_Stdout : Boolean := False)
1437 File : File_Descriptor := Standout;
1440 -- For call to Close
1442 function Array_Img (N, T, R1 : String; R2 : String := "") return String;
1443 -- Return string "N : constant array (R1[, R2]) of T;"
1445 function Range_Img (F, L : Natural; T : String := "") return String;
1446 -- Return string "[T range ]F .. L"
1448 function Type_Img (L : Natural) return String;
1449 -- Return the larger unsigned type T such that T'Last < L
1457 R2 : String := "") return String
1463 Add (" : constant array (");
1474 return Line (1 .. Last);
1481 function Range_Img (F, L : Natural; T : String := "") return String is
1482 FI : constant String := Image (F);
1483 FL : constant Natural := FI'Length;
1484 LI : constant String := Image (L);
1485 LL : constant Natural := LI'Length;
1486 TL : constant Natural := T'Length;
1487 RI : String (1 .. TL + 7 + FL + 4 + LL);
1492 RI (Len + 1 .. Len + TL) := T;
1494 RI (Len + 1 .. Len + 7) := " range ";
1498 RI (Len + 1 .. Len + FL) := FI;
1500 RI (Len + 1 .. Len + 4) := " .. ";
1502 RI (Len + 1 .. Len + LL) := LI;
1504 return RI (1 .. Len);
1511 function Type_Img (L : Natural) return String is
1512 S : constant String := Image (Type_Size (L));
1513 U : String := "Unsigned_ ";
1517 for J in S'Range loop
1529 FName : String := Ada_File_Base_Name (Pkg_Name) & ".ads";
1530 -- Initially, the name of the spec file, then modified to be the name of
1531 -- the body file. Not used if Use_Stdout is True.
1533 -- Start of processing for Produce
1537 if Verbose and then not Use_Stdout then
1539 "Producing " & Ada.Directories.Current_Directory & "/" & FName);
1543 if not Use_Stdout then
1544 File := Create_File (FName, Binary);
1546 if File = Invalid_FD then
1547 raise Program_Error with "cannot create: " & FName;
1551 Put (File, "package ");
1552 Put (File, Pkg_Name);
1555 Put (File, " function Hash (S : String) return Natural;");
1558 Put (File, Pkg_Name);
1562 if not Use_Stdout then
1563 Close (File, Status);
1570 if not Use_Stdout then
1572 -- Set to body file name
1574 FName (FName'Last) := 'b';
1576 File := Create_File (FName, Binary);
1578 if File = Invalid_FD then
1579 raise Program_Error with "cannot create: " & FName;
1583 Put (File, "with Interfaces; use Interfaces;");
1586 Put (File, "package body ");
1587 Put (File, Pkg_Name);
1592 if Opt = CPU_Time then
1593 Put (File, Array_Img ("C", Type_Img (256), "Character"));
1596 F := Character'Pos (Character'First);
1597 L := Character'Pos (Character'Last);
1599 for J in Character'Range loop
1600 P := Get_Used_Char (J);
1601 Put (File, Image (P), 1, 0, 1, F, L, Character'Pos (J));
1608 L := Char_Pos_Set_Len - 1;
1610 Put (File, Array_Img ("P", "Natural", Range_Img (F, L)));
1613 for J in F .. L loop
1614 Put (File, Image (Get_Char_Pos (J)), 1, 0, 1, F, L, J);
1623 Array_Img ("T1", Type_Img (NV),
1624 Range_Img (0, T1_Len - 1),
1625 Range_Img (0, T2_Len - 1, Type_Img (256))),
1626 T1, T1_Len, T2_Len);
1628 when Memory_Space =>
1631 Array_Img ("T1", Type_Img (NV),
1632 Range_Img (0, T1_Len - 1)),
1642 Array_Img ("T2", Type_Img (NV),
1643 Range_Img (0, T1_Len - 1),
1644 Range_Img (0, T2_Len - 1, Type_Img (256))),
1645 T2, T1_Len, T2_Len);
1647 when Memory_Space =>
1650 Array_Img ("T2", Type_Img (NV),
1651 Range_Img (0, T1_Len - 1)),
1659 Array_Img ("G", Type_Img (NK),
1660 Range_Img (0, G_Len - 1)),
1664 Put (File, " function Hash (S : String) return Natural is");
1666 Put (File, " F : constant Natural := S'First - 1;");
1668 Put (File, " L : constant Natural := S'Length;");
1670 Put (File, " F1, F2 : Natural := 0;");
1673 Put (File, " J : ");
1677 Put (File, Type_Img (256));
1678 when Memory_Space =>
1679 Put (File, "Natural");
1685 Put (File, " begin");
1687 Put (File, " for K in P'Range loop");
1689 Put (File, " exit when L < P (K);");
1691 Put (File, " J := ");
1696 when Memory_Space =>
1697 Put (File, "Character'Pos");
1700 Put (File, " (S (P (K) + F));");
1703 Put (File, " F1 := (F1 + Natural (T1 (K");
1705 if Opt = CPU_Time then
1711 if Opt = Memory_Space then
1715 Put (File, ") mod ");
1716 Put (File, Image (NV));
1720 Put (File, " F2 := (F2 + Natural (T2 (K");
1722 if Opt = CPU_Time then
1728 if Opt = Memory_Space then
1732 Put (File, ") mod ");
1733 Put (File, Image (NV));
1737 Put (File, " end loop;");
1741 " return (Natural (G (F1)) + Natural (G (F2))) mod ");
1743 Put (File, Image (NK));
1746 Put (File, " end Hash;");
1750 Put (File, Pkg_Name);
1754 if not Use_Stdout then
1755 Close (File, Status);
1767 procedure Put (File : File_Descriptor; Str : String) is
1768 Len : constant Natural := Str'Length;
1770 for J in Str'Range loop
1771 pragma Assert (Str (J) /= ASCII.NUL);
1775 if Write (File, Str'Address, Len) /= Len then
1776 raise Program_Error;
1785 (F : File_Descriptor;
1794 Len : constant Natural := S'Length;
1797 -- Write current line, followed by LF
1805 Put (F, Line (1 .. Last));
1810 -- Start of processing for Put
1813 if C1 = F1 and then C2 = F2 then
1817 if Last + Len + 3 >= Max then
1825 if C1 = F1 and then C2 = F2 then
1877 procedure Put_Edges (File : File_Descriptor; Title : String) is
1879 F1 : constant Natural := 1;
1880 L1 : constant Natural := Edges_Len - 1;
1881 M : constant Natural := Max / 5;
1887 -- Edges valid range is 1 .. Edge_Len - 1
1889 for J in F1 .. L1 loop
1891 Put (File, Image (J, M), F1, L1, J, 1, 4, 1);
1892 Put (File, Image (E.X, M), F1, L1, J, 1, 4, 2);
1893 Put (File, Image (E.Y, M), F1, L1, J, 1, 4, 3);
1894 Put (File, Image (E.Key, M), F1, L1, J, 1, 4, 4);
1898 ----------------------
1899 -- Put_Initial_Keys --
1900 ----------------------
1902 procedure Put_Initial_Keys (File : File_Descriptor; Title : String) is
1903 F1 : constant Natural := 0;
1904 L1 : constant Natural := NK - 1;
1905 M : constant Natural := Max / 5;
1912 for J in F1 .. L1 loop
1914 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
1915 Put (File, Image (K.Edge, M), F1, L1, J, 1, 3, 2);
1916 Put (File, Trim_Trailing_Nuls (WT.Table (Initial (J)).all),
1917 F1, L1, J, 1, 3, 3);
1919 end Put_Initial_Keys;
1921 --------------------
1922 -- Put_Int_Matrix --
1923 --------------------
1925 procedure Put_Int_Matrix
1926 (File : File_Descriptor;
1932 F1 : constant Integer := 0;
1933 L1 : constant Integer := Len_1 - 1;
1934 F2 : constant Integer := 0;
1935 L2 : constant Integer := Len_2 - 1;
1943 for J in F1 .. L1 loop
1944 Ix := IT.Table (Table + J);
1945 Put (File, Image (Ix), 1, 0, 1, F1, L1, J);
1949 for J in F1 .. L1 loop
1950 for K in F2 .. L2 loop
1951 Ix := IT.Table (Table + J + K * Len_1);
1952 Put (File, Image (Ix), F1, L1, J, F2, L2, K);
1958 --------------------
1959 -- Put_Int_Vector --
1960 --------------------
1962 procedure Put_Int_Vector
1963 (File : File_Descriptor;
1968 F2 : constant Natural := 0;
1969 L2 : constant Natural := Length - 1;
1975 for J in F2 .. L2 loop
1976 Put (File, Image (IT.Table (Vector + J)), 1, 0, 1, F2, L2, J);
1980 ----------------------
1981 -- Put_Reduced_Keys --
1982 ----------------------
1984 procedure Put_Reduced_Keys (File : File_Descriptor; Title : String) is
1985 F1 : constant Natural := 0;
1986 L1 : constant Natural := NK - 1;
1987 M : constant Natural := Max / 5;
1994 for J in F1 .. L1 loop
1996 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
1997 Put (File, Image (K.Edge, M), F1, L1, J, 1, 3, 2);
1998 Put (File, Trim_Trailing_Nuls (WT.Table (Reduced (J)).all),
1999 F1, L1, J, 1, 3, 3);
2001 end Put_Reduced_Keys;
2003 -----------------------
2004 -- Put_Used_Char_Set --
2005 -----------------------
2007 procedure Put_Used_Char_Set (File : File_Descriptor; Title : String) is
2008 F : constant Natural := Character'Pos (Character'First);
2009 L : constant Natural := Character'Pos (Character'Last);
2015 for J in Character'Range loop
2017 (File, Image (Get_Used_Char (J)), 1, 0, 1, F, L, Character'Pos (J));
2019 end Put_Used_Char_Set;
2021 ----------------------
2022 -- Put_Vertex_Table --
2023 ----------------------
2025 procedure Put_Vertex_Table (File : File_Descriptor; Title : String) is
2026 F1 : constant Natural := 0;
2027 L1 : constant Natural := NV - 1;
2028 M : constant Natural := Max / 4;
2035 for J in F1 .. L1 loop
2036 V := Get_Vertices (J);
2037 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
2038 Put (File, Image (V.First, M), F1, L1, J, 1, 3, 2);
2039 Put (File, Image (V.Last, M), F1, L1, J, 1, 3, 3);
2041 end Put_Vertex_Table;
2047 procedure Random (Seed : in out Natural) is
2049 -- Park & Miller Standard Minimal using Schrage's algorithm to avoid
2050 -- overflow: Xn+1 = 16807 * Xn mod (2 ** 31 - 1)
2057 R := Seed mod 127773;
2059 X := 16807 * R - 2836 * Q;
2061 Seed := (if X < 0 then X + 2147483647 else X);
2068 function Reduced (K : Key_Id) return Word_Id is
2077 procedure Resize_Word (W : in out Word_Type; Len : Natural) is
2078 S1 : constant String := W.all;
2079 S2 : String (1 .. Len) := (others => ASCII.NUL);
2080 L : constant Natural := S1'Length;
2089 --------------------------
2090 -- Select_Char_Position --
2091 --------------------------
2093 procedure Select_Char_Position is
2095 type Vertex_Table_Type is array (Natural range <>) of Vertex_Type;
2097 procedure Build_Identical_Keys_Sets
2098 (Table : in out Vertex_Table_Type;
2099 Last : in out Natural;
2101 -- Build a list of keys subsets that are identical with the current
2102 -- position selection plus Pos. Once this routine is called, reduced
2103 -- words are sorted by subsets and each item (First, Last) in Sets
2104 -- defines the range of identical keys.
2105 -- Need comment saying exactly what Last is ???
2107 function Count_Different_Keys
2108 (Table : Vertex_Table_Type;
2110 Pos : Natural) return Natural;
2111 -- For each subset in Sets, count the number of different keys if we add
2112 -- Pos to the current position selection.
2114 Sel_Position : IT.Table_Type (1 .. Max_Key_Len);
2115 Last_Sel_Pos : Natural := 0;
2116 Max_Sel_Pos : Natural := 0;
2118 -------------------------------
2119 -- Build_Identical_Keys_Sets --
2120 -------------------------------
2122 procedure Build_Identical_Keys_Sets
2123 (Table : in out Vertex_Table_Type;
2124 Last : in out Natural;
2127 S : constant Vertex_Table_Type := Table (Table'First .. Last);
2128 C : constant Natural := Pos;
2129 -- Shortcuts (why are these not renames ???)
2133 -- First and last words of a subset
2136 -- GNAT.Heap_Sort assumes that the first array index is 1. Offset
2137 -- defines the translation to operate.
2139 function Lt (L, R : Natural) return Boolean;
2140 procedure Move (From : Natural; To : Natural);
2141 -- Subprograms needed by GNAT.Heap_Sort_G
2147 function Lt (L, R : Natural) return Boolean is
2148 C : constant Natural := Pos;
2155 Right := Offset + R;
2161 Right := Offset + R;
2164 return WT.Table (Left)(C) < WT.Table (Right)(C);
2171 procedure Move (From : Natural; To : Natural) is
2172 Target, Source : Natural;
2177 Target := Offset + To;
2179 Source := Offset + From;
2182 Source := Offset + From;
2183 Target := Offset + To;
2186 WT.Table (Target) := WT.Table (Source);
2187 WT.Table (Source) := null;
2190 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
2192 -- Start of processing for Build_Identical_Key_Sets
2197 -- For each subset in S, extract the new subsets we have by adding C
2198 -- in the position selection.
2200 for J in S'Range loop
2201 if S (J).First = S (J).Last then
2205 Table (Last) := (F, L);
2208 Offset := Reduced (S (J).First) - 1;
2209 Sorting.Sort (S (J).Last - S (J).First + 1);
2213 for N in S (J).First .. S (J).Last loop
2215 -- For the last item, close the last subset
2217 if N = S (J).Last then
2219 Table (Last) := (F, N);
2221 -- Two contiguous words are identical when they have the
2222 -- same Cth character.
2224 elsif WT.Table (Reduced (N))(C) =
2225 WT.Table (Reduced (N + 1))(C)
2229 -- Find a new subset of identical keys. Store the current
2230 -- one and create a new subset.
2234 Table (Last) := (F, L);
2241 end Build_Identical_Keys_Sets;
2243 --------------------------
2244 -- Count_Different_Keys --
2245 --------------------------
2247 function Count_Different_Keys
2248 (Table : Vertex_Table_Type;
2250 Pos : Natural) return Natural
2252 N : array (Character) of Natural;
2257 -- For each subset, count the number of words that are still
2258 -- different when we include Pos in the position selection. Only
2259 -- focus on this position as the other positions already produce
2262 for S in 1 .. Last loop
2264 -- Count the occurrences of the different characters
2267 for K in Table (S).First .. Table (S).Last loop
2268 C := WT.Table (Reduced (K))(Pos);
2272 -- Update the number of different keys. Each character used
2273 -- denotes a different key.
2275 for J in N'Range loop
2283 end Count_Different_Keys;
2285 -- Start of processing for Select_Char_Position
2288 -- Initialize the reduced words set
2290 for K in 0 .. NK - 1 loop
2291 WT.Table (Reduced (K)) := New_Word (WT.Table (Initial (K)).all);
2295 Differences : Natural;
2296 Max_Differences : Natural := 0;
2297 Old_Differences : Natural;
2298 Max_Diff_Sel_Pos : Natural := 0; -- init to kill warning
2299 Max_Diff_Sel_Pos_Idx : Natural := 0; -- init to kill warning
2300 Same_Keys_Sets_Table : Vertex_Table_Type (1 .. NK);
2301 Same_Keys_Sets_Last : Natural := 1;
2304 for C in Sel_Position'Range loop
2305 Sel_Position (C) := C;
2308 Same_Keys_Sets_Table (1) := (0, NK - 1);
2311 -- Preserve maximum number of different keys and check later on
2312 -- that this value is strictly incrementing. Otherwise, it means
2313 -- that two keys are strictly identical.
2315 Old_Differences := Max_Differences;
2317 -- The first position should not exceed the minimum key length.
2318 -- Otherwise, we may end up with an empty word once reduced.
2321 (if Last_Sel_Pos = 0 then Min_Key_Len else Max_Key_Len);
2323 -- Find which position increases more the number of differences
2325 for J in Last_Sel_Pos + 1 .. Max_Sel_Pos loop
2326 Differences := Count_Different_Keys
2327 (Same_Keys_Sets_Table,
2328 Same_Keys_Sets_Last,
2333 "Selecting position" & Sel_Position (J)'Img &
2334 " results in" & Differences'Img &
2339 if Differences > Max_Differences then
2340 Max_Differences := Differences;
2341 Max_Diff_Sel_Pos := Sel_Position (J);
2342 Max_Diff_Sel_Pos_Idx := J;
2346 if Old_Differences = Max_Differences then
2347 raise Program_Error with "some keys are identical";
2350 -- Insert selected position and sort Sel_Position table
2352 Last_Sel_Pos := Last_Sel_Pos + 1;
2353 Sel_Position (Last_Sel_Pos + 1 .. Max_Diff_Sel_Pos_Idx) :=
2354 Sel_Position (Last_Sel_Pos .. Max_Diff_Sel_Pos_Idx - 1);
2355 Sel_Position (Last_Sel_Pos) := Max_Diff_Sel_Pos;
2357 for P in 1 .. Last_Sel_Pos - 1 loop
2358 if Max_Diff_Sel_Pos < Sel_Position (P) then
2359 Sel_Position (P + 1 .. Last_Sel_Pos) :=
2360 Sel_Position (P .. Last_Sel_Pos - 1);
2361 Sel_Position (P) := Max_Diff_Sel_Pos;
2366 exit when Max_Differences = NK;
2368 Build_Identical_Keys_Sets
2369 (Same_Keys_Sets_Table,
2370 Same_Keys_Sets_Last,
2375 "Selecting position" & Max_Diff_Sel_Pos'Img &
2376 " results in" & Max_Differences'Img &
2381 for J in 1 .. Same_Keys_Sets_Last loop
2383 Same_Keys_Sets_Table (J).First ..
2384 Same_Keys_Sets_Table (J).Last
2387 Trim_Trailing_Nuls (WT.Table (Reduced (K)).all));
2397 Char_Pos_Set_Len := Last_Sel_Pos;
2398 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
2400 for C in 1 .. Last_Sel_Pos loop
2401 Set_Char_Pos (C - 1, Sel_Position (C));
2403 end Select_Char_Position;
2405 --------------------------
2406 -- Select_Character_Set --
2407 --------------------------
2409 procedure Select_Character_Set is
2410 Last : Natural := 0;
2411 Used : array (Character) of Boolean := (others => False);
2415 for J in 0 .. NK - 1 loop
2416 for K in 0 .. Char_Pos_Set_Len - 1 loop
2417 Char := WT.Table (Initial (J))(Get_Char_Pos (K));
2418 exit when Char = ASCII.NUL;
2419 Used (Char) := True;
2423 Used_Char_Set_Len := 256;
2424 Used_Char_Set := Allocate (Used_Char_Set_Len);
2426 for J in Used'Range loop
2428 Set_Used_Char (J, Last);
2431 Set_Used_Char (J, 0);
2434 end Select_Character_Set;
2440 procedure Set_Char_Pos (P : Natural; Item : Natural) is
2441 N : constant Natural := Char_Pos_Set + P;
2443 IT.Table (N) := Item;
2450 procedure Set_Edges (F : Natural; Item : Edge_Type) is
2451 N : constant Natural := Edges + (F * Edge_Size);
2453 IT.Table (N) := Item.X;
2454 IT.Table (N + 1) := Item.Y;
2455 IT.Table (N + 2) := Item.Key;
2462 procedure Set_Graph (N : Natural; Item : Integer) is
2464 IT.Table (G + N) := Item;
2471 procedure Set_Key (N : Key_Id; Item : Key_Type) is
2473 IT.Table (Keys + N) := Item.Edge;
2480 procedure Set_Table (T : Integer; X, Y : Natural; Item : Natural) is
2481 N : constant Natural := T + ((Y * T1_Len) + X);
2483 IT.Table (N) := Item;
2490 procedure Set_Used_Char (C : Character; Item : Natural) is
2491 N : constant Natural := Used_Char_Set + Character'Pos (C);
2493 IT.Table (N) := Item;
2500 procedure Set_Vertices (F : Natural; Item : Vertex_Type) is
2501 N : constant Natural := Vertices + (F * Vertex_Size);
2503 IT.Table (N) := Item.First;
2504 IT.Table (N + 1) := Item.Last;
2514 Opt : Optimization) return Natural
2522 for J in 0 .. T1_Len - 1 loop
2523 exit when Word (J + 1) = ASCII.NUL;
2524 R := Get_Table (Table, J, Get_Used_Char (Word (J + 1)));
2525 S := (S + R) mod NV;
2528 when Memory_Space =>
2529 for J in 0 .. T1_Len - 1 loop
2530 exit when Word (J + 1) = ASCII.NUL;
2531 R := Get_Table (Table, J, 0);
2532 S := (S + R * Character'Pos (Word (J + 1))) mod NV;
2539 ------------------------
2540 -- Trim_Trailing_Nuls --
2541 ------------------------
2543 function Trim_Trailing_Nuls (Str : String) return String is
2545 for J in reverse Str'Range loop
2546 if Str (J) /= ASCII.NUL then
2547 return Str (Str'First .. J);
2552 end Trim_Trailing_Nuls;
2558 function Type_Size (L : Natural) return Natural is
2562 elsif L <= 2 ** 16 then
2576 K : Natural := 0) return Natural
2580 when Character_Position =>
2581 return Get_Char_Pos (J);
2583 when Used_Character_Set =>
2584 return Get_Used_Char (Character'Val (J));
2586 when Function_Table_1 =>
2587 return Get_Table (T1, J, K);
2589 when Function_Table_2 =>
2590 return Get_Table (T2, J, K);
2593 return Get_Graph (J);
2598 end GNAT.Perfect_Hash_Generators;