Index: src/natools-smaz-tools.adb ================================================================== --- src/natools-smaz-tools.adb +++ src/natools-smaz-tools.adb @@ -49,22 +49,10 @@ ---------------------- -- Public Interface -- ---------------------- - - function Linear_Search (Value : String) return Natural is - Result : Ada.Streams.Stream_Element := 0; - begin - for S of List_For_Linear_Search loop - exit when S = Value; - Result := Result + 1; - end loop; - - return Natural (Result); - end Linear_Search; - procedure Print_Dictionary_In_Ada (Dict : in Dictionary; Hash_Image : in String := "TODO"; Max_Width : in Positive := 70; @@ -346,10 +334,49 @@ Values => Values, Hash => Dummy_Hash'Access); end; end To_Dictionary; + + + --------------------------------- + -- Dynamic Dictionary Searches -- + --------------------------------- + + function Linear_Search (Value : String) return Natural is + Result : Ada.Streams.Stream_Element := 0; + begin + for S of List_For_Linear_Search loop + exit when S = Value; + Result := Result + 1; + end loop; + + return Natural (Result); + end Linear_Search; + + + function Map_Search (Value : String) return Natural is + Cursor : constant Dictionary_Maps.Cursor + := Dictionary_Maps.Find (Search_Map, Value); + begin + if Dictionary_Maps.Has_Element (Cursor) then + return Natural (Dictionary_Maps.Element (Cursor)); + else + return Natural (Ada.Streams.Stream_Element'Last); + end if; + end Map_Search; + + + procedure Set_Dictionary_For_Map_Search (Dict : in Dictionary) is + begin + Dictionary_Maps.Clear (Search_Map); + + for I in Dict.Offsets'Range loop + Dictionary_Maps.Insert (Search_Map, Dict_Entry (Dict, I), I); + end loop; + end Set_Dictionary_For_Map_Search; + ------------------- -- Word Counting -- ------------------- Index: src/natools-smaz-tools.ads ================================================================== --- src/natools-smaz-tools.ads +++ src/natools-smaz-tools.ads @@ -67,10 +67,15 @@ 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; + -- Function and data source for logarithmic search using standard + -- ordered map, that can be used with Dictionary.Hash. + type String_Count is range 0 .. 2 ** 31 - 1; -- Type for a number of substring occurrences type Word_Counter is private; -- Accumulate frequency/occurrence counts for a set of strings @@ -146,7 +151,12 @@ function To_Scored_Word (Cursor : in Word_Maps.Cursor) return Scored_Word; package Scored_Word_Sets is new Ada.Containers.Indefinite_Ordered_Sets (Scored_Word); + + package Dictionary_Maps is new Ada.Containers.Indefinite_Ordered_Maps + (String, Ada.Streams.Stream_Element); + + Search_Map : Dictionary_Maps.Map; end Natools.Smaz.Tools;