Natools

Check-in [ba447ff2ea]
Login
Overview
Comment:static_hash_maps: allow elements to be indefinite by looking them up in a function rather than an array
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ba447ff2eaa629ba9511cba76bfb0fd87c800d8f
User & Date: nat on 2014-11-08 21:24:36
Other Links: manifest | tags
Context
2014-11-09
20:48
static_hash_maps-s_expressions: add commands for definite or indefinite elements check-in: 1cc98d90f5 user: nat tags: trunk
2014-11-08
21:24
static_hash_maps: allow elements to be indefinite by looking them up in a function rather than an array check-in: ba447ff2ea user: nat tags: trunk
2014-10-07
16:41
constant_indefinite_ordered_map_tests: new test suite for constant indefinite ordered maps check-in: 72852bae7a user: nat tags: trunk
Changes

Modified src/natools-static_hash_maps.adb from [d09dd75265] to [88f07d9369].

196
197
198
199
200
201
202


































203
204
205
206
207
208
209


   procedure Write_Map_Body
     (Map : in Map_Description;
      Prefix : in String;
      File : in Ada.Text_IO.File_Type) is
   begin


































      Ada.Text_IO.Put_Line
        (File,
         "   function "
         & To_String (Map.Function_Name)
         & " (Key : String) return "
         & To_String (Map.Element_Type)
         & " is");







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243


   procedure Write_Map_Body
     (Map : in Map_Description;
      Prefix : in String;
      File : in Ada.Text_IO.File_Type) is
   begin
      if Map.Indefinite then
         Ada.Text_IO.Put_Line
           (File,
            "   function " & Prefix & "_Elements (Hash : "
            & Prefix & "_Hash)");
         Ada.Text_IO.Put_Line
           (File,
            "     return " & To_String (Map.Element_Type) & " is");
         Ada.Text_IO.Put_Line (File, "   begin");
         Ada.Text_IO.Put_Line (File, "      case Hash is");

         declare
            Pos : Natural := 0;
            Cursor : Node_Lists.Cursor := Map.Nodes.First;
         begin
            while Node_Lists.Has_Element (Cursor) loop
               Ada.Text_IO.Put_Line
                 (File, "         when " & Image (Pos) & " =>");
               Ada.Text_IO.Put_Line
                 (File,
                  "            return "
                  & To_String (Node_Lists.Element (Cursor).Name)
                  & ';');

               Node_Lists.Next (Cursor);
               Pos := Pos + 1;
            end loop;
         end;

         Ada.Text_IO.Put_Line (File, "      end case;");
         Ada.Text_IO.Put_Line (File, "   end " & Prefix & "_Elements;");
         Ada.Text_IO.New_Line (File);
      end if;

      Ada.Text_IO.Put_Line
        (File,
         "   function "
         & To_String (Map.Function_Name)
         & " (Key : String) return "
         & To_String (Map.Element_Type)
         & " is");
321
322
323
324
325
326
327

328
329












330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352

353
354
355
356
357
358
359
            Ada.Text_IO.Put_Line (File, ",");
         end if;

         Pos := Pos + 1;
         Node_Lists.Next (Cursor);
      end loop;


      Ada.Text_IO.Put_Line
        (File,












         "   " & Prefix & "_Elements : constant array (0 .. " & Image (Last)
         & ") of " & To_String (Map.Element_Type));
      Pos := 0;
      Cursor := Map.Nodes.First;
      while Node_Lists.Has_Element (Cursor) loop
         if Pos = 0 then
            Ada.Text_IO.Put (File, "     := (");
         else
            Ada.Text_IO.Put (File, "         ");
         end if;

         Ada.Text_IO.Put
           (File, To_String (Node_Lists.Element (Cursor).Name));

         if Pos = Last then
            Ada.Text_IO.Put_Line (File, ");");
         else
            Ada.Text_IO.Put_Line (File, ",");
         end if;

         Pos := Pos + 1;
         Node_Lists.Next (Cursor);
      end loop;

   end Write_Map_Private_Spec;


   procedure Write_Map_Public_Spec
     (Map : in Map_Description;
      File : in Ada.Text_IO.File_Type) is
   begin







>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|

|
|

|
|
|
|
|

|
|
|
>







355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
            Ada.Text_IO.Put_Line (File, ",");
         end if;

         Pos := Pos + 1;
         Node_Lists.Next (Cursor);
      end loop;

      if Map.Indefinite then
         Ada.Text_IO.Put_Line
           (File,
            "   subtype " & Prefix & "_Hash is Natural range 0 .. "
            & Image (Last) & ';');
         Ada.Text_IO.Put_Line
           (File,
            "   function " & Prefix & "_Elements (Hash : "
            & Prefix & "_Hash)");
         Ada.Text_IO.Put_Line
           (File,
            "     return " & To_String (Map.Element_Type) & ';');
      else
         Ada.Text_IO.Put_Line
           (File,
            "   " & Prefix & "_Elements : constant array (0 .. " & Image (Last)
            & ") of " & To_String (Map.Element_Type));
         Pos := 0;
         Cursor := Map.Nodes.First;
         while Node_Lists.Has_Element (Cursor) loop
            if Pos = 0 then
               Ada.Text_IO.Put (File, "     := (");
            else
               Ada.Text_IO.Put (File, "         ");
            end if;

            Ada.Text_IO.Put
              (File, To_String (Node_Lists.Element (Cursor).Name));

            if Pos = Last then
               Ada.Text_IO.Put_Line (File, ");");
            else
               Ada.Text_IO.Put_Line (File, ",");
            end if;

            Pos := Pos + 1;
            Node_Lists.Next (Cursor);
         end loop;
      end if;
   end Write_Map_Private_Spec;


   procedure Write_Map_Public_Spec
     (Map : in Map_Description;
      File : in Ada.Text_IO.File_Type) is
   begin
568
569
570
571
572
573
574
575

576
577
578
579
580
581
582
583
584
585
586






587
588
589
590
591
592
593

   procedure Reset (Self : out Map_Description) is
   begin
      Self := (Element_Type => Hold (""),
               Hash_Package_Name => Hold (""),
               Function_Name => Hold (""),
               Not_Found => Hold (""),
               Nodes => Node_Lists.Empty_List);

   end Reset;


   procedure Insert
     (Self : in out Map_Description;
      Key : in String;
      Element_Name : in String) is
   begin
      Self.Nodes.Append  (Node (Key, Element_Name));
   end Insert;








   procedure Set_Element_Type
     (Self : in out Map_Description;
      Name : in String) is
   begin
      Self.Element_Type := Hold (Name);
   end Set_Element_Type;







|
>











>
>
>
>
>
>







616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648

   procedure Reset (Self : out Map_Description) is
   begin
      Self := (Element_Type => Hold (""),
               Hash_Package_Name => Hold (""),
               Function_Name => Hold (""),
               Not_Found => Hold (""),
               Nodes => Node_Lists.Empty_List,
               Indefinite => False);
   end Reset;


   procedure Insert
     (Self : in out Map_Description;
      Key : in String;
      Element_Name : in String) is
   begin
      Self.Nodes.Append  (Node (Key, Element_Name));
   end Insert;


   procedure Set_Definite (Self : in out Map_Description) is
   begin
      Self.Indefinite := False;
   end Set_Definite;


   procedure Set_Element_Type
     (Self : in out Map_Description;
      Name : in String) is
   begin
      Self.Element_Type := Hold (Name);
   end Set_Element_Type;
604
605
606
607
608
609
610








611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

626
627
628
629
630
631
632
633

634
635
636
637
638
639
640
   procedure Set_Hash_Package_Name
     (Self : in out Map_Description;
      Name : in String) is
   begin
      Self.Hash_Package_Name := Hold (Name);
   end Set_Hash_Package_Name;










   procedure Set_Not_Found
     (Self : in out Map_Description;
      Name : in String) is
   begin
      Self.Not_Found := Hold (Name);
   end Set_Not_Found;


   function Map
     (Element_Type : String;
      Nodes : Node_Array;
      Hash_Package_Name : String := "";
      Function_Name : String := "Element";
      Not_Found : String := "")

     return Map_Description
   is
      Result : Map_Description
        := (Element_Type => Hold (Element_Type),
            Hash_Package_Name => Hold (Hash_Package_Name),
            Function_Name => Hold (Function_Name),
            Not_Found => Hold (Not_Found),
            Nodes => Node_Lists.Empty_List);

   begin
      for I in Nodes'Range loop
         Result.Nodes.Append (Nodes (I));
      end loop;

      return Result;
   end Map;







>
>
>
>
>
>
>
>














|
>







|
>







659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
   procedure Set_Hash_Package_Name
     (Self : in out Map_Description;
      Name : in String) is
   begin
      Self.Hash_Package_Name := Hold (Name);
   end Set_Hash_Package_Name;


   procedure Set_Indefinite
     (Self : in out Map_Description;
      Indefinite : in Boolean := True) is
   begin
      Self.Indefinite := Indefinite;
   end Set_Indefinite;


   procedure Set_Not_Found
     (Self : in out Map_Description;
      Name : in String) is
   begin
      Self.Not_Found := Hold (Name);
   end Set_Not_Found;


   function Map
     (Element_Type : String;
      Nodes : Node_Array;
      Hash_Package_Name : String := "";
      Function_Name : String := "Element";
      Not_Found : String := "";
      Indefinite : Boolean := False)
     return Map_Description
   is
      Result : Map_Description
        := (Element_Type => Hold (Element_Type),
            Hash_Package_Name => Hold (Hash_Package_Name),
            Function_Name => Hold (Function_Name),
            Not_Found => Hold (Not_Found),
            Nodes => Node_Lists.Empty_List,
            Indefinite => Indefinite);
   begin
      for I in Nodes'Range loop
         Result.Nodes.Append (Nodes (I));
      end loop;

      return Result;
   end Map;

Modified src/natools-static_hash_maps.ads from [b53cc2dad2] to [5651639d9d].

42
43
44
45
46
47
48



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64







65
66
67
68
69
70
71
72
73
74
75
76
77

78
79
80
81
82
83
84

   procedure Insert
     (Self : in out Map_Description;
      Key : in String;
      Element_Name : in String);
      --  Add the pair Key and element designated by Name.




   procedure Set_Element_Type
     (Self : in out Map_Description;
      Name : in String);
      --  String used to desginate the returning type of the element function.
      --  This must be set.

   procedure Set_Function_Name
     (Self : in out Map_Description;
      Name : in String);
      --  Element function name, defaulting to "Element"

   procedure Set_Hash_Package_Name
     (Self : in out Map_Description;
      Name : in String);
      --  Set the package name where the perfect hash function will be write.
      --  Defaults to "<map package>.<element type>_Hash".








   procedure Set_Not_Found
     (Self : in out Map_Description;
      Name : in String);
      --  If non-empty, when element function is called with an unknown key,
      --  return the elemnet named Name instead of raising Constraint_Error.

   function Map
     (Element_Type : String;
      Nodes : Node_Array;
      Hash_Package_Name : String := "";
      Function_Name : String := "Element";
      Not_Found : String := "")

     return Map_Description;
      --  Create a map in a single call


   type Map_Package is private;

   procedure Open







>
>
>
















>
>
>
>
>
>
>












|
>







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

   procedure Insert
     (Self : in out Map_Description;
      Key : in String;
      Element_Name : in String);
      --  Add the pair Key and element designated by Name.

   procedure Set_Definite (Self : in out Map_Description);
      --  Use an array to store definite elements (default)

   procedure Set_Element_Type
     (Self : in out Map_Description;
      Name : in String);
      --  String used to desginate the returning type of the element function.
      --  This must be set.

   procedure Set_Function_Name
     (Self : in out Map_Description;
      Name : in String);
      --  Element function name, defaulting to "Element"

   procedure Set_Hash_Package_Name
     (Self : in out Map_Description;
      Name : in String);
      --  Set the package name where the perfect hash function will be write.
      --  Defaults to "<map package>.<element type>_Hash".

   procedure Set_Indefinite
     (Self : in out Map_Description;
      Indefinite : in Boolean := True);
      --  Set whether element type is indefinite (and returned by a function
      --  containing a case statement) or definite (and stored in a global
      --  array).

   procedure Set_Not_Found
     (Self : in out Map_Description;
      Name : in String);
      --  If non-empty, when element function is called with an unknown key,
      --  return the elemnet named Name instead of raising Constraint_Error.

   function Map
     (Element_Type : String;
      Nodes : Node_Array;
      Hash_Package_Name : String := "";
      Function_Name : String := "Element";
      Not_Found : String := "";
      Indefinite : Boolean := False)
     return Map_Description;
      --  Create a map in a single call


   type Map_Package is private;

   procedure Open
149
150
151
152
153
154
155

156
157
158
159
160
161
162

   type Map_Description is record
      Element_Type : String_Holder;
      Hash_Package_Name : String_Holder;
      Function_Name : String_Holder;
      Not_Found : String_Holder;
      Nodes : Node_Lists.List;

   end record;


   package Map_Lists is new Ada.Containers.Doubly_Linked_Lists
     (Map_Description);

   type Map_Package is record







>







160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

   type Map_Description is record
      Element_Type : String_Holder;
      Hash_Package_Name : String_Holder;
      Function_Name : String_Holder;
      Not_Found : String_Holder;
      Nodes : Node_Lists.List;
      Indefinite : Boolean := False;
   end record;


   package Map_Lists is new Ada.Containers.Doubly_Linked_Lists
     (Map_Description);

   type Map_Package is record