billstclair / elm-id-search / IdSearch

Indexes a set of records by identifying strings.

Create a table


type alias Table a =
{ getIdentifiers : a -> List String
, dictCount : Basics.Int
, dicts : List (Dict String (List a)) 
}

The table type.

makeTable : Basics.Int -> (a -> List String) -> Table a

Make a table.

makeTable dictCount getIdentifiers

dictCount is the number of dictionaries to populate. The dictionaries map prefixes of the search string to matches. If the search string is longer than dictCount, then a linear search is done on the results from the final dictionary.

This provides a tradeoff between insertion speed, storage space, and lookup speed.

A dictCount of 3 or 4 is probably right for most applications.

The list of strings returned by getIdentifiers are the indices for an inserted element. Usually, there will only be one.

Insert and lookup

insert : a -> Table a -> Table a

Insert a record in the table.

insertList : List a -> Table a -> Table a

Insert multiple elements into a table.

lookup : String -> Table a -> List a

Lookup a string in a table.

remove : a -> Table a -> Table a

Remove a record from a table.

This implementation is slow, looping over every element of every dictionary, since I don't expect it to be used much.

Utilities

findSubstrings : Basics.Int -> String -> List String

Get a string's substrings of a given length.

If the length is 1, returns only the first character in the string.

None of the substrings will contain spaces. Each element in the returned list will be unique.