Natools

Diff
Login

Differences From Artifact [3d7f94da33]:

To Artifact [80ca13ccbf]:


25
26
27
28
29
30
31

32
33
34
35
36
37
38
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39







+







with Ada.Strings.Unbounded;
with Ada.Text_IO.Text_Streams;
with Natools.Getopt_Long;
with Natools.S_Expressions.Parsers;
with Natools.S_Expressions.Printers;
with Natools.Smaz.Tools;
with Natools.Smaz.Tools.GNAT;
with Natools.String_Escapes;

procedure Smaz is
   function To_SEA (S : String) return Ada.Streams.Stream_Element_Array
     renames Natools.S_Expressions.To_Atom;

   package Actions is
      type Enum is
589
590
591
592
593
594
595
















































596
597
598






























































599
600
601
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
641
642
643
644



645
646
647
648
649
650
651
652
653
654
655
656
657
658
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
706
707
708
709







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



                        Ada.Strings.Both));
                     Sx_Output.Close_List;
                  end loop;
                  Sx_Output.Close_List;
               end if;

               if Handler.Stat_Output then
                  declare
                     type Score_Value is range 0 .. 2 ** 31 - 1;

                     function Length (E : Ada.Streams.Stream_Element)
                       return Score_Value
                       is (Natools.Smaz.Dict_Entry (Dictionary, E)'Length);

                     function Encoded (E : Ada.Streams.Stream_Element)
                       return Score_Value
                       is (Score_Value (Counts (E)) * Length (E));
                     function Frequency (E : Ada.Streams.Stream_Element)
                       return Score_Value
                       is (Score_Value (Counts (E)));
                     function Gain (E : Ada.Streams.Stream_Element)
                       return Score_Value
                       is (Score_Value (Counts (E)) * (Length (E) - 1));

                     procedure Print
                       (Label : in String;
                        E : in Ada.Streams.Stream_Element;
                        Score : in Score_Value);

                     procedure Print_Min_Max
                       (Label : in String;
                        Score : not null access function
                          (E : Ada.Streams.Stream_Element) return Score_Value);

                     procedure Print_Value
                       (Label : in String;
                        Score : not null access function
                          (E : Ada.Streams.Stream_Element) return Score_Value;
                        Ref : in Score_Value);


                     procedure Print
                       (Label : in String;
                        E : in Ada.Streams.Stream_Element;
                        Score : in Score_Value) is
                     begin
                        if Handler.Sx_Output then
                           Sx_Output.Open_List;
                           Sx_Output.Append_Atom ((0 => E));
                           Sx_Output.Append_String
                             (Natools.Smaz.Dict_Entry (Dictionary, E));
                           Sx_Output.Append_String (Ada.Strings.Fixed.Trim
                             (Score_Value'Image (Score), Ada.Strings.Both));
                           Sx_Output.Close_List;
                        else
                  Ada.Text_IO.Put_Line ("Not implemented yet");
               end if;
            end;
                           Ada.Text_IO.Put_Line
                             (Label
                              & Ada.Characters.Latin_1.HT
                              & Ada.Streams.Stream_Element'Image (E)
                              & Ada.Characters.Latin_1.HT
                              & Natools.String_Escapes.C_Escape_Hex
                                (Natools.Smaz.Dict_Entry (Dictionary, E), True)
                              & Ada.Characters.Latin_1.HT
                              & Score_Value'Image (Score));
                        end if;
                     end Print;

                     procedure Print_Min_Max
                       (Label : in String;
                        Score : not null access function
                          (E : Ada.Streams.Stream_Element) return Score_Value)
                     is
                        Min_Score, Max_Score : Score_Value := Score (0);
                        S : Score_Value;
                     begin
                        for E in 1 .. Dictionary.Dict_Last loop
                           S := Score (E);
                           if S < Min_Score then
                              Min_Score := S;
                           end if;
                           if S > Max_Score then
                              Max_Score := S;
                           end if;
                        end loop;

                        Print_Value ("best-" & Label, Score, Max_Score);
                        Print_Value ("worst-" & Label, Score, Min_Score);
                     end Print_Min_Max;

                     procedure Print_Value
                       (Label : in String;
                        Score : not null access function
                          (E : Ada.Streams.Stream_Element) return Score_Value;
                        Ref : in Score_Value) is
                     begin
                        if Handler.Sx_Output then
                           Sx_Output.Open_List;
                           Sx_Output.Append_String (Label);
                        end if;

                        for E in Dictionary.Offsets'Range loop
                           if Score (E) = Ref then
                              Print (Label, E, Ref);
                           end if;
                        end loop;

                        if Handler.Sx_Output then
                           Sx_Output.Close_List;
                        end if;
                     end Print_Value;
                  begin
                     Print_Min_Max ("encoded", Encoded'Access);
                     Print_Min_Max ("frequency", Frequency'Access);
                     Print_Min_Max ("gain", Gain'Access);
                  end;
               end if;
            end;
      end case;
   end Build_Dictionary;
end Smaz;