Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not clone prefix when inserting #59

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,16 +705,13 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
depth: usize,
replace: bool,
) -> NodeArc<P, V> {
// Obtain the current node's prefix and its length.
let cur_node_prefix = cur_node.prefix().clone();

let (key_prefix, new_key, prefix, is_prefix_match, longest_common_prefix) =
Self::common_insert_logic(&cur_node_prefix, key, depth);
Self::common_insert_logic(cur_node.prefix(), key, depth);

// If the current node is a Twig node and the prefixes match up to the end of both prefixes,
// update the existing value in the Twig node.
if let NodeType::Twig(ref twig) = &cur_node.node_type {
if is_prefix_match && cur_node_prefix.len() == key_prefix.len() {
if is_prefix_match && twig.prefix.len() == key_prefix.len() {
let new_twig = if replace {
// Create a replacement Twig node with the new value only.
let mut new_twig = TwigNode::new(twig.prefix.clone(), twig.key.clone());
Expand All @@ -735,7 +732,7 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
old_node.set_prefix(new_key);
let mut n4 = Node::new_node4(prefix);

let k1 = cur_node_prefix.at(longest_common_prefix);
let k1 = cur_node.prefix().at(longest_common_prefix);
let k2 = key_prefix[longest_common_prefix];
let new_twig = Node::new_twig(
key_prefix[longest_common_prefix..].into(),
Expand Down Expand Up @@ -788,16 +785,13 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
depth: usize,
replace: bool,
) {
// Obtain the current node's prefix and its length.
let cur_node_prefix = cur_node.prefix().clone();

let (key_prefix, new_key, prefix, is_prefix_match, longest_common_prefix) =
Self::common_insert_logic(&cur_node_prefix, key, depth);
Self::common_insert_logic(cur_node.prefix(), key, depth);

// If the current node is a Twig node and the prefixes match up to the end of both prefixes,
// update the existing value in the Twig node.
if let NodeType::Twig(ref mut twig) = &mut cur_node.node_type {
if is_prefix_match && cur_node_prefix.len() == key_prefix.len() {
if is_prefix_match && twig.prefix.len() == key_prefix.len() {
if replace {
// Only replace if the provided value is more recent than
// the existing ones. This is important because this method
Expand All @@ -815,13 +809,14 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {

// If the prefixes don't match, create a new Node4 with the old node and a new Twig as children.
if !is_prefix_match {
cur_node.set_prefix(new_key);

let n4 = Node::new_node4(prefix);

let k1 = cur_node_prefix.at(longest_common_prefix);
let k1 = cur_node.prefix().at(longest_common_prefix);
let k2 = key_prefix[longest_common_prefix];

// Must be set after the calculation of k1 above.
cur_node.set_prefix(new_key);

let old_node = std::mem::replace(cur_node, n4);

let new_twig = Node::new_twig(
Expand Down
Loading