Index: src/natools-smaz_generic-tools.adb ================================================================== --- src/natools-smaz_generic-tools.adb +++ src/natools-smaz_generic-tools.adb @@ -324,10 +324,80 @@ 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 Index: src/natools-smaz_generic-tools.ads ================================================================== --- src/natools-smaz_generic-tools.ads +++ src/natools-smaz_generic-tools.ads @@ -92,10 +92,23 @@ 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