Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | smaz-tools: new primitive for in-place element replacement |
|---|---|
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA1: |
7fb143f443c363047d6de392cc6206e8 |
| User & Date: | nat 2017-05-07 20:27:40.041 |
Context
|
2017-05-08
| ||
| 21:48 | tools/smaz: add a command-line option for a list of forced words check-in: 95e42d23fe user: nat tags: trunk | |
|
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 | |
Changes
Changes to src/natools-smaz-tools.adb.
| ︙ | |||
437 438 439 440 441 442 443 444 445 446 447 448 449 450 | 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + |
Variable_Length_Verbatim => Dict.Variable_Length_Verbatim,
Max_Word_Length => New_Max_Word_Length,
Offsets => New_Offsets,
Values => New_Values,
Hash => Dummy_Hash'Access);
end Remove_Element;
function Replace_Element
(Dict : in Dictionary;
Index : in Ada.Streams.Stream_Element;
Value : in String)
return Dictionary
is
Removed_Length : constant Positive := Dict_Entry (Dict, Index)'Length;
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.Dict_Last);
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 = 0 then
return Value
& Dict.Values (Dict.Offsets (Index + 1) .. Dict.Values'Last);
elsif Index < Dict.Dict_Last then
return Dict.Values (1 .. Dict.Offsets (Index) - 1)
& Value
& Dict.Values (Dict.Offsets (Index + 1) .. 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 (Dict, I)'Length > New_Max_Word_Length
then
New_Max_Word_Length := Dict_Entry (Dict, I)'Length;
end if;
end loop;
end if;
if New_Max_Word_Length < Value'Length then
New_Max_Word_Length := Value'Length;
end if;
return Dictionary'
(Dict_Last => Dict.Dict_Last,
String_Size => Dict.String_Size + Length_Delta,
Variable_Length_Verbatim => Dict.Variable_Length_Verbatim,
Max_Word_Length => New_Max_Word_Length,
Offsets => New_Offsets,
Values => New_Values,
Hash => Dummy_Hash'Access);
end Replace_Element;
function To_Dictionary
(List : in String_Lists.List;
Variable_Length_Verbatim : in Boolean)
return Dictionary
is
Dict_Last : constant Ada.Streams.Stream_Element
|
| ︙ |
Changes to src/natools-smaz-tools.ads.
| ︙ | |||
92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 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 117 118 | + + + + + + + + + + + + + |
=> Dict_Entry (Dict, I)
= Dict_Entry (Append_String'Result, I))
and then Dict_Entry (Append_String'Result,
Append_String'Result.Dict_Last)
= Value;
-- Return a new dictionary with Value appended
function Replace_Element
(Dict : in Dictionary;
Index : in Ada.Streams.Stream_Element;
Value : in String)
return Dictionary
with Pre => Index <= Dict.Dict_Last and then Value'Length > 0,
Post => Dict.Dict_Last = Replace_Element'Result.Dict_Last
and then (for all I in 0 .. Dict.Dict_Last
=> (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
List_For_Linear_Search : String_Lists.List;
function Linear_Search (Value : String) return Natural;
-- Function and data source for inefficient but dynamic function
-- that can be used with Dictionary.Hash.
procedure Set_Dictionary_For_Map_Search (Dict : in Dictionary);
function Map_Search (Value : String) return Natural;
|
| ︙ |