Code Monkey home page Code Monkey logo

quicktree's Introduction

quicktree


A hashmap backed tree implementation. Provides constant access to a node given its id. Especially useful for UI DOMs.

Implementation

quicktree uses a map-based implementation of a tree. Rather than the "naive" implementation of a tree in which each node contains a vector of child nodes, we instead store nodes in a map, indexable by an id. Each node then stores the ids of its children, rather than a pointer to its children directly.

The former implementation is essentially a generalized linked-list, and thus suffers from similar performance issues (e.g., poor CPU-cache localization, linear-time access). The latter instead offers constant time access to a node given its unique id.

This will be especially appealing for applications such as UI DOMs.

Usage

Cargo

Add the following to your Cargo.toml:

[dependencies.quicktree]
git = "https://github.com/raunakab/quicktree.git"

Buck2

Steve Klabnik has an amazing series of posts on how to include third-party dependencies (vendored or non-vendored) in your buck2 project. This crate does not contain any build scripts, so you will not need to perform any additional configurations.

Other

You will need search up your specific build tool's docs in order to get up and running with quicktree in your project.

Example

use quicktree::Tree;

let mut tree = Tree::<&'static str>::default();

let root_id = tree.insert_root("Hello");
let child_1_id = tree.insert(root_id, "world!").unwrap();
let child_2_id = tree.insert(root_id, "there!").unwrap();
let child_3_id = tree.insert(root_id, "old friend!").unwrap();

assert_eq!(*tree.get(root_id).unwrap().value, "Hello");
assert_eq!(*tree.get(child_1_id).unwrap().value, "world!");
assert_eq!(*tree.get(child_2_id).unwrap().value, "there!");
assert_eq!(*tree.get(child_3_id).unwrap().value, "old friend!");

let _removed_node = tree.remove(child_3_id).unwrap();

assert_eq!(*tree.get(root_id).unwrap().value, "Hello");
assert_eq!(*tree.get(child_1_id).unwrap().value, "world!");
assert_eq!(*tree.get(child_2_id).unwrap().value, "there!");
assert_eq!(tree.get(child_3_id), None);

Benches

Based off of some limited benchmarks (on my desktop PC), the following metrics are being observed:

Test Average Time [microseconds]
Inserting 1000 children 24.172
Inserting 1000 children and then removing the root 24.724

Please note that these results are hardware specific, and should not be used for comparisons against other implementations! You can run these results yourself by cloning the repository and running:

cargo bench

License

Licensed under the MIT License.

quicktree's People

Contributors

raunakab avatar

Stargazers

 avatar

Watchers

 avatar

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.