Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Dec 9, 2023
1 parent 0a2bf42 commit b710d1f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,32 @@ impl<K: Bytes, V> GenericPatriciaMap<K, V> {
Some((K::Borrowed::from_bytes(key), value))
}

/// Returns the longest common prefix length of `key` and the keys in this map.
///
/// Different from `get_longest_common_prefix()`, this method does not consider if there is
///
/// # Examples
///
/// ```
/// use patricia_tree::PatriciaMap;
///
/// let mut map = PatriciaMap::new();
/// map.insert("foo", 1);
/// map.insert("foobar", 2);
/// assert_eq!(map.get_longest_common_prefix_len("fo"), 2);
/// assert_eq!(map.get_longest_common_prefix_len("foo"), 3);

Check failure on line 235 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (beta)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope

Check failure on line 235 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (nightly)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope
/// assert_eq!(map.get_longest_common_prefix_len("fooba"), 5);

Check failure on line 236 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (beta)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope

Check failure on line 236 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (nightly)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope
/// assert_eq!(map.get_longest_common_prefix_len("foobar"), 6);

Check failure on line 237 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (beta)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope

Check failure on line 237 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (nightly)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope
/// assert_eq!(map.get_longest_common_prefix_len("foobarbaz"), 6);

Check failure on line 238 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (beta)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope

Check failure on line 238 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (nightly)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope
/// assert_eq!(map.get_longest_common_prefix_len("foba"), 2);

Check failure on line 239 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (beta)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope

Check failure on line 239 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (nightly)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope
/// ```

Check failure on line 240 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (beta)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope

Check failure on line 240 in src/map.rs

View workflow job for this annotation

GitHub Actions / Test Suite (nightly)

no method named `get_longest_common_prefix_len` found for struct `GenericPatriciaMap` in the current scope
pub fn longest_common_prefix_len<Q>(&self, key: &Q) -> usize
where
Q: ?Sized + AsRef<K::Borrowed>,
{
self.tree.longest_common_prefix_len(key.as_ref())
}

/// Inserts a key-value pair into this map.
///
/// If the map did not have this key present, `None` is returned.
Expand Down
24 changes: 23 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,29 @@ impl<V> Node<V> {
None
}
}

pub(crate) fn longest_common_prefix_len<K: ?Sized + BorrowedBytes>(
&self,
key: &K,
offset: usize,
) -> usize {
let (next, common_prefix_len) = key.strip_common_prefix_and_len(self.label());
let next_offset = offset + common_prefix_len;
if common_prefix_len == self.label().len() {
if next.is_empty() {
next_offset
} else {
self.child()
.map(|child| child.longest_common_prefix_len(next, next_offset))
.unwrap_or(next_offset)
}
} else if common_prefix_len == 0 && key.cmp_first_item(self.label()).is_ge() {
self.sibling()
.map(|sibling| sibling.longest_common_prefix_len(next, offset))
.unwrap_or(next_offset)
} else {
next_offset
}
}
pub(crate) fn get_longest_common_prefix<K: ?Sized + BorrowedBytes>(
&self,
key: &K,
Expand Down
3 changes: 3 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ impl<V> PatriciaTree<V> {
pub fn get_mut<K: ?Sized + BorrowedBytes>(&mut self, key: &K) -> Option<&mut V> {
self.root.get_mut(key)
}
pub fn longest_common_prefix_len<K: ?Sized + BorrowedBytes>(&self, key: &K) -> usize {
self.root.longest_common_prefix_len(key, 0)
}
pub fn get_longest_common_prefix<'a, K: ?Sized + BorrowedBytes>(
&self,
key: &'a K,
Expand Down

0 comments on commit b710d1f

Please sign in to comment.