Daily bump.
[official-gcc.git] / gcc / ada / g-pehage.adb
blob6d9670f69f8ce19991cb4ca94989e641d0c514bc
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . P E R F E C T _ H A S H _ G E N E R A T O R S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2002-2007, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Ada.Exceptions; use Ada.Exceptions;
35 with Ada.IO_Exceptions; use Ada.IO_Exceptions;
37 with GNAT.Heap_Sort_G;
38 with GNAT.OS_Lib; use GNAT.OS_Lib;
39 with GNAT.Table;
41 package body GNAT.Perfect_Hash_Generators is
43 -- We are using the algorithm of J. Czech as described in Zbigniew J.
44 -- Czech, George Havas, and Bohdan S. Majewski ``An Optimal Algorithm for
45 -- Generating Minimal Perfect Hash Functions'', Information Processing
46 -- Letters, 43(1992) pp.257-264, Oct.1992
48 -- This minimal perfect hash function generator is based on random graphs
49 -- and produces a hash function of the form:
51 -- h (w) = (g (f1 (w)) + g (f2 (w))) mod m
53 -- where f1 and f2 are functions that map strings into integers, and g is a
54 -- function that maps integers into [0, m-1]. h can be order preserving.
55 -- For instance, let W = {w_0, ..., w_i, ...,
56 -- w_m-1}, h can be defined such that h (w_i) = i.
58 -- This algorithm defines two possible constructions of f1 and f2. Method
59 -- b) stores the hash function in less memory space at the expense of
60 -- greater CPU time.
62 -- a) fk (w) = sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n
64 -- size (Tk) = max (for w in W) (length (w)) * size (used char set)
66 -- b) fk (w) = sum (for i in 1 .. length (w)) (Tk (i) * w (i)) mod n
68 -- size (Tk) = max (for w in W) (length (w)) but the table lookups are
69 -- replaced by multiplications.
71 -- where Tk values are randomly generated. n is defined later on but the
72 -- algorithm recommends to use a value a little bit greater than 2m. Note
73 -- that for large values of m, the main memory space requirements comes
74 -- from the memory space for storing function g (>= 2m entries).
76 -- Random graphs are frequently used to solve difficult problems that do
77 -- not have polynomial solutions. This algorithm is based on a weighted
78 -- undirected graph. It comprises two steps: mapping and assigment.
80 -- In the mapping step, a graph G = (V, E) is constructed, where = {0, 1,
81 -- ..., n-1} and E = {(for w in W) (f1 (w), f2 (w))}. In order for the
82 -- assignment step to be successful, G has to be acyclic. To have a high
83 -- probability of generating an acyclic graph, n >= 2m. If it is not
84 -- acyclic, Tk have to be regenerated.
86 -- In the assignment step, the algorithm builds function g. As is acyclic,
87 -- there is a vertex v1 with only one neighbor v2. Let w_i be the word such
88 -- that v1 = f1 (w_i) and v2 = f2 (w_i). Let g (v1) = 0 by construction and
89 -- g (v2) = (i - g (v1)) mod n (or to be general, (h (i) - g (v1) mod n).
90 -- If word w_j is such that v2 = f1 (w_j) and v3 = f2 (w_j), g (v3) = (j -
91 -- g (v2)) mod (or to be general, (h (j) - g (v2)) mod n). If w_i has no
92 -- neighbor, then another vertex is selected. The algorithm traverses G to
93 -- assign values to all the vertices. It cannot assign a value to an
94 -- already assigned vertex as G is acyclic.
96 subtype Word_Id is Integer;
97 subtype Key_Id is Integer;
98 subtype Vertex_Id is Integer;
99 subtype Edge_Id is Integer;
100 subtype Table_Id is Integer;
102 No_Vertex : constant Vertex_Id := -1;
103 No_Edge : constant Edge_Id := -1;
104 No_Table : constant Table_Id := -1;
106 Max_Word_Length : constant := 32;
107 subtype Word_Type is String (1 .. Max_Word_Length);
108 Null_Word : constant Word_Type := (others => ASCII.NUL);
109 -- Store keyword in a word. Note that the length of word is limited to 32
110 -- characters.
112 type Key_Type is record
113 Edge : Edge_Id;
114 end record;
115 -- A key corresponds to an edge in the algorithm graph
117 type Vertex_Type is record
118 First : Edge_Id;
119 Last : Edge_Id;
120 end 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
125 X : Vertex_Id;
126 Y : Vertex_Id;
127 Key : Key_Id;
128 end record;
129 -- An edge is a peer of vertices. In the algorithm, a key is associated to
130 -- an edge.
132 package WT is new GNAT.Table (Word_Type, Word_Id, 0, 32, 32);
133 package IT is new GNAT.Table (Integer, Integer, 0, 32, 32);
134 -- The two main tables. IT is used to store several tables of components
135 -- containing only integers.
137 function Image (Int : Integer; W : Natural := 0) return String;
138 function Image (Str : String; W : Natural := 0) return String;
139 -- Return a string which includes string Str or integer Int preceded by
140 -- leading spaces if required by width W.
142 Output : File_Descriptor renames GNAT.OS_Lib.Standout;
143 -- Shortcuts
145 EOL : constant Character := ASCII.LF;
147 Max : constant := 78;
148 Last : Natural := 0;
149 Line : String (1 .. Max);
150 -- Use this line to provide buffered IO
152 procedure Add (C : Character);
153 procedure Add (S : String);
154 -- Add a character or a string in Line and update Last
156 procedure Put
157 (F : File_Descriptor;
158 S : String;
159 F1 : Natural;
160 L1 : Natural;
161 C1 : Natural;
162 F2 : Natural;
163 L2 : Natural;
164 C2 : Natural);
165 -- Write string S into file F as a element of an array of one or two
166 -- dimensions. Fk (resp. Lk and Ck) indicates the first (resp last and
167 -- current) index in the k-th dimension. If F1 = L1 the array is considered
168 -- as a one dimension array. This dimension is described by F2 and L2. This
169 -- routine takes care of all the parenthesis, spaces and commas needed to
170 -- format correctly the array. Moreover, the array is well indented and is
171 -- wrapped to fit in a 80 col line. When the line is full, the routine
172 -- writes it into file F. When the array is completed, the routine adds
173 -- semi-colon and writes the line into file F.
175 procedure New_Line (File : File_Descriptor);
176 -- Simulate Ada.Text_IO.New_Line with GNAT.OS_Lib
178 procedure Put (File : File_Descriptor; Str : String);
179 -- Simulate Ada.Text_IO.Put with GNAT.OS_Lib
181 procedure Put_Used_Char_Set (File : File_Descriptor; Title : String);
182 -- Output a title and a used character set
184 procedure Put_Int_Vector
185 (File : File_Descriptor;
186 Title : String;
187 Vector : Integer;
188 Length : Natural);
189 -- Output a title and a vector
191 procedure Put_Int_Matrix
192 (File : File_Descriptor;
193 Title : String;
194 Table : Table_Id;
195 Len_1 : Natural;
196 Len_2 : Natural);
197 -- Output a title and a matrix. When the matrix has only one non-empty
198 -- dimension (Len_2 = 0), output a vector.
200 procedure Put_Edges (File : File_Descriptor; Title : String);
201 -- Output a title and an edge table
203 procedure Put_Initial_Keys (File : File_Descriptor; Title : String);
204 -- Output a title and a key table
206 procedure Put_Reduced_Keys (File : File_Descriptor; Title : String);
207 -- Output a title and a key table
209 procedure Put_Vertex_Table (File : File_Descriptor; Title : String);
210 -- Output a title and a vertex table
212 ----------------------------------
213 -- Character Position Selection --
214 ----------------------------------
216 -- We reduce the maximum key size by selecting representative positions
217 -- in these keys. We build a matrix with one word per line. We fill the
218 -- remaining space of a line with ASCII.NUL. The heuristic selects the
219 -- position that induces the minimum number of collisions. If there are
220 -- collisions, select another position on the reduced key set responsible
221 -- of the collisions. Apply the heuristic until there is no more collision.
223 procedure Apply_Position_Selection;
224 -- Apply Position selection and build the reduced key table
226 procedure Parse_Position_Selection (Argument : String);
227 -- Parse Argument and compute the position set. Argument is list of
228 -- substrings separated by commas. Each substring represents a position
229 -- or a range of positions (like x-y).
231 procedure Select_Character_Set;
232 -- Define an optimized used character set like Character'Pos in order not
233 -- to allocate tables of 256 entries.
235 procedure Select_Char_Position;
236 -- Find a min char position set in order to reduce the max key length. The
237 -- heuristic selects the position that induces the minimum number of
238 -- collisions. If there are collisions, select another position on the
239 -- reduced key set responsible of the collisions. Apply the heuristic until
240 -- there is no collision.
242 -----------------------------
243 -- Random Graph Generation --
244 -----------------------------
246 procedure Random (Seed : in out Natural);
247 -- Simulate Ada.Discrete_Numerics.Random
249 procedure Generate_Mapping_Table
250 (Tab : Table_Id;
251 L1 : Natural;
252 L2 : Natural;
253 Seed : in out Natural);
254 -- Random generation of the tables below. T is already allocated
256 procedure Generate_Mapping_Tables
257 (Opt : Optimization;
258 Seed : in out Natural);
259 -- Generate the mapping tables T1 and T2. They are used to define fk (w) =
260 -- sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n. Keys, NK and Chars
261 -- are used to compute the matrix size.
263 ---------------------------
264 -- Algorithm Computation --
265 ---------------------------
267 procedure Compute_Edges_And_Vertices (Opt : Optimization);
268 -- Compute the edge and vertex tables. These are empty when a self loop is
269 -- detected (f1 (w) = f2 (w)). The edge table is sorted by X value and then
270 -- Y value. Keys is the key table and NK the number of keys. Chars is the
271 -- set of characters really used in Keys. NV is the number of vertices
272 -- recommended by the algorithm. T1 and T2 are the mapping tables needed to
273 -- compute f1 (w) and f2 (w).
275 function Acyclic return Boolean;
276 -- Return True when the graph is acyclic. Vertices is the current vertex
277 -- table and Edges the current edge table.
279 procedure Assign_Values_To_Vertices;
280 -- Execute the assignment step of the algorithm. Keys is the current key
281 -- table. Vertices and Edges represent the random graph. G is the result of
282 -- the assignment step such that:
283 -- h (w) = (g (f1 (w)) + g (f2 (w))) mod m
285 function Sum
286 (Word : Word_Type;
287 Table : Table_Id;
288 Opt : Optimization) return Natural;
289 -- For an optimization of CPU_Time return
290 -- fk (w) = sum (for i in 1 .. length (w)) (Tk (i, w (i))) mod n
291 -- For an optimization of Memory_Space return
292 -- fk (w) = sum (for i in 1 .. length (w)) (Tk (i) * w (i)) mod n
293 -- Here NV = n
295 -------------------------------
296 -- Internal Table Management --
297 -------------------------------
299 function Allocate (N : Natural; S : Natural := 1) return Table_Id;
300 -- Allocate N * S ints from IT table
302 procedure Free_Tmp_Tables;
303 -- Deallocate the tables used by the algorithm (but not the keys table)
305 ----------
306 -- Keys --
307 ----------
309 Keys : Table_Id := No_Table;
310 NK : Natural := 0;
311 -- NK : Number of Keys
313 function Initial (K : Key_Id) return Word_Id;
314 pragma Inline (Initial);
316 function Reduced (K : Key_Id) return Word_Id;
317 pragma Inline (Reduced);
319 function Get_Key (N : Key_Id) return Key_Type;
320 procedure Set_Key (N : Key_Id; Item : Key_Type);
321 -- Get or Set Nth element of Keys table
323 ------------------
324 -- Char_Pos_Set --
325 ------------------
327 Char_Pos_Set : Table_Id := No_Table;
328 Char_Pos_Set_Len : Natural;
329 -- Character Selected Position Set
331 function Get_Char_Pos (P : Natural) return Natural;
332 procedure Set_Char_Pos (P : Natural; Item : Natural);
333 -- Get or Set the string position of the Pth selected character
335 -------------------
336 -- Used_Char_Set --
337 -------------------
339 Used_Char_Set : Table_Id := No_Table;
340 Used_Char_Set_Len : Natural;
341 -- Used Character Set : Define a new character mapping. When all the
342 -- characters are not present in the keys, in order to reduce the size
343 -- of some tables, we redefine the character mapping.
345 function Get_Used_Char (C : Character) return Natural;
346 procedure Set_Used_Char (C : Character; Item : Natural);
348 ------------
349 -- Tables --
350 ------------
352 T1 : Table_Id := No_Table;
353 T2 : Table_Id := No_Table;
354 T1_Len : Natural;
355 T2_Len : Natural;
356 -- T1 : Values table to compute F1
357 -- T2 : Values table to compute F2
359 function Get_Table (T : Integer; X, Y : Natural) return Natural;
360 procedure Set_Table (T : Integer; X, Y : Natural; Item : Natural);
362 -----------
363 -- Graph --
364 -----------
366 G : Table_Id := No_Table;
367 G_Len : Natural;
368 -- Values table to compute G
370 NT : Natural := Default_Tries;
371 -- Number of tries running the algorithm before raising an error
373 function Get_Graph (N : Natural) return Integer;
374 procedure Set_Graph (N : Natural; Item : Integer);
375 -- Get or Set Nth element of graph
377 -----------
378 -- Edges --
379 -----------
381 Edge_Size : constant := 3;
382 Edges : Table_Id := No_Table;
383 Edges_Len : Natural;
384 -- Edges : Edge table of the random graph G
386 function Get_Edges (F : Natural) return Edge_Type;
387 procedure Set_Edges (F : Natural; Item : Edge_Type);
389 --------------
390 -- Vertices --
391 --------------
393 Vertex_Size : constant := 2;
395 Vertices : Table_Id := No_Table;
396 -- Vertex table of the random graph G
398 NV : Natural;
399 -- Number of Vertices
401 function Get_Vertices (F : Natural) return Vertex_Type;
402 procedure Set_Vertices (F : Natural; Item : Vertex_Type);
403 -- Comments needed ???
405 K2V : Float;
406 -- Ratio between Keys and Vertices (parameter of Czech's algorithm)
408 Opt : Optimization;
409 -- Optimization mode (memory vs CPU)
411 Max_Key_Len : Natural := 0;
412 Min_Key_Len : Natural := Max_Word_Length;
413 -- Maximum and minimum of all the word length
415 S : Natural;
416 -- Seed
418 function Type_Size (L : Natural) return Natural;
419 -- Given the last L of an unsigned integer type T, return its size
421 -------------
422 -- Acyclic --
423 -------------
425 function Acyclic return Boolean is
426 Marks : array (0 .. NV - 1) of Vertex_Id := (others => No_Vertex);
428 function Traverse (Edge : Edge_Id; Mark : Vertex_Id) return Boolean;
429 -- Propagate Mark from X to Y. X is already marked. Mark Y and propagate
430 -- it to the edges of Y except the one representing the same key. Return
431 -- False when Y is marked with Mark.
433 --------------
434 -- Traverse --
435 --------------
437 function Traverse (Edge : Edge_Id; Mark : Vertex_Id) return Boolean is
438 E : constant Edge_Type := Get_Edges (Edge);
439 K : constant Key_Id := E.Key;
440 Y : constant Vertex_Id := E.Y;
441 M : constant Vertex_Id := Marks (E.Y);
442 V : Vertex_Type;
444 begin
445 if M = Mark then
446 return False;
448 elsif M = No_Vertex then
449 Marks (Y) := Mark;
450 V := Get_Vertices (Y);
452 for J in V.First .. V.Last loop
454 -- Do not propagate to the edge representing the same key
456 if Get_Edges (J).Key /= K
457 and then not Traverse (J, Mark)
458 then
459 return False;
460 end if;
461 end loop;
462 end if;
464 return True;
465 end Traverse;
467 Edge : Edge_Type;
469 -- Start of processing for Acyclic
471 begin
472 -- Edges valid range is
474 for J in 1 .. Edges_Len - 1 loop
476 Edge := Get_Edges (J);
478 -- Mark X of E when it has not been already done
480 if Marks (Edge.X) = No_Vertex then
481 Marks (Edge.X) := Edge.X;
482 end if;
484 -- Traverse E when this has not already been done
486 if Marks (Edge.Y) = No_Vertex
487 and then not Traverse (J, Edge.X)
488 then
489 return False;
490 end if;
491 end loop;
493 return True;
494 end Acyclic;
496 ---------
497 -- Add --
498 ---------
500 procedure Add (C : Character) is
501 begin
502 Line (Last + 1) := C;
503 Last := Last + 1;
504 end Add;
506 ---------
507 -- Add --
508 ---------
510 procedure Add (S : String) is
511 Len : constant Natural := S'Length;
512 begin
513 Line (Last + 1 .. Last + Len) := S;
514 Last := Last + Len;
515 end Add;
517 --------------
518 -- Allocate --
519 --------------
521 function Allocate (N : Natural; S : Natural := 1) return Table_Id is
522 L : constant Integer := IT.Last;
523 begin
524 IT.Set_Last (L + N * S);
525 return L + 1;
526 end Allocate;
528 ------------------------------
529 -- Apply_Position_Selection --
530 ------------------------------
532 procedure Apply_Position_Selection is
533 begin
534 WT.Set_Last (2 * NK);
535 for J in 0 .. NK - 1 loop
536 declare
537 I_Word : constant Word_Type := WT.Table (Initial (J));
538 R_Word : Word_Type := Null_Word;
539 Index : Natural := I_Word'First - 1;
541 begin
542 -- Select the characters of Word included in the position
543 -- selection.
545 for C in 0 .. Char_Pos_Set_Len - 1 loop
546 exit when I_Word (Get_Char_Pos (C)) = ASCII.NUL;
547 Index := Index + 1;
548 R_Word (Index) := I_Word (Get_Char_Pos (C));
549 end loop;
551 -- Build the new table with the reduced word
553 WT.Table (Reduced (J)) := R_Word;
554 Set_Key (J, (Edge => No_Edge));
555 end;
556 end loop;
557 end Apply_Position_Selection;
559 -------------------------------
560 -- Assign_Values_To_Vertices --
561 -------------------------------
563 procedure Assign_Values_To_Vertices is
564 X : Vertex_Id;
566 procedure Assign (X : Vertex_Id);
567 -- Execute assignment on X's neighbors except the vertex that we are
568 -- coming from which is already assigned.
570 ------------
571 -- Assign --
572 ------------
574 procedure Assign (X : Vertex_Id) is
575 E : Edge_Type;
576 V : constant Vertex_Type := Get_Vertices (X);
578 begin
579 for J in V.First .. V.Last loop
580 E := Get_Edges (J);
582 if Get_Graph (E.Y) = -1 then
583 Set_Graph (E.Y, (E.Key - Get_Graph (X)) mod NK);
584 Assign (E.Y);
585 end if;
586 end loop;
587 end Assign;
589 -- Start of processing for Assign_Values_To_Vertices
591 begin
592 -- Value -1 denotes an unitialized value as it is supposed to
593 -- be in the range 0 .. NK.
595 if G = No_Table then
596 G_Len := NV;
597 G := Allocate (G_Len, 1);
598 end if;
600 for J in 0 .. G_Len - 1 loop
601 Set_Graph (J, -1);
602 end loop;
604 for K in 0 .. NK - 1 loop
605 X := Get_Edges (Get_Key (K).Edge).X;
607 if Get_Graph (X) = -1 then
608 Set_Graph (X, 0);
609 Assign (X);
610 end if;
611 end loop;
613 for J in 0 .. G_Len - 1 loop
614 if Get_Graph (J) = -1 then
615 Set_Graph (J, 0);
616 end if;
617 end loop;
619 if Verbose then
620 Put_Int_Vector (Output, "Assign Values To Vertices", G, G_Len);
621 end if;
622 end Assign_Values_To_Vertices;
624 -------------
625 -- Compute --
626 -------------
628 procedure Compute (Position : String := Default_Position) is
629 Success : Boolean := False;
631 begin
632 NV := Natural (K2V * Float (NK));
634 Keys := Allocate (NK);
636 if Verbose then
637 Put_Initial_Keys (Output, "Initial Key Table");
638 end if;
640 if Position'Length /= 0 then
641 Parse_Position_Selection (Position);
642 else
643 Select_Char_Position;
644 end if;
646 if Verbose then
647 Put_Int_Vector
648 (Output, "Char Position Set", Char_Pos_Set, Char_Pos_Set_Len);
649 end if;
651 Apply_Position_Selection;
653 if Verbose then
654 Put_Reduced_Keys (Output, "Reduced Keys Table");
655 end if;
657 Select_Character_Set;
659 if Verbose then
660 Put_Used_Char_Set (Output, "Character Position Table");
661 end if;
663 -- Perform Czech's algorithm
665 for J in 1 .. NT loop
666 Generate_Mapping_Tables (Opt, S);
667 Compute_Edges_And_Vertices (Opt);
669 -- When graph is not empty (no self-loop from previous operation) and
670 -- not acyclic.
672 if 0 < Edges_Len and then Acyclic then
673 Success := True;
674 exit;
675 end if;
676 end loop;
678 if not Success then
679 raise Too_Many_Tries;
680 end if;
682 Assign_Values_To_Vertices;
683 end Compute;
685 --------------------------------
686 -- Compute_Edges_And_Vertices --
687 --------------------------------
689 procedure Compute_Edges_And_Vertices (Opt : Optimization) is
690 X : Natural;
691 Y : Natural;
692 Key : Key_Type;
693 Edge : Edge_Type;
694 Vertex : Vertex_Type;
695 Not_Acyclic : Boolean := False;
697 procedure Move (From : Natural; To : Natural);
698 function Lt (L, R : Natural) return Boolean;
699 -- Subprograms needed for GNAT.Heap_Sort_G
701 --------
702 -- Lt --
703 --------
705 function Lt (L, R : Natural) return Boolean is
706 EL : constant Edge_Type := Get_Edges (L);
707 ER : constant Edge_Type := Get_Edges (R);
708 begin
709 return EL.X < ER.X or else (EL.X = ER.X and then EL.Y < ER.Y);
710 end Lt;
712 ----------
713 -- Move --
714 ----------
716 procedure Move (From : Natural; To : Natural) is
717 begin
718 Set_Edges (To, Get_Edges (From));
719 end Move;
721 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
723 -- Start of processing for Compute_Edges_And_Vertices
725 begin
726 -- We store edges from 1 to 2 * NK and leave zero alone in order to use
727 -- GNAT.Heap_Sort_G.
729 Edges_Len := 2 * NK + 1;
731 if Edges = No_Table then
732 Edges := Allocate (Edges_Len, Edge_Size);
733 end if;
735 if Vertices = No_Table then
736 Vertices := Allocate (NV, Vertex_Size);
737 end if;
739 for J in 0 .. NV - 1 loop
740 Set_Vertices (J, (No_Vertex, No_Vertex - 1));
741 end loop;
743 -- For each w, X = f1 (w) and Y = f2 (w)
745 for J in 0 .. NK - 1 loop
746 Key := Get_Key (J);
747 Key.Edge := No_Edge;
748 Set_Key (J, Key);
750 X := Sum (WT.Table (Reduced (J)), T1, Opt);
751 Y := Sum (WT.Table (Reduced (J)), T2, Opt);
753 -- Discard T1 and T2 as soon as we discover a self loop
755 if X = Y then
756 Not_Acyclic := True;
757 exit;
758 end if;
760 -- We store (X, Y) and (Y, X) to ease assignment step
762 Set_Edges (2 * J + 1, (X, Y, J));
763 Set_Edges (2 * J + 2, (Y, X, J));
764 end loop;
766 -- Return an empty graph when self loop detected
768 if Not_Acyclic then
769 Edges_Len := 0;
771 else
772 if Verbose then
773 Put_Edges (Output, "Unsorted Edge Table");
774 Put_Int_Matrix (Output, "Function Table 1", T1,
775 T1_Len, T2_Len);
776 Put_Int_Matrix (Output, "Function Table 2", T2,
777 T1_Len, T2_Len);
778 end if;
780 -- Enforce consistency between edges and keys. Construct Vertices and
781 -- compute the list of neighbors of a vertex First .. Last as Edges
782 -- is sorted by X and then Y. To compute the neighbor list, sort the
783 -- edges.
785 Sorting.Sort (Edges_Len - 1);
787 if Verbose then
788 Put_Edges (Output, "Sorted Edge Table");
789 Put_Int_Matrix (Output, "Function Table 1", T1,
790 T1_Len, T2_Len);
791 Put_Int_Matrix (Output, "Function Table 2", T2,
792 T1_Len, T2_Len);
793 end if;
795 -- Edges valid range is 1 .. 2 * NK
797 for E in 1 .. Edges_Len - 1 loop
798 Edge := Get_Edges (E);
799 Key := Get_Key (Edge.Key);
801 if Key.Edge = No_Edge then
802 Key.Edge := E;
803 Set_Key (Edge.Key, Key);
804 end if;
806 Vertex := Get_Vertices (Edge.X);
808 if Vertex.First = No_Edge then
809 Vertex.First := E;
810 end if;
812 Vertex.Last := E;
813 Set_Vertices (Edge.X, Vertex);
814 end loop;
816 if Verbose then
817 Put_Reduced_Keys (Output, "Key Table");
818 Put_Edges (Output, "Edge Table");
819 Put_Vertex_Table (Output, "Vertex Table");
820 end if;
821 end if;
822 end Compute_Edges_And_Vertices;
824 ------------
825 -- Define --
826 ------------
828 procedure Define
829 (Name : Table_Name;
830 Item_Size : out Natural;
831 Length_1 : out Natural;
832 Length_2 : out Natural)
834 begin
835 case Name is
836 when Character_Position =>
837 Item_Size := 8;
838 Length_1 := Char_Pos_Set_Len;
839 Length_2 := 0;
841 when Used_Character_Set =>
842 Item_Size := 8;
843 Length_1 := 256;
844 Length_2 := 0;
846 when Function_Table_1
847 | Function_Table_2 =>
848 Item_Size := Type_Size (NV);
849 Length_1 := T1_Len;
850 Length_2 := T2_Len;
852 when Graph_Table =>
853 Item_Size := Type_Size (NK);
854 Length_1 := NV;
855 Length_2 := 0;
856 end case;
857 end Define;
859 --------------
860 -- Finalize --
861 --------------
863 procedure Finalize is
864 begin
865 Free_Tmp_Tables;
867 WT.Release;
868 IT.Release;
870 NK := 0;
871 Max_Key_Len := 0;
872 Min_Key_Len := Max_Word_Length;
873 end Finalize;
875 ---------------------
876 -- Free_Tmp_Tables --
877 ---------------------
879 procedure Free_Tmp_Tables is
880 begin
881 IT.Init;
883 Keys := No_Table;
885 Char_Pos_Set := No_Table;
886 Char_Pos_Set_Len := 0;
888 Used_Char_Set := No_Table;
889 Used_Char_Set_Len := 0;
891 T1 := No_Table;
892 T2 := No_Table;
894 T1_Len := 0;
895 T2_Len := 0;
897 G := No_Table;
898 G_Len := 0;
900 Edges := No_Table;
901 Edges_Len := 0;
903 Vertices := No_Table;
904 NV := 0;
905 end Free_Tmp_Tables;
907 ----------------------------
908 -- Generate_Mapping_Table --
909 ----------------------------
911 procedure Generate_Mapping_Table
912 (Tab : Integer;
913 L1 : Natural;
914 L2 : Natural;
915 Seed : in out Natural)
917 begin
918 for J in 0 .. L1 - 1 loop
919 for K in 0 .. L2 - 1 loop
920 Random (Seed);
921 Set_Table (Tab, J, K, Seed mod NV);
922 end loop;
923 end loop;
924 end Generate_Mapping_Table;
926 -----------------------------
927 -- Generate_Mapping_Tables --
928 -----------------------------
930 procedure Generate_Mapping_Tables
931 (Opt : Optimization;
932 Seed : in out Natural)
934 begin
935 -- If T1 and T2 are already allocated no need to do it twice. Reuse them
936 -- as their size has not changed.
938 if T1 = No_Table and then T2 = No_Table then
939 declare
940 Used_Char_Last : Natural := 0;
941 Used_Char : Natural;
943 begin
944 if Opt = CPU_Time then
945 for P in reverse Character'Range loop
946 Used_Char := Get_Used_Char (P);
947 if Used_Char /= 0 then
948 Used_Char_Last := Used_Char;
949 exit;
950 end if;
951 end loop;
952 end if;
954 T1_Len := Char_Pos_Set_Len;
955 T2_Len := Used_Char_Last + 1;
956 T1 := Allocate (T1_Len * T2_Len);
957 T2 := Allocate (T1_Len * T2_Len);
958 end;
959 end if;
961 Generate_Mapping_Table (T1, T1_Len, T2_Len, Seed);
962 Generate_Mapping_Table (T2, T1_Len, T2_Len, Seed);
964 if Verbose then
965 Put_Used_Char_Set (Output, "Used Character Set");
966 Put_Int_Matrix (Output, "Function Table 1", T1,
967 T1_Len, T2_Len);
968 Put_Int_Matrix (Output, "Function Table 2", T2,
969 T1_Len, T2_Len);
970 end if;
971 end Generate_Mapping_Tables;
973 ------------------
974 -- Get_Char_Pos --
975 ------------------
977 function Get_Char_Pos (P : Natural) return Natural is
978 N : constant Natural := Char_Pos_Set + P;
979 begin
980 return IT.Table (N);
981 end Get_Char_Pos;
983 ---------------
984 -- Get_Edges --
985 ---------------
987 function Get_Edges (F : Natural) return Edge_Type is
988 N : constant Natural := Edges + (F * Edge_Size);
989 E : Edge_Type;
990 begin
991 E.X := IT.Table (N);
992 E.Y := IT.Table (N + 1);
993 E.Key := IT.Table (N + 2);
994 return E;
995 end Get_Edges;
997 ---------------
998 -- Get_Graph --
999 ---------------
1001 function Get_Graph (N : Natural) return Integer is
1002 begin
1003 return IT.Table (G + N);
1004 end Get_Graph;
1006 -------------
1007 -- Get_Key --
1008 -------------
1010 function Get_Key (N : Key_Id) return Key_Type is
1011 K : Key_Type;
1012 begin
1013 K.Edge := IT.Table (Keys + N);
1014 return K;
1015 end Get_Key;
1017 ---------------
1018 -- Get_Table --
1019 ---------------
1021 function Get_Table (T : Integer; X, Y : Natural) return Natural is
1022 N : constant Natural := T + (Y * T1_Len) + X;
1023 begin
1024 return IT.Table (N);
1025 end Get_Table;
1027 -------------------
1028 -- Get_Used_Char --
1029 -------------------
1031 function Get_Used_Char (C : Character) return Natural is
1032 N : constant Natural := Used_Char_Set + Character'Pos (C);
1033 begin
1034 return IT.Table (N);
1035 end Get_Used_Char;
1037 ------------------
1038 -- Get_Vertices --
1039 ------------------
1041 function Get_Vertices (F : Natural) return Vertex_Type is
1042 N : constant Natural := Vertices + (F * Vertex_Size);
1043 V : Vertex_Type;
1044 begin
1045 V.First := IT.Table (N);
1046 V.Last := IT.Table (N + 1);
1047 return V;
1048 end Get_Vertices;
1050 -----------
1051 -- Image --
1052 -----------
1054 function Image (Int : Integer; W : Natural := 0) return String is
1055 B : String (1 .. 32);
1056 L : Natural := 0;
1058 procedure Img (V : Natural);
1059 -- Compute image of V into B, starting at B (L), incrementing L
1061 ---------
1062 -- Img --
1063 ---------
1065 procedure Img (V : Natural) is
1066 begin
1067 if V > 9 then
1068 Img (V / 10);
1069 end if;
1071 L := L + 1;
1072 B (L) := Character'Val ((V mod 10) + Character'Pos ('0'));
1073 end Img;
1075 -- Start of processing for Image
1077 begin
1078 if Int < 0 then
1079 L := L + 1;
1080 B (L) := '-';
1081 Img (-Int);
1082 else
1083 Img (Int);
1084 end if;
1086 return Image (B (1 .. L), W);
1087 end Image;
1089 -----------
1090 -- Image --
1091 -----------
1093 function Image (Str : String; W : Natural := 0) return String is
1094 Len : constant Natural := Str'Length;
1095 Max : Natural := Len;
1097 begin
1098 if Max < W then
1099 Max := W;
1100 end if;
1102 declare
1103 Buf : String (1 .. Max) := (1 .. Max => ' ');
1105 begin
1106 for J in 0 .. Len - 1 loop
1107 Buf (Max - Len + 1 + J) := Str (Str'First + J);
1108 end loop;
1110 return Buf;
1111 end;
1112 end Image;
1114 -------------
1115 -- Initial --
1116 -------------
1118 function Initial (K : Key_Id) return Word_Id is
1119 begin
1120 return K;
1121 end Initial;
1123 ----------------
1124 -- Initialize --
1125 ----------------
1127 procedure Initialize
1128 (Seed : Natural;
1129 K_To_V : Float := Default_K_To_V;
1130 Optim : Optimization := CPU_Time;
1131 Tries : Positive := Default_Tries)
1133 begin
1134 -- Free previous tables (the settings may have changed between two runs)
1136 Free_Tmp_Tables;
1138 if K_To_V <= 2.0 then
1139 Put (Output, "K to V ratio cannot be lower than 2.0");
1140 New_Line (Output);
1141 raise Program_Error;
1142 end if;
1144 S := Seed;
1145 K2V := K_To_V;
1146 Opt := Optim;
1147 NT := Tries;
1148 end Initialize;
1150 ------------
1151 -- Insert --
1152 ------------
1154 procedure Insert (Value : String) is
1155 Word : Word_Type := Null_Word;
1156 Len : constant Natural := Value'Length;
1158 begin
1159 Word (1 .. Len) := Value (Value'First .. Value'First + Len - 1);
1160 WT.Set_Last (NK);
1161 WT.Table (NK) := Word;
1162 NK := NK + 1;
1163 NV := Natural (Float (NK) * K2V);
1165 -- Do not accept a value of K2V too close to 2.0 such that once rounded
1166 -- up, NV = 2 * NK because the algorithm would not converge.
1168 if NV <= 2 * NK then
1169 NV := 2 * NK + 1;
1170 end if;
1172 if Max_Key_Len < Len then
1173 Max_Key_Len := Len;
1174 end if;
1176 if Len < Min_Key_Len then
1177 Min_Key_Len := Len;
1178 end if;
1179 end Insert;
1181 --------------
1182 -- New_Line --
1183 --------------
1185 procedure New_Line (File : File_Descriptor) is
1186 begin
1187 if Write (File, EOL'Address, 1) /= 1 then
1188 raise Program_Error;
1189 end if;
1190 end New_Line;
1192 ------------------------------
1193 -- Parse_Position_Selection --
1194 ------------------------------
1196 procedure Parse_Position_Selection (Argument : String) is
1197 N : Natural := Argument'First;
1198 L : constant Natural := Argument'Last;
1199 M : constant Natural := Max_Key_Len;
1201 T : array (1 .. M) of Boolean := (others => False);
1203 function Parse_Index return Natural;
1204 -- Parse argument starting at index N to find an index
1206 -----------------
1207 -- Parse_Index --
1208 -----------------
1210 function Parse_Index return Natural is
1211 C : Character := Argument (N);
1212 V : Natural := 0;
1214 begin
1215 if C = '$' then
1216 N := N + 1;
1217 return M;
1218 end if;
1220 if C not in '0' .. '9' then
1221 Raise_Exception
1222 (Program_Error'Identity, "cannot read position argument");
1223 end if;
1225 while C in '0' .. '9' loop
1226 V := V * 10 + (Character'Pos (C) - Character'Pos ('0'));
1227 N := N + 1;
1228 exit when L < N;
1229 C := Argument (N);
1230 end loop;
1232 return V;
1233 end Parse_Index;
1235 -- Start of processing for Parse_Position_Selection
1237 begin
1238 -- Empty specification means all the positions
1240 if L < N then
1241 Char_Pos_Set_Len := M;
1242 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
1244 for C in 0 .. Char_Pos_Set_Len - 1 loop
1245 Set_Char_Pos (C, C + 1);
1246 end loop;
1248 else
1249 loop
1250 declare
1251 First, Last : Natural;
1253 begin
1254 First := Parse_Index;
1255 Last := First;
1257 -- Detect a range
1259 if N <= L and then Argument (N) = '-' then
1260 N := N + 1;
1261 Last := Parse_Index;
1262 end if;
1264 -- Include the positions in the selection
1266 for J in First .. Last loop
1267 T (J) := True;
1268 end loop;
1269 end;
1271 exit when L < N;
1273 if Argument (N) /= ',' then
1274 Raise_Exception
1275 (Program_Error'Identity, "cannot read position argument");
1276 end if;
1278 N := N + 1;
1279 end loop;
1281 -- Compute position selection length
1283 N := 0;
1284 for J in T'Range loop
1285 if T (J) then
1286 N := N + 1;
1287 end if;
1288 end loop;
1290 -- Fill position selection
1292 Char_Pos_Set_Len := N;
1293 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
1295 N := 0;
1296 for J in T'Range loop
1297 if T (J) then
1298 Set_Char_Pos (N, J);
1299 N := N + 1;
1300 end if;
1301 end loop;
1302 end if;
1303 end Parse_Position_Selection;
1305 -------------
1306 -- Produce --
1307 -------------
1309 procedure Produce (Pkg_Name : String := Default_Pkg_Name) is
1310 File : File_Descriptor;
1312 Status : Boolean;
1313 -- For call to Close
1315 function Array_Img (N, T, R1 : String; R2 : String := "") return String;
1316 -- Return string "N : constant array (R1[, R2]) of T;"
1318 function Range_Img (F, L : Natural; T : String := "") return String;
1319 -- Return string "[T range ]F .. L"
1321 function Type_Img (L : Natural) return String;
1322 -- Return the larger unsigned type T such that T'Last < L
1324 ---------------
1325 -- Array_Img --
1326 ---------------
1328 function Array_Img
1329 (N, T, R1 : String;
1330 R2 : String := "") return String
1332 begin
1333 Last := 0;
1334 Add (" ");
1335 Add (N);
1336 Add (" : constant array (");
1337 Add (R1);
1339 if R2 /= "" then
1340 Add (", ");
1341 Add (R2);
1342 end if;
1344 Add (") of ");
1345 Add (T);
1346 Add (" :=");
1347 return Line (1 .. Last);
1348 end Array_Img;
1350 ---------------
1351 -- Range_Img --
1352 ---------------
1354 function Range_Img (F, L : Natural; T : String := "") return String is
1355 FI : constant String := Image (F);
1356 FL : constant Natural := FI'Length;
1357 LI : constant String := Image (L);
1358 LL : constant Natural := LI'Length;
1359 TL : constant Natural := T'Length;
1360 RI : String (1 .. TL + 7 + FL + 4 + LL);
1361 Len : Natural := 0;
1363 begin
1364 if TL /= 0 then
1365 RI (Len + 1 .. Len + TL) := T;
1366 Len := Len + TL;
1367 RI (Len + 1 .. Len + 7) := " range ";
1368 Len := Len + 7;
1369 end if;
1371 RI (Len + 1 .. Len + FL) := FI;
1372 Len := Len + FL;
1373 RI (Len + 1 .. Len + 4) := " .. ";
1374 Len := Len + 4;
1375 RI (Len + 1 .. Len + LL) := LI;
1376 Len := Len + LL;
1377 return RI (1 .. Len);
1378 end Range_Img;
1380 --------------
1381 -- Type_Img --
1382 --------------
1384 function Type_Img (L : Natural) return String is
1385 S : constant String := Image (Type_Size (L));
1386 U : String := "Unsigned_ ";
1387 N : Natural := 9;
1389 begin
1390 for J in S'Range loop
1391 N := N + 1;
1392 U (N) := S (J);
1393 end loop;
1395 return U (1 .. N);
1396 end Type_Img;
1398 F : Natural;
1399 L : Natural;
1400 P : Natural;
1402 PLen : constant Natural := Pkg_Name'Length;
1403 FName : String (1 .. PLen + 4);
1405 -- Start of processing for Produce
1407 begin
1408 FName (1 .. PLen) := Pkg_Name;
1409 for J in 1 .. PLen loop
1410 if FName (J) in 'A' .. 'Z' then
1411 FName (J) := Character'Val (Character'Pos (FName (J))
1412 - Character'Pos ('A')
1413 + Character'Pos ('a'));
1415 elsif FName (J) = '.' then
1416 FName (J) := '-';
1417 end if;
1418 end loop;
1420 FName (PLen + 1 .. PLen + 4) := ".ads";
1422 File := Create_File (FName, Binary);
1424 Put (File, "package ");
1425 Put (File, Pkg_Name);
1426 Put (File, " is");
1427 New_Line (File);
1428 Put (File, " function Hash (S : String) return Natural;");
1429 New_Line (File);
1430 Put (File, "end ");
1431 Put (File, Pkg_Name);
1432 Put (File, ";");
1433 New_Line (File);
1434 Close (File, Status);
1436 if not Status then
1437 raise Device_Error;
1438 end if;
1440 FName (PLen + 4) := 'b';
1442 File := Create_File (FName, Binary);
1444 Put (File, "with Interfaces; use Interfaces;");
1445 New_Line (File);
1446 New_Line (File);
1447 Put (File, "package body ");
1448 Put (File, Pkg_Name);
1449 Put (File, " is");
1450 New_Line (File);
1451 New_Line (File);
1453 if Opt = CPU_Time then
1454 Put (File, Array_Img ("C", Type_Img (256), "Character"));
1455 New_Line (File);
1457 F := Character'Pos (Character'First);
1458 L := Character'Pos (Character'Last);
1460 for J in Character'Range loop
1461 P := Get_Used_Char (J);
1462 Put (File, Image (P), 1, 0, 1, F, L, Character'Pos (J));
1463 end loop;
1465 New_Line (File);
1466 end if;
1468 F := 0;
1469 L := Char_Pos_Set_Len - 1;
1471 Put (File, Array_Img ("P", "Natural", Range_Img (F, L)));
1472 New_Line (File);
1474 for J in F .. L loop
1475 Put (File, Image (Get_Char_Pos (J)), 1, 0, 1, F, L, J);
1476 end loop;
1478 New_Line (File);
1480 if Opt = CPU_Time then
1481 Put_Int_Matrix
1482 (File,
1483 Array_Img ("T1", Type_Img (NV),
1484 Range_Img (0, T1_Len - 1),
1485 Range_Img (0, T2_Len - 1, Type_Img (256))),
1486 T1, T1_Len, T2_Len);
1488 else
1489 Put_Int_Matrix
1490 (File,
1491 Array_Img ("T1", Type_Img (NV),
1492 Range_Img (0, T1_Len - 1)),
1493 T1, T1_Len, 0);
1494 end if;
1496 New_Line (File);
1498 if Opt = CPU_Time then
1499 Put_Int_Matrix
1500 (File,
1501 Array_Img ("T2", Type_Img (NV),
1502 Range_Img (0, T1_Len - 1),
1503 Range_Img (0, T2_Len - 1, Type_Img (256))),
1504 T2, T1_Len, T2_Len);
1506 else
1507 Put_Int_Matrix
1508 (File,
1509 Array_Img ("T2", Type_Img (NV),
1510 Range_Img (0, T1_Len - 1)),
1511 T2, T1_Len, 0);
1512 end if;
1514 New_Line (File);
1516 Put_Int_Vector
1517 (File,
1518 Array_Img ("G", Type_Img (NK),
1519 Range_Img (0, G_Len - 1)),
1520 G, G_Len);
1521 New_Line (File);
1523 Put (File, " function Hash (S : String) return Natural is");
1524 New_Line (File);
1525 Put (File, " F : constant Natural := S'First - 1;");
1526 New_Line (File);
1527 Put (File, " L : constant Natural := S'Length;");
1528 New_Line (File);
1529 Put (File, " F1, F2 : Natural := 0;");
1530 New_Line (File);
1532 Put (File, " J : ");
1534 if Opt = CPU_Time then
1535 Put (File, Type_Img (256));
1536 else
1537 Put (File, "Natural");
1538 end if;
1540 Put (File, ";");
1541 New_Line (File);
1543 Put (File, " begin");
1544 New_Line (File);
1545 Put (File, " for K in P'Range loop");
1546 New_Line (File);
1547 Put (File, " exit when L < P (K);");
1548 New_Line (File);
1549 Put (File, " J := ");
1551 if Opt = CPU_Time then
1552 Put (File, "C");
1553 else
1554 Put (File, "Character'Pos");
1555 end if;
1557 Put (File, " (S (P (K) + F));");
1558 New_Line (File);
1560 Put (File, " F1 := (F1 + Natural (T1 (K");
1562 if Opt = CPU_Time then
1563 Put (File, ", J");
1564 end if;
1566 Put (File, "))");
1568 if Opt = Memory_Space then
1569 Put (File, " * J");
1570 end if;
1572 Put (File, ") mod ");
1573 Put (File, Image (NV));
1574 Put (File, ";");
1575 New_Line (File);
1577 Put (File, " F2 := (F2 + Natural (T2 (K");
1579 if Opt = CPU_Time then
1580 Put (File, ", J");
1581 end if;
1583 Put (File, "))");
1585 if Opt = Memory_Space then
1586 Put (File, " * J");
1587 end if;
1589 Put (File, ") mod ");
1590 Put (File, Image (NV));
1591 Put (File, ";");
1592 New_Line (File);
1594 Put (File, " end loop;");
1595 New_Line (File);
1597 Put (File,
1598 " return (Natural (G (F1)) + Natural (G (F2))) mod ");
1600 Put (File, Image (NK));
1601 Put (File, ";");
1602 New_Line (File);
1603 Put (File, " end Hash;");
1604 New_Line (File);
1605 New_Line (File);
1606 Put (File, "end ");
1607 Put (File, Pkg_Name);
1608 Put (File, ";");
1609 New_Line (File);
1610 Close (File, Status);
1612 if not Status then
1613 raise Device_Error;
1614 end if;
1615 end Produce;
1617 ---------
1618 -- Put --
1619 ---------
1621 procedure Put (File : File_Descriptor; Str : String) is
1622 Len : constant Natural := Str'Length;
1623 begin
1624 if Write (File, Str'Address, Len) /= Len then
1625 raise Program_Error;
1626 end if;
1627 end Put;
1629 ---------
1630 -- Put --
1631 ---------
1633 procedure Put
1634 (F : File_Descriptor;
1635 S : String;
1636 F1 : Natural;
1637 L1 : Natural;
1638 C1 : Natural;
1639 F2 : Natural;
1640 L2 : Natural;
1641 C2 : Natural)
1643 Len : constant Natural := S'Length;
1645 procedure Flush;
1646 -- Write current line, followed by LF
1648 -----------
1649 -- Flush --
1650 -----------
1652 procedure Flush is
1653 begin
1654 Put (F, Line (1 .. Last));
1655 New_Line (F);
1656 Last := 0;
1657 end Flush;
1659 -- Start of processing for Put
1661 begin
1662 if C1 = F1 and then C2 = F2 then
1663 Last := 0;
1664 end if;
1666 if Last + Len + 3 > Max then
1667 Flush;
1668 end if;
1670 if Last = 0 then
1671 Line (Last + 1 .. Last + 5) := " ";
1672 Last := Last + 5;
1674 if F1 <= L1 then
1675 if C1 = F1 and then C2 = F2 then
1676 Add ('(');
1678 if F1 = L1 then
1679 Add ("0 .. 0 => ");
1680 end if;
1682 else
1683 Add (' ');
1684 end if;
1685 end if;
1686 end if;
1688 if C2 = F2 then
1689 Add ('(');
1691 if F2 = L2 then
1692 Add ("0 .. 0 => ");
1693 end if;
1695 else
1696 Add (' ');
1697 end if;
1699 Line (Last + 1 .. Last + Len) := S;
1700 Last := Last + Len;
1702 if C2 = L2 then
1703 Add (')');
1705 if F1 > L1 then
1706 Add (';');
1707 Flush;
1709 elsif C1 /= L1 then
1710 Add (',');
1711 Flush;
1713 else
1714 Add (')');
1715 Add (';');
1716 Flush;
1717 end if;
1719 else
1720 Add (',');
1721 end if;
1722 end Put;
1724 ---------------
1725 -- Put_Edges --
1726 ---------------
1728 procedure Put_Edges (File : File_Descriptor; Title : String) is
1729 E : Edge_Type;
1730 F1 : constant Natural := 1;
1731 L1 : constant Natural := Edges_Len - 1;
1732 M : constant Natural := Max / 5;
1734 begin
1735 Put (File, Title);
1736 New_Line (File);
1738 -- Edges valid range is 1 .. Edge_Len - 1
1740 for J in F1 .. L1 loop
1741 E := Get_Edges (J);
1742 Put (File, Image (J, M), F1, L1, J, 1, 4, 1);
1743 Put (File, Image (E.X, M), F1, L1, J, 1, 4, 2);
1744 Put (File, Image (E.Y, M), F1, L1, J, 1, 4, 3);
1745 Put (File, Image (E.Key, M), F1, L1, J, 1, 4, 4);
1746 end loop;
1747 end Put_Edges;
1749 ----------------------
1750 -- Put_Initial_Keys --
1751 ----------------------
1753 procedure Put_Initial_Keys (File : File_Descriptor; Title : String) is
1754 F1 : constant Natural := 0;
1755 L1 : constant Natural := NK - 1;
1756 M : constant Natural := Max / 5;
1757 K : Key_Type;
1759 begin
1760 Put (File, Title);
1761 New_Line (File);
1763 for J in F1 .. L1 loop
1764 K := Get_Key (J);
1765 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
1766 Put (File, Image (K.Edge, M), F1, L1, J, 1, 3, 2);
1767 Put (File, WT.Table (Initial (J)), F1, L1, J, 1, 3, 3);
1768 end loop;
1769 end Put_Initial_Keys;
1771 --------------------
1772 -- Put_Int_Matrix --
1773 --------------------
1775 procedure Put_Int_Matrix
1776 (File : File_Descriptor;
1777 Title : String;
1778 Table : Integer;
1779 Len_1 : Natural;
1780 Len_2 : Natural)
1782 F1 : constant Integer := 0;
1783 L1 : constant Integer := Len_1 - 1;
1784 F2 : constant Integer := 0;
1785 L2 : constant Integer := Len_2 - 1;
1786 Ix : Natural;
1788 begin
1789 Put (File, Title);
1790 New_Line (File);
1792 if Len_2 = 0 then
1793 for J in F1 .. L1 loop
1794 Ix := IT.Table (Table + J);
1795 Put (File, Image (Ix), 1, 0, 1, F1, L1, J);
1796 end loop;
1798 else
1799 for J in F1 .. L1 loop
1800 for K in F2 .. L2 loop
1801 Ix := IT.Table (Table + J + K * Len_1);
1802 Put (File, Image (Ix), F1, L1, J, F2, L2, K);
1803 end loop;
1804 end loop;
1805 end if;
1806 end Put_Int_Matrix;
1808 --------------------
1809 -- Put_Int_Vector --
1810 --------------------
1812 procedure Put_Int_Vector
1813 (File : File_Descriptor;
1814 Title : String;
1815 Vector : Integer;
1816 Length : Natural)
1818 F2 : constant Natural := 0;
1819 L2 : constant Natural := Length - 1;
1821 begin
1822 Put (File, Title);
1823 New_Line (File);
1825 for J in F2 .. L2 loop
1826 Put (File, Image (IT.Table (Vector + J)), 1, 0, 1, F2, L2, J);
1827 end loop;
1828 end Put_Int_Vector;
1830 ----------------------
1831 -- Put_Reduced_Keys --
1832 ----------------------
1834 procedure Put_Reduced_Keys (File : File_Descriptor; Title : String) is
1835 F1 : constant Natural := 0;
1836 L1 : constant Natural := NK - 1;
1837 M : constant Natural := Max / 5;
1838 K : Key_Type;
1840 begin
1841 Put (File, Title);
1842 New_Line (File);
1844 for J in F1 .. L1 loop
1845 K := Get_Key (J);
1846 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
1847 Put (File, Image (K.Edge, M), F1, L1, J, 1, 3, 2);
1848 Put (File, WT.Table (Reduced (J)), F1, L1, J, 1, 3, 3);
1849 end loop;
1850 end Put_Reduced_Keys;
1852 -----------------------
1853 -- Put_Used_Char_Set --
1854 -----------------------
1856 procedure Put_Used_Char_Set (File : File_Descriptor; Title : String) is
1857 F : constant Natural := Character'Pos (Character'First);
1858 L : constant Natural := Character'Pos (Character'Last);
1860 begin
1861 Put (File, Title);
1862 New_Line (File);
1864 for J in Character'Range loop
1866 (File, Image (Get_Used_Char (J)), 1, 0, 1, F, L, Character'Pos (J));
1867 end loop;
1868 end Put_Used_Char_Set;
1870 ----------------------
1871 -- Put_Vertex_Table --
1872 ----------------------
1874 procedure Put_Vertex_Table (File : File_Descriptor; Title : String) is
1875 F1 : constant Natural := 0;
1876 L1 : constant Natural := NV - 1;
1877 M : constant Natural := Max / 4;
1878 V : Vertex_Type;
1880 begin
1881 Put (File, Title);
1882 New_Line (File);
1884 for J in F1 .. L1 loop
1885 V := Get_Vertices (J);
1886 Put (File, Image (J, M), F1, L1, J, 1, 3, 1);
1887 Put (File, Image (V.First, M), F1, L1, J, 1, 3, 2);
1888 Put (File, Image (V.Last, M), F1, L1, J, 1, 3, 3);
1889 end loop;
1890 end Put_Vertex_Table;
1892 ------------
1893 -- Random --
1894 ------------
1896 procedure Random (Seed : in out Natural) is
1898 -- Park & Miller Standard Minimal using Schrage's algorithm to avoid
1899 -- overflow: Xn+1 = 16807 * Xn mod (2 ** 31 - 1)
1901 R : Natural;
1902 Q : Natural;
1903 X : Integer;
1905 begin
1906 R := Seed mod 127773;
1907 Q := Seed / 127773;
1908 X := 16807 * R - 2836 * Q;
1910 if X < 0 then
1911 Seed := X + 2147483647;
1912 else
1913 Seed := X;
1914 end if;
1915 end Random;
1917 -------------
1918 -- Reduced --
1919 -------------
1921 function Reduced (K : Key_Id) return Word_Id is
1922 begin
1923 return K + NK + 1;
1924 end Reduced;
1926 --------------------------
1927 -- Select_Char_Position --
1928 --------------------------
1930 procedure Select_Char_Position is
1932 type Vertex_Table_Type is array (Natural range <>) of Vertex_Type;
1934 procedure Build_Identical_Keys_Sets
1935 (Table : in out Vertex_Table_Type;
1936 Last : in out Natural;
1937 Pos : Natural);
1938 -- Build a list of keys subsets that are identical with the current
1939 -- position selection plus Pos. Once this routine is called, reduced
1940 -- words are sorted by subsets and each item (First, Last) in Sets
1941 -- defines the range of identical keys.
1942 -- Need comment saying exactly what Last is ???
1944 function Count_Different_Keys
1945 (Table : Vertex_Table_Type;
1946 Last : Natural;
1947 Pos : Natural) return Natural;
1948 -- For each subset in Sets, count the number of different keys if we add
1949 -- Pos to the current position selection.
1951 Sel_Position : IT.Table_Type (1 .. Max_Key_Len);
1952 Last_Sel_Pos : Natural := 0;
1953 Max_Sel_Pos : Natural := 0;
1955 -------------------------------
1956 -- Build_Identical_Keys_Sets --
1957 -------------------------------
1959 procedure Build_Identical_Keys_Sets
1960 (Table : in out Vertex_Table_Type;
1961 Last : in out Natural;
1962 Pos : Natural)
1964 S : constant Vertex_Table_Type := Table (Table'First .. Last);
1965 C : constant Natural := Pos;
1966 -- Shortcuts (why are these not renames ???)
1968 F : Integer;
1969 L : Integer;
1970 -- First and last words of a subset
1972 Offset : Natural;
1973 -- GNAT.Heap_Sort assumes that the first array index is 1. Offset
1974 -- defines the translation to operate.
1976 function Lt (L, R : Natural) return Boolean;
1977 procedure Move (From : Natural; To : Natural);
1978 -- Subprograms needed by GNAT.Heap_Sort_G
1980 --------
1981 -- Lt --
1982 --------
1984 function Lt (L, R : Natural) return Boolean is
1985 C : constant Natural := Pos;
1986 Left : Natural;
1987 Right : Natural;
1989 begin
1990 if L = 0 then
1991 Left := Reduced (0) - 1;
1992 Right := Offset + R;
1993 elsif R = 0 then
1994 Left := Offset + L;
1995 Right := Reduced (0) - 1;
1996 else
1997 Left := Offset + L;
1998 Right := Offset + R;
1999 end if;
2001 return WT.Table (Left)(C) < WT.Table (Right)(C);
2002 end Lt;
2004 ----------
2005 -- Move --
2006 ----------
2008 procedure Move (From : Natural; To : Natural) is
2009 Target, Source : Natural;
2011 begin
2012 if From = 0 then
2013 Source := Reduced (0) - 1;
2014 Target := Offset + To;
2015 elsif To = 0 then
2016 Source := Offset + From;
2017 Target := Reduced (0) - 1;
2018 else
2019 Source := Offset + From;
2020 Target := Offset + To;
2021 end if;
2023 WT.Table (Target) := WT.Table (Source);
2024 end Move;
2026 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
2028 -- Start of processing for Build_Identical_Key_Sets
2030 begin
2031 Last := 0;
2033 -- For each subset in S, extract the new subsets we have by adding C
2034 -- in the position selection.
2036 for J in S'Range loop
2037 if S (J).First = S (J).Last then
2038 F := S (J).First;
2039 L := S (J).Last;
2040 Last := Last + 1;
2041 Table (Last) := (F, L);
2043 else
2044 Offset := Reduced (S (J).First) - 1;
2045 Sorting.Sort (S (J).Last - S (J).First + 1);
2047 F := S (J).First;
2048 L := F;
2049 for N in S (J).First .. S (J).Last loop
2051 -- For the last item, close the last subset
2053 if N = S (J).Last then
2054 Last := Last + 1;
2055 Table (Last) := (F, N);
2057 -- Two contiguous words are identical when they have the
2058 -- same Cth character.
2060 elsif WT.Table (Reduced (N))(C) =
2061 WT.Table (Reduced (N + 1))(C)
2062 then
2063 L := N + 1;
2065 -- Find a new subset of identical keys. Store the current
2066 -- one and create a new subset.
2068 else
2069 Last := Last + 1;
2070 Table (Last) := (F, L);
2071 F := N + 1;
2072 L := F;
2073 end if;
2074 end loop;
2075 end if;
2076 end loop;
2077 end Build_Identical_Keys_Sets;
2079 --------------------------
2080 -- Count_Different_Keys --
2081 --------------------------
2083 function Count_Different_Keys
2084 (Table : Vertex_Table_Type;
2085 Last : Natural;
2086 Pos : Natural) return Natural
2088 N : array (Character) of Natural;
2089 C : Character;
2090 T : Natural := 0;
2092 begin
2093 -- For each subset, count the number of words that are still
2094 -- different when we include Pos in the position selection. Only
2095 -- focus on this position as the other positions already produce
2096 -- identical keys.
2098 for S in 1 .. Last loop
2100 -- Count the occurrences of the different characters
2102 N := (others => 0);
2103 for K in Table (S).First .. Table (S).Last loop
2104 C := WT.Table (Reduced (K))(Pos);
2105 N (C) := N (C) + 1;
2106 end loop;
2108 -- Update the number of different keys. Each character used
2109 -- denotes a different key.
2111 for J in N'Range loop
2112 if N (J) > 0 then
2113 T := T + 1;
2114 end if;
2115 end loop;
2116 end loop;
2118 return T;
2119 end Count_Different_Keys;
2121 -- Start of processing for Select_Char_Position
2123 begin
2124 -- Initialize the reduced words set
2126 WT.Set_Last (2 * NK);
2127 for K in 0 .. NK - 1 loop
2128 WT.Table (Reduced (K)) := WT.Table (Initial (K));
2129 end loop;
2131 declare
2132 Differences : Natural;
2133 Max_Differences : Natural := 0;
2134 Old_Differences : Natural;
2135 Max_Diff_Sel_Pos : Natural := 0; -- init to kill warning
2136 Max_Diff_Sel_Pos_Idx : Natural := 0; -- init to kill warning
2137 Same_Keys_Sets_Table : Vertex_Table_Type (1 .. NK);
2138 Same_Keys_Sets_Last : Natural := 1;
2140 begin
2141 for C in Sel_Position'Range loop
2142 Sel_Position (C) := C;
2143 end loop;
2145 Same_Keys_Sets_Table (1) := (0, NK - 1);
2147 loop
2148 -- Preserve maximum number of different keys and check later on
2149 -- that this value is strictly incrementing. Otherwise, it means
2150 -- that two keys are stricly identical.
2152 Old_Differences := Max_Differences;
2154 -- The first position should not exceed the minimum key length.
2155 -- Otherwise, we may end up with an empty word once reduced.
2157 if Last_Sel_Pos = 0 then
2158 Max_Sel_Pos := Min_Key_Len;
2159 else
2160 Max_Sel_Pos := Max_Key_Len;
2161 end if;
2163 -- Find which position increases more the number of differences
2165 for J in Last_Sel_Pos + 1 .. Max_Sel_Pos loop
2166 Differences := Count_Different_Keys
2167 (Same_Keys_Sets_Table,
2168 Same_Keys_Sets_Last,
2169 Sel_Position (J));
2171 if Verbose then
2172 Put (Output,
2173 "Selecting position" & Sel_Position (J)'Img &
2174 " results in" & Differences'Img &
2175 " differences");
2176 New_Line (Output);
2177 end if;
2179 if Differences > Max_Differences then
2180 Max_Differences := Differences;
2181 Max_Diff_Sel_Pos := Sel_Position (J);
2182 Max_Diff_Sel_Pos_Idx := J;
2183 end if;
2184 end loop;
2186 if Old_Differences = Max_Differences then
2187 Raise_Exception
2188 (Program_Error'Identity, "some keys are identical");
2189 end if;
2191 -- Insert selected position and sort Sel_Position table
2193 Last_Sel_Pos := Last_Sel_Pos + 1;
2194 Sel_Position (Last_Sel_Pos + 1 .. Max_Diff_Sel_Pos_Idx) :=
2195 Sel_Position (Last_Sel_Pos .. Max_Diff_Sel_Pos_Idx - 1);
2196 Sel_Position (Last_Sel_Pos) := Max_Diff_Sel_Pos;
2198 for P in 1 .. Last_Sel_Pos - 1 loop
2199 if Max_Diff_Sel_Pos < Sel_Position (P) then
2200 Sel_Position (P + 1 .. Last_Sel_Pos) :=
2201 Sel_Position (P .. Last_Sel_Pos - 1);
2202 Sel_Position (P) := Max_Diff_Sel_Pos;
2203 exit;
2204 end if;
2205 end loop;
2207 exit when Max_Differences = NK;
2209 Build_Identical_Keys_Sets
2210 (Same_Keys_Sets_Table,
2211 Same_Keys_Sets_Last,
2212 Max_Diff_Sel_Pos);
2214 if Verbose then
2215 Put (Output,
2216 "Selecting position" & Max_Diff_Sel_Pos'Img &
2217 " results in" & Max_Differences'Img &
2218 " differences");
2219 New_Line (Output);
2220 Put (Output, "--");
2221 New_Line (Output);
2222 for J in 1 .. Same_Keys_Sets_Last loop
2223 for K in
2224 Same_Keys_Sets_Table (J).First ..
2225 Same_Keys_Sets_Table (J).Last
2226 loop
2227 Put (Output, WT.Table (Reduced (K)));
2228 New_Line (Output);
2229 end loop;
2230 Put (Output, "--");
2231 New_Line (Output);
2232 end loop;
2233 end if;
2234 end loop;
2235 end;
2237 Char_Pos_Set_Len := Last_Sel_Pos;
2238 Char_Pos_Set := Allocate (Char_Pos_Set_Len);
2240 for C in 1 .. Last_Sel_Pos loop
2241 Set_Char_Pos (C - 1, Sel_Position (C));
2242 end loop;
2243 end Select_Char_Position;
2245 --------------------------
2246 -- Select_Character_Set --
2247 --------------------------
2249 procedure Select_Character_Set is
2250 Last : Natural := 0;
2251 Used : array (Character) of Boolean := (others => False);
2252 Char : Character;
2254 begin
2255 for J in 0 .. NK - 1 loop
2256 for K in 0 .. Char_Pos_Set_Len - 1 loop
2257 Char := WT.Table (Initial (J))(Get_Char_Pos (K));
2258 exit when Char = ASCII.NUL;
2259 Used (Char) := True;
2260 end loop;
2261 end loop;
2263 Used_Char_Set_Len := 256;
2264 Used_Char_Set := Allocate (Used_Char_Set_Len);
2266 for J in Used'Range loop
2267 if Used (J) then
2268 Set_Used_Char (J, Last);
2269 Last := Last + 1;
2270 else
2271 Set_Used_Char (J, 0);
2272 end if;
2273 end loop;
2274 end Select_Character_Set;
2276 ------------------
2277 -- Set_Char_Pos --
2278 ------------------
2280 procedure Set_Char_Pos (P : Natural; Item : Natural) is
2281 N : constant Natural := Char_Pos_Set + P;
2282 begin
2283 IT.Table (N) := Item;
2284 end Set_Char_Pos;
2286 ---------------
2287 -- Set_Edges --
2288 ---------------
2290 procedure Set_Edges (F : Natural; Item : Edge_Type) is
2291 N : constant Natural := Edges + (F * Edge_Size);
2292 begin
2293 IT.Table (N) := Item.X;
2294 IT.Table (N + 1) := Item.Y;
2295 IT.Table (N + 2) := Item.Key;
2296 end Set_Edges;
2298 ---------------
2299 -- Set_Graph --
2300 ---------------
2302 procedure Set_Graph (N : Natural; Item : Integer) is
2303 begin
2304 IT.Table (G + N) := Item;
2305 end Set_Graph;
2307 -------------
2308 -- Set_Key --
2309 -------------
2311 procedure Set_Key (N : Key_Id; Item : Key_Type) is
2312 begin
2313 IT.Table (Keys + N) := Item.Edge;
2314 end Set_Key;
2316 ---------------
2317 -- Set_Table --
2318 ---------------
2320 procedure Set_Table (T : Integer; X, Y : Natural; Item : Natural) is
2321 N : constant Natural := T + ((Y * T1_Len) + X);
2322 begin
2323 IT.Table (N) := Item;
2324 end Set_Table;
2326 -------------------
2327 -- Set_Used_Char --
2328 -------------------
2330 procedure Set_Used_Char (C : Character; Item : Natural) is
2331 N : constant Natural := Used_Char_Set + Character'Pos (C);
2332 begin
2333 IT.Table (N) := Item;
2334 end Set_Used_Char;
2336 ------------------
2337 -- Set_Vertices --
2338 ------------------
2340 procedure Set_Vertices (F : Natural; Item : Vertex_Type) is
2341 N : constant Natural := Vertices + (F * Vertex_Size);
2342 begin
2343 IT.Table (N) := Item.First;
2344 IT.Table (N + 1) := Item.Last;
2345 end Set_Vertices;
2347 ---------
2348 -- Sum --
2349 ---------
2351 function Sum
2352 (Word : Word_Type;
2353 Table : Table_Id;
2354 Opt : Optimization) return Natural
2356 S : Natural := 0;
2357 R : Natural;
2359 begin
2360 if Opt = CPU_Time then
2361 for J in 0 .. T1_Len - 1 loop
2362 exit when Word (J + 1) = ASCII.NUL;
2363 R := Get_Table (Table, J, Get_Used_Char (Word (J + 1)));
2364 S := (S + R) mod NV;
2365 end loop;
2367 else
2368 for J in 0 .. T1_Len - 1 loop
2369 exit when Word (J + 1) = ASCII.NUL;
2370 R := Get_Table (Table, J, 0);
2371 S := (S + R * Character'Pos (Word (J + 1))) mod NV;
2372 end loop;
2373 end if;
2375 return S;
2376 end Sum;
2378 ---------------
2379 -- Type_Size --
2380 ---------------
2382 function Type_Size (L : Natural) return Natural is
2383 begin
2384 if L <= 2 ** 8 then
2385 return 8;
2386 elsif L <= 2 ** 16 then
2387 return 16;
2388 else
2389 return 32;
2390 end if;
2391 end Type_Size;
2393 -----------
2394 -- Value --
2395 -----------
2397 function Value
2398 (Name : Table_Name;
2399 J : Natural;
2400 K : Natural := 0) return Natural
2402 begin
2403 case Name is
2404 when Character_Position =>
2405 return Get_Char_Pos (J);
2407 when Used_Character_Set =>
2408 return Get_Used_Char (Character'Val (J));
2410 when Function_Table_1 =>
2411 return Get_Table (T1, J, K);
2413 when Function_Table_2 =>
2414 return Get_Table (T2, J, K);
2416 when Graph_Table =>
2417 return Get_Graph (J);
2419 end case;
2420 end Value;
2422 end GNAT.Perfect_Hash_Generators;