Natools

Check-in [5c617d9676]
Login
Overview
Comment:tools/smaz: genericize Optimize_Dictionary
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5c617d9676e508d718769728c26fbc085cb1b95f
User & Date: nat on 2016-12-04 20:06:55
Other Links: manifest | tags
Context
2016-12-05
21:01
tools/smaz: genericize To_Dictionary check-in: 1dce15910c user: nat tags: trunk
2016-12-04
20:06
tools/smaz: genericize Optimize_Dictionary check-in: 5c617d9676 user: nat tags: trunk
2016-12-03
22:29
tools/smaz: genericize Evaluate_Dictionary check-in: bc86bc41ee user: nat tags: trunk
Changes

Modified tools/smaz.adb from [0fafdd0696] to [20a034958d].

124
125
126
127
128
129
130
131
132
133


134
135
136
137
138
139
140
141
142

143
144
145
146
147
148
149

150
151
152
153
154
155
156
157
158
124
125
126
127
128
129
130



131
132









133







134


135
136
137
138
139
140
141







-
-
-
+
+
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-







      Argument : in String)
     is null;


   function Getopt_Config return Getopt.Configuration;
      --  Build the configuration object

   procedure Optimization_Round
     (Dict : in out Holders.Holder;
      Score : in out Ada.Streams.Stream_Element_Count;
   function Last_Code (Dict : in Natools.Smaz_256.Dictionary)
     return Ada.Streams.Stream_Element
      Counts : in out Tools_256.Dictionary_Counts;
      Pending_Words : in out Natools.Smaz_Tools.String_Lists.List;
      Input_Texts : in Natools.Smaz_Tools.String_Lists.List;
      Job_Count : in Natural;
      Method : in Methods.Enum;
      Updated : out Boolean);
      --  Try to improve on Dict by replacing a single entry from it with
      --  one of the substring in Pending_Words.

     is (Dict.Last_Code);
   function Optimize_Dictionary
     (Base : in Natools.Smaz_256.Dictionary;
      Pending_Words : in Natools.Smaz_Tools.String_Lists.List;
      Input_Texts : in Natools.Smaz_Tools.String_Lists.List;
      Job_Count : in Natural;
      Method : in Methods.Enum)
     return Natools.Smaz_256.Dictionary;
      --  Return the last valid entry
      --  Optimize the dictionary on Input_Texts, starting with Base and
      --  adding substrings from Pending_Words.

   procedure Print_Dictionary
     (Filename : in String;
      Dictionary : in Natools.Smaz_256.Dictionary;
      Hash_Package_Name : in String := "");
   procedure Print_Dictionary
     (Output : in Ada.Text_IO.File_Type;
181
182
183
184
185
186
187

188
189
190
191


192
193










194
195
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257







+




+
+


+
+
+
+
+
+
+
+
+
+













+
+
+
+
+
+
+

+
+
+
+
+
+












+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







      --  Update Dictionary.Hash so that it can be actually used



   generic
      type Dictionary (<>) is private;
      type Dictionary_Entry is (<>);
      type Methods is (<>);
      type String_Count is range <>;

      type Dictionary_Counts is array (Dictionary_Entry) of String_Count;

      with package Holders
        is new Ada.Containers.Indefinite_Holders (Dictionary);
      with package String_Lists
        is new Ada.Containers.Indefinite_Doubly_Linked_Lists (String);

      with function Append_String
        (Dict : in Dictionary;
         Element : in String)
        return Dictionary;

      with function Dict_Entry
        (Dict : in Dictionary;
         Element : in Dictionary_Entry)
        return String;

      with procedure Evaluate_Dictionary
        (Dict : in Dictionary;
         Corpus : in String_Lists.List;
         Compressed_Size : out Ada.Streams.Stream_Element_Count;
         Counts : out Dictionary_Counts);

      with procedure Evaluate_Dictionary_Partial
        (Dict : in Dictionary;
         Corpus_Entry : in String;
         Compressed_Size : in out Ada.Streams.Stream_Element_Count;
         Counts : in out Dictionary_Counts);

      with function Last_Code (Dict : in Dictionary) return Dictionary_Entry;

      with function Remove_Element
        (Dict : in Dictionary;
         Element : in Dictionary_Entry)
        return Dictionary;

      with procedure Use_Dictionary (Dict : in out Dictionary) is <>;

      with function Worst_Element
        (Dict : in Dictionary;
         Counts : in Dictionary_Counts;
         Method : in Methods)
        return Dictionary_Entry;

   package Dictionary_Subprograms is

      procedure Evaluate_Dictionary
        (Job_Count : in Natural;
         Dict : in Dictionary;
         Corpus : in String_Lists.List;
         Compressed_Size : out Ada.Streams.Stream_Element_Count;
         Counts : out Dictionary_Counts);
         --  Dispatch to parallel or non-parallel version of
         --  Evaluate_Dictionary depending on Job_Count.

         procedure Optimization_Round
           (Dict : in out Holders.Holder;
            Score : in out Ada.Streams.Stream_Element_Count;
            Counts : in out Dictionary_Counts;
            Pending_Words : in out String_Lists.List;
            Input_Texts : in String_Lists.List;
            Job_Count : in Natural;
            Method : in Methods;
            Updated : out Boolean);
         --  Try to improve on Dict by replacing a single entry from it with
         --  one of the substring in Pending_Words.

         function Optimize_Dictionary
           (Base : in Dictionary;
            Pending_Words : in String_Lists.List;
            Input_Texts : in String_Lists.List;
            Job_Count : in Natural;
            Method : in Methods)
           return Dictionary;
         --  Optimize the dictionary on Input_Texts, starting with Base and
         --  adding substrings from Pending_Words.

      procedure Parallel_Evaluate_Dictionary
        (Job_Count : in Positive;
         Dict : in Dictionary;
         Corpus : in String_Lists.List;
         Compressed_Size : out Ada.Streams.Stream_Element_Count;
         Counts : out Dictionary_Counts);
         --  Return the same results as Natools.Smaz.Tools.Evaluate_Dictionary,
248
249
250
251
252
253
254






































































































255
256
257
258
259
260
261
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







               Actual_Dict, Corpus, Compressed_Size, Counts);
         else
            Evaluate_Dictionary
              (Actual_Dict, Corpus, Compressed_Size, Counts);
         end if;
      end Evaluate_Dictionary;


      procedure Optimization_Round
        (Dict : in out Holders.Holder;
         Score : in out Ada.Streams.Stream_Element_Count;
         Counts : in out Dictionary_Counts;
         Pending_Words : in out String_Lists.List;
         Input_Texts : in String_Lists.List;
         Job_Count : in Natural;
         Method : in Methods;
         Updated : out Boolean)
      is
         use type Ada.Streams.Stream_Element_Offset;

         New_Value : Ada.Strings.Unbounded.Unbounded_String;
         New_Position : String_Lists.Cursor;
         Worst_Index : constant Dictionary_Entry
           := Worst_Element (Dict.Element, Counts, Method);
         Worst_Value : constant String
           := Dict_Entry (Dict.Element, Worst_Index);
         Worst_Count : constant String_Count := Counts (Worst_Index);
         Base : constant Dictionary
           := Remove_Element (Dict.Element, Worst_Index);
         Old_Score : constant Ada.Streams.Stream_Element_Count := Score;
      begin
         Updated := False;

         for Position in Pending_Words.Iterate loop
            declare
               Word : constant String := String_Lists.Element (Position);
               New_Dict : constant Dictionary := Append_String (Base, Word);
               New_Score : Ada.Streams.Stream_Element_Count;
               New_Counts : Dictionary_Counts;
            begin
               Evaluate_Dictionary
                 (Job_Count, New_Dict, Input_Texts, New_Score, New_Counts);

               if New_Score < Score then
                  Dict := Holders.To_Holder (New_Dict);
                  Score := New_Score;
                  Counts := New_Counts;
                  New_Value
                    := Ada.Strings.Unbounded.To_Unbounded_String (Word);
                  New_Position := Position;
                  Updated := True;
               end if;
            end;
         end loop;

         if Updated then
            Pending_Words.Delete (New_Position);
            Pending_Words.Append (Worst_Value);

            Ada.Text_IO.Put_Line
              (Ada.Text_IO.Current_Error,
               "Removing"
               & Worst_Count'Img & "x "
               & Natools.String_Escapes.C_Escape_Hex (Worst_Value, True)
               & ", adding"
               & Counts (Last_Code (Dict.Element))'Img & "x "
               & Natools.String_Escapes.C_Escape_Hex
                  (Ada.Strings.Unbounded.To_String (New_Value), True)
               & ", size"
               & Score'Img
               & " ("
               & Ada.Streams.Stream_Element_Offset'Image (Score - Old_Score)
               & ')');
         end if;
      end Optimization_Round;


      function Optimize_Dictionary
        (Base : in Dictionary;
         Pending_Words : in String_Lists.List;
         Input_Texts : in String_Lists.List;
         Job_Count : in Natural;
         Method : in Methods)
        return Dictionary
      is
         Holder : Holders.Holder := Holders.To_Holder (Base);
         Pending : String_Lists.List := Pending_Words;
         Score : Ada.Streams.Stream_Element_Count;
         Counts : Dictionary_Counts;
         Running : Boolean := True;
      begin
         Evaluate_Dictionary
           (Job_Count, Base, Input_Texts, Score, Counts);

         while Running loop
            Optimization_Round
              (Holder,
               Score,
               Counts,
               Pending,
               Input_Texts,
               Job_Count,
               Method,
               Running);
         end loop;

         return Holder.Element;
      end Optimize_Dictionary;


      procedure Parallel_Evaluate_Dictionary
        (Job_Count : in Positive;
         Dict : in Dictionary;
         Corpus : in String_Lists.List;
         Compressed_Size : out Ada.Streams.Stream_Element_Count;
         Counts : out Dictionary_Counts)
343
344
345
346
347
348
349

350
351

352


353
354




355
356
357
358
359
360
361
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490

491
492
493
494
495
496
497
498
499
500
501







+


+

+
+

-
+
+
+
+







   end Dictionary_Subprograms;



   package Dict_256 is new Dictionary_Subprograms
     (Dictionary => Natools.Smaz_256.Dictionary,
      Dictionary_Entry => Ada.Streams.Stream_Element,
      Methods => Natools.Smaz_Tools.Methods.Enum,
      String_Count => Natools.Smaz_Tools.String_Count,
      Dictionary_Counts => Tools_256.Dictionary_Counts,
      Holders => Holders,
      String_Lists => Natools.Smaz_Tools.String_Lists,
      Append_String => Tools_256.Append_String,
      Dict_Entry => Natools.Smaz_256.Dict_Entry,
      Evaluate_Dictionary => Tools_256.Evaluate_Dictionary,
      Evaluate_Dictionary_Partial => Tools_256.Evaluate_Dictionary_Partial);
      Evaluate_Dictionary_Partial => Tools_256.Evaluate_Dictionary_Partial,
      Last_Code => Last_Code,
      Remove_Element => Tools_256.Remove_Element,
      Worst_Element => Tools_256.Worst_Index);



   overriding procedure Option
     (Handler  : in out Callback;
      Id       : in Options.Id;
      Argument : in String) is
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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
594
595
596
597
620
621
622
623
624
625
626








































































































627
628
629
630
631
632
633







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







      R.Add_Option ("no-vlen-verbatim",   No_Argument,       No_Vlen_Verbatim);
      R.Add_Option ("score-method",       Required_Argument, Score_Method);
      R.Add_Option ("vlen-verbatim",      No_Argument,       Vlen_Verbatim);

      return R;
   end Getopt_Config;


   procedure Optimization_Round
     (Dict : in out Holders.Holder;
      Score : in out Ada.Streams.Stream_Element_Count;
      Counts : in out Tools_256.Dictionary_Counts;
      Pending_Words : in out Natools.Smaz_Tools.String_Lists.List;
      Input_Texts : in Natools.Smaz_Tools.String_Lists.List;
      Job_Count : in Natural;
      Method : in Methods.Enum;
      Updated : out Boolean)
   is
      use type Ada.Streams.Stream_Element_Offset;

      New_Value : Ada.Strings.Unbounded.Unbounded_String;
      New_Position : Natools.Smaz_Tools.String_Lists.Cursor;
      Worst_Index : constant Ada.Streams.Stream_Element
        := Tools_256.Worst_Index (Dict.Element, Counts, Method);
      Worst_Value : constant String
        := Natools.Smaz_256.Dict_Entry (Dict.Element, Worst_Index);
      Worst_Count : constant Natools.Smaz_Tools.String_Count
        := Counts (Worst_Index);
      Base : constant Natools.Smaz_256.Dictionary
        := Tools_256.Remove_Element (Dict.Element, Worst_Index);
      Old_Score : constant Ada.Streams.Stream_Element_Count := Score;
   begin
      Updated := False;

      for Position in Pending_Words.Iterate loop
         declare
            Word : constant String
              := Natools.Smaz_Tools.String_Lists.Element (Position);
            New_Dict : constant Natools.Smaz_256.Dictionary
              := Tools_256.Append_String (Base, Word);
            New_Score : Ada.Streams.Stream_Element_Count;
            New_Counts : Tools_256.Dictionary_Counts;
         begin
            Dict_256.Evaluate_Dictionary
              (Job_Count, New_Dict, Input_Texts, New_Score, New_Counts);

            if New_Score < Score then
               Dict := Holders.To_Holder (New_Dict);
               Score := New_Score;
               Counts := New_Counts;
               New_Value := Ada.Strings.Unbounded.To_Unbounded_String (Word);
               New_Position := Position;
               Updated := True;
            end if;
         end;
      end loop;

      if Updated then
         Pending_Words.Delete (New_Position);
         Pending_Words.Append (Worst_Value);

         Ada.Text_IO.Put_Line
           (Ada.Text_IO.Current_Error,
            "Removing"
            & Worst_Count'Img & "x "
            & Natools.String_Escapes.C_Escape_Hex (Worst_Value, True)
            & ", adding"
            & Counts (Dict.Element.Last_Code)'Img & "x "
            & Natools.String_Escapes.C_Escape_Hex
               (Ada.Strings.Unbounded.To_String (New_Value), True)
            & ", size"
            & Score'Img
            & " ("
            & Ada.Streams.Stream_Element_Offset'Image (Score - Old_Score)
            & ')');
      end if;
   end Optimization_Round;


   function Optimize_Dictionary
     (Base : in Natools.Smaz_256.Dictionary;
      Pending_Words : in Natools.Smaz_Tools.String_Lists.List;
      Input_Texts : in Natools.Smaz_Tools.String_Lists.List;
      Job_Count : in Natural;
      Method : in Methods.Enum)
     return Natools.Smaz_256.Dictionary
   is
      Holder : Holders.Holder := Holders.To_Holder (Base);
      Pending : Natools.Smaz_Tools.String_Lists.List := Pending_Words;
      Score : Ada.Streams.Stream_Element_Count;
      Counts : Tools_256.Dictionary_Counts;
      Running : Boolean := True;
   begin
      Dict_256.Evaluate_Dictionary
        (Job_Count, Base, Input_Texts, Score, Counts);

      while Running loop
         Optimization_Round
           (Holder,
            Score,
            Counts,
            Pending,
            Input_Texts,
            Job_Count,
            Method,
            Running);
      end loop;

      return Holder.Element;
   end Optimize_Dictionary;


   procedure Print_Dictionary
     (Filename : in String;
      Dictionary : in Natools.Smaz_256.Dictionary;
      Hash_Package_Name : in String := "") is
   begin
      if Filename = "-" then
1097
1098
1099
1100
1101
1102
1103
1104

1105
1106
1107
1108
1109
1110
1111
1133
1134
1135
1136
1137
1138
1139

1140
1141
1142
1143
1144
1145
1146
1147







-
+







                       (Counter,
                        Handler.Dict_Size,
                        Selected,
                        Pending,
                        Handler.Score_Method,
                        Handler.Max_Pending);

                     return Optimize_Dictionary
                     return Dict_256.Optimize_Dictionary
                       (Tools_256.To_Dictionary
                          (Selected, Handler.Vlen_Verbatim),
                        Pending,
                        Input,
                        Handler.Job_Count,
                        Handler.Score_Method);
                  end;