Natools

Check-in [df71bfedf6]
Login
Overview
Comment:smaz_generic-tools: new primitive for in-place element replacement
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: df71bfedf6ca78deca71fd0a09a057db43c8ae14
User & Date: nat on 2017-05-06 21:05:09
Other Links: manifest | tags
Context
2017-05-07
20:27
smaz-tools: new primitive for in-place element replacement check-in: 7fb143f443 user: nat tags: trunk
2017-05-06
21:05
smaz_generic-tools: new primitive for in-place element replacement check-in: df71bfedf6 user: nat tags: trunk
2017-05-05
19:50
s_expressions-parsers-tests: fix alphabetical ordering of tests check-in: a76936f058 user: nat tags: trunk
Changes

Modified src/natools-smaz_generic-tools.adb from [bd000f80e7] to [6d6d345c58].

322
323
324
325
326
327
328






































































329
330
331
332
333
334
335
         Variable_Length_Verbatim => Dict.Variable_Length_Verbatim,
         Max_Word_Length => New_Max_Word_Length,
         Offsets => New_Offsets,
         Values => New_Values,
         Hash => Smaz_Tools.Dummy_Hash'Access);
   end Remove_Element;








































































   function To_Dictionary
     (List : in String_Lists.List;
      Variable_Length_Verbatim : in Boolean)
     return Dictionary
   is
      Code_After_Last : Dictionary_Code := Dictionary_Code'First;







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







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
395
396
397
398
399
400
401
402
403
404
405
         Variable_Length_Verbatim => Dict.Variable_Length_Verbatim,
         Max_Word_Length => New_Max_Word_Length,
         Offsets => New_Offsets,
         Values => New_Values,
         Hash => Smaz_Tools.Dummy_Hash'Access);
   end Remove_Element;


   function Replace_Element
     (Dict : in Dictionary;
      Index : in Dictionary_Code;
      Value : in String)
     return Dictionary
   is
      Removed_Length : constant Positive := Dict_Entry_Length (Dict, Index);
      Length_Delta : constant Integer := Value'Length - Removed_Length;

      function New_Offsets return Offset_Array;
      function New_Values return String;

      function New_Offsets return Offset_Array is
         Result : Offset_Array (Dict.Offsets'First .. Dict.Last_Code);
      begin
         for I in Result'Range loop
            if I <= Index then
               Result (I) := Dict.Offsets (I);
            else
               Result (I) := Dict.Offsets (I) + Length_Delta;
            end if;
         end loop;

         return Result;
      end New_Offsets;

      function New_Values return String is
      begin
         if Index = Dictionary_Code'First then
            return Value & Dict.Values
              (Dict.Offsets (Dictionary_Code'Succ (Index))
               .. Dict.Values'Last);
         elsif Index < Dict.Last_Code then
            return Dict.Values (1 .. Dict.Offsets (Index) - 1)
              & Value
              & Dict.Values (Dict.Offsets (Dictionary_Code'Succ (Index))
                             .. Dict.Values'Last);
         else
            return Dict.Values (1 .. Dict.Offsets (Index) - 1) & Value;
         end if;
      end New_Values;

      New_Max_Word_Length : Positive := Dict.Max_Word_Length;
   begin
      if Removed_Length = Dict.Max_Word_Length then
         New_Max_Word_Length := 1;
         for I in Dict.Offsets'Range loop
            if I /= Index
              and then Dict_Entry_Length (Dict, I) > New_Max_Word_Length
            then
               New_Max_Word_Length := Dict_Entry_Length (Dict, I);
            end if;
         end loop;
      end if;

      if New_Max_Word_Length < Value'Length then
         New_Max_Word_Length := Value'Length;
      end if;

      return Dictionary'
        (Last_Code => Dict.Last_Code,
         Values_Last => Dict.Values_Last + Length_Delta,
         Variable_Length_Verbatim => Dict.Variable_Length_Verbatim,
         Max_Word_Length => New_Max_Word_Length,
         Offsets => New_Offsets,
         Values => New_Values,
         Hash => Smaz_Tools.Dummy_Hash'Access);
   end Replace_Element;


   function To_Dictionary
     (List : in String_Lists.List;
      Variable_Length_Verbatim : in Boolean)
     return Dictionary
   is
      Code_After_Last : Dictionary_Code := Dictionary_Code'First;

Modified src/natools-smaz_generic-tools.ads from [d93257e757] to [af4ec8d531].

90
91
92
93
94
95
96













97
98
99
100
101
102
103
                  => Dict_Entry (Dict, I)
                     = Dict_Entry (Append_String'Result, I))
               and then Dict_Entry (Append_String'Result,
                                    Append_String'Result.Last_Code)
                        = Value;
      --  Return a new dictionary with Value appended















   type Dictionary_Counts is
     array (Dictionary_Code) of Smaz_Tools.String_Count;

   procedure Evaluate_Dictionary
     (Dict : in Dictionary;
      Corpus : in String_Lists.List;







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







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
                  => Dict_Entry (Dict, I)
                     = Dict_Entry (Append_String'Result, I))
               and then Dict_Entry (Append_String'Result,
                                    Append_String'Result.Last_Code)
                        = Value;
      --  Return a new dictionary with Value appended

   function Replace_Element
     (Dict : in Dictionary;
      Index : in Dictionary_Code;
      Value : in String)
     return Dictionary
     with Pre => Index <= Dict.Last_Code and then Value'Length > 0,
         Post => Dict.Last_Code = Replace_Element'Result.Last_Code
               and then (for all I in Dictionary_Code'First .. Dict.Last_Code
                  => (I = Index or else Dict_Entry (Dict, I)
                                    = Dict_Entry (Replace_Element'Result, I)))
               and then Dict_Entry (Replace_Element'Result, Index) = Value;
      --  Return a new dictionary with entry at Index replaced by Value


   type Dictionary_Counts is
     array (Dictionary_Code) of Smaz_Tools.String_Count;

   procedure Evaluate_Dictionary
     (Dict : in Dictionary;
      Corpus : in String_Lists.List;