Code Monkey home page Code Monkey logo

ptrie's Introduction

๐ŸŽ„ Prefix Trie

Crates.io Test Release Documentation Codecov status MIT license

PTrie is a generic implementation of the trie data structure with no dependencies, tailored for easy and efficient prefix and postfix search within a collection of objects, such as strings.

The structure is defined as Trie<K, V>, where K represents the type of keys in each node (an iterator of the chain to index), and V is the type of the associated values (any object to which the key points to).

๐Ÿ’ญ Motivation

The trie is particularly effective for operations involving common prefix identification and retrieval, making it a good choice for applications that require fast and efficient prefix-based search functionalities.

๐Ÿš€ Usage

Results are sorted in ascending order of their length.

โœจ Find prefixes

You can return all prefixes in the trie that matches a given string, or directly retrieve the longest prefix.

use ptrie::Trie;

let mut trie = Trie::new();

trie.insert("a".bytes(), "A");
trie.insert("ab".bytes(), "AB");
trie.insert("abc".bytes(), "ABC");
trie.insert("abcde".bytes(), "ABCDE");

// Find all potential prefixes
let prefixes = trie.find_prefixes("abcd".bytes());
assert_eq!(prefixes, vec![&"A", &"AB", &"ABC"]);

// Find the longest prefix
let longest = trie.find_longest_prefix("abcd".bytes());
assert_eq!(longest, Some("ABC").as_ref());

// Find longest with length
if let Some((length, prefix)) = trie.find_longest_prefix_len("abcd".bytes()) {
    println!("Longest prefix: {} {}", prefix, length);
}

๐Ÿ” Find postfixes

You can also find all postfixes in the trie, e.g. all strings which have the given string as a prefix, and extends it.

use ptrie::Trie;

let mut trie = Trie::new();

trie.insert("app".bytes(), "App");
trie.insert("apple".bytes(), "Apple");
trie.insert("applet".bytes(), "Applet");
trie.insert("apricot".bytes(), "Apricot");

let strings = trie.find_postfixes("app".bytes());
assert_eq!(strings, vec![&"App", &"Apple", &"Applet"]);

๐Ÿ”‘ Key-based retrieval functions

The crate provides functions to check for the existence of a key, to retrieve the associated value, or iterate the trie nodes.

use ptrie::Trie;

let mut trie = Trie::new();
trie.insert("app".bytes(), "App");
trie.insert("applet".bytes(), "Applet");

// Get a key
assert!(trie.contains_key("app".bytes()));
assert!(!trie.contains_key("not_existing_key".bytes()));
assert_eq!(trie.get("app".bytes()), Some("App").as_ref());
assert_eq!(trie.get("none".bytes()), None.as_ref());

// Iterate the trie
for (k, v) in &trie {
    println!("kv: {:?} {}", k, v);
}

// Remove a key
trie.remove("app".bytes());
assert!(!trie.contains_key("app".bytes()));

๐Ÿท๏ธ Features

The serde feature adds Serde Serialize and Deserialize traits to the Trie and TrieNode struct.

ptrie = { version = "0.6", features = ["serde"] }

๐Ÿ› ๏ธ Contributing

Contributions are welcome, checkout the CONTRIBUTING.md for instructions to run the project in development.

๐Ÿ“œ Changelog

Changelog available in the CHANGELOG.md.

โš–๏ธ License

MIT License

ptrie's People

Contributors

aserebryakov avatar vemonet avatar pacak avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

pacak planetoryd

ptrie's Issues

`.remove` method missing

Hi!

I'd love to use this crate in one of my projects, but I need the functionality to remove nodes at some point, which this crate does not allow. Is this intended?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.