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

Add values to Augmentation functions #35

Merged
merged 9 commits into from
Nov 4, 2024
Merged

Add values to Augmentation functions #35

merged 9 commits into from
Nov 4, 2024

Conversation

shuoli84
Copy link
Owner

@shuoli84 shuoli84 commented Sep 1, 2024

This is a rough workable version to handle duplicate values with "Upsert" approach.

@shuoli84 shuoli84 changed the title init workable version Examples to handle duplicate values Sep 1, 2024
@shuoli84
Copy link
Owner Author

shuoli84 commented Sep 1, 2024

#24 ref

@YeungOnion
Copy link

Hmm, but key removal and reinsertion might require rebalancing twice (I did not prove this, but I believe it is true).

Perhaps I am simply wanting too specific of a behavior from a general data structure while maintaining all of its advantages.

@shuoli84
Copy link
Owner Author

shuoli84 commented Oct 5, 2024

Just tweaked the Augmentation trait, and pass in the values reference. The code indead looks much cleaner.
But "get_mut" functions won't update the augmentation. I think it is ok as long as we make it clear in the doc..

@shuoli84
Copy link
Owner Author

shuoli84 commented Oct 5, 2024

fn insert(tree: &mut BPlusTree<NodeStoreVec<i64, usize, Count>>, key: i64) {
    let prev_count = tree.get(&key).cloned().unwrap_or_default();
    tree.insert(key, prev_count + 1);
}

fn delete(tree: &mut BPlusTree<NodeStoreVec<i64, usize, Count>>, key: i64) {
    let prev_count = tree.get(&key).cloned().unwrap_or_default();
    if prev_count == 1 {
        tree.remove(&key);
    } else {
        tree.insert(key, prev_count - 1);
    }
}

@YeungOnion
Copy link

Hey thanks for doing this!

I suppose that implementing key only augmentations will be optimized by the compiler. Did you compare any benchmarks to make sure you're not losing out on perf?

@shuoli84
Copy link
Owner Author

shuoli84 commented Oct 6, 2024

Noop, I did not do any benchmark yet. I believe the compiler should kick in and optimize it, but that's hard to say... It would be great to define a new set of benchmark for augmentation features.

Before doing that, do you think the implementation suits your use case?

@YeungOnion
Copy link

It looks to me like the count_as_value example is what I need. I tried it in a few different cases, and it seems to work as expected.

So is your plan to document that values can be used, but that implementors of Augmentation should know that there will be logical errors if values are mutated in-place instead of with the insertion API?

@shuoli84
Copy link
Owner Author

Yup. I think the use case is common and the likelihood of both "get_mut and update" + "Augmentation based on Value" is low.

@YeungOnion
Copy link

I think so as well, and at least it would only be a logic error, similar to mutating key in-place in a collections::BTree*

@shuoli84 shuoli84 changed the title Examples to handle duplicate values Add values to Augmentation functions Nov 4, 2024
@shuoli84 shuoli84 marked this pull request as ready for review November 4, 2024 00:40
@shuoli84 shuoli84 merged commit 94beb4a into main Nov 4, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants