sile / patricia_tree

A memory-efficient patricia tree implementation written in Rust
MIT License
111 stars 17 forks source link

Add `PatriciaMap::insert_str()` and `PatriciaSet::insert_str()` methods #15

Closed sile closed 1 year ago

sile commented 2 years ago

See #14 for the motivation.

Examples

// [CURRENT] Insert keys as bytes.
let mut t = PatriciaMap::new();
t.insert("🌏🗻", ()); // [240,159,140,143,240,159,151,187]
t.insert("🌏🍔", ()); // [240,159,140,143,240,159,141,148]

let first_label = t.as_ref().child().unwrap().label();
assert!(std::str::from_utf8(first_label).is_err());
assert_eq!(first_label, [240, 159, 140, 143, 240, 159]);

// [NEW] Insert keys as strings.
let mut t = PatriciaMap::new();
t.insert_str("🌏🗻", ());
t.insert_str("🌏🍔", ());

let first_label = t.as_ref().child().unwrap().label();
assert_eq!(std::str::from_utf8(first_label).ok(), Some("🌏"));
vinaykakade commented 1 year ago

I was looking for using this in a project, but need utf8 support. Is there a plan to update/merge this PR?

sile commented 1 year ago

@vinaykakade Thank you for your comment. I'm going to merge this PR after some modifications.