prefix_dictionary

Crates.ioprefix_dictionary
lib.rsprefix_dictionary
version0.0.2
sourcesrc
created_at2024-05-30 13:49:06.975086
updated_at2024-05-30 13:53:34.420781
descriptionData structure similar to a dictionary, but enabling search for prefixes
homepagehttps://github.com/JeWaVe/head-dictionary
repositoryhttps://github.com/JeWaVe/head-dictionary
max_upload_size
id1256882
size8,192
Portalez RĂ©gis (JeWaVe)

documentation

https://docs.rs/prefix_dictionary/

README

PrefixDictionary

A PrefixDictionary is like a dictionary, but enabling the capacity to search by prefix. It's underlying structure is a n-ary tree.

It's generic (template) on data, but most common use is with characters.

Pseudo code

a = PrefixDictionary("dictionary", "lapin", "lapins", "lapine")

  • a.contains("dict") -> as_prefix because "dict" is not in the word list but only a prefix
  • a.contains("dictionary") -> as_word because "dictionary" is an actual word and no longer word exist
  • a.contains("lapin") -> as_prefix | as_word because "lapin" is both a word and a prefix (for "lapins" and "lapine")
  • a.contains("tutu") -> None because tutu is not in the dictionary

Note

In the examples above, only the first t of the word "tutu" will be read as we know (from the structure of dictionary), that no words inserted starts with the letter t.

Example

pub fn test_simple_1() {
    let mut dict = PrefixDictionary::new();
    dict.feed(&["dictionary", "lapin", "lapins", "lapine"]);
    assert_eq!(4, dict.len());

    let mut result = dict.contains("dict");
    assert!(result.is_some());
    assert_eq!(result.unwrap(), SearchResult::AS_PREFIX);

    result = dict.contains("lapin");
    assert!(result.is_some());
    assert_eq!(result.unwrap(), SearchResult::AS_PREFIX | SearchResult::AS_WORD);

    result = dict.contains("dictionary");
    assert!(result.is_some());
    assert_eq!(result.unwrap(), SearchResult::AS_WORD);

    result = dict.contains("tutu");
    assert!(result.is_none());
}
Commit count: 7

cargo fmt