Skip to content

Includes new add method that uses .clone() for support. #11961

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

Merged
merged 1 commit into from
Feb 12, 2014

Conversation

niftynif
Copy link
Contributor

I implemented an add method for the btree in progress. It is intended to be refactored later using an alternative to .clone() that passes the borrow checker, but for now, it works as intended. r? @catamorphism


///An add method that uses the clone() feature for support.
pub fn add(mut self, k: K, v: V) -> BTree<K, V> {
let (a, b) = self.root.clone().add(k, v, self.upper_bound.clone());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do realise that this is cloning the whole tree, right? i.e. every insert is requiring duplicating everything, which completely destroys the O(log n) behaviour. I am personally very against merging something like this; would it be possible to do the work to avoid the clones now, before merging?

Also, almost all of these methods should be taking &self or &mut self to modify in place rather than consuming the tree, which will probably make it much easier to avoid .clones. (Compare to the various methods of extra::treemap, e.g. .insert.)

E.g. I'd expect signatures like:

impl<K: TotalOrd, V> BTree<K,V> {
    pub fn find<'a>(&'a self, k: &K) -> Option<&'a V> { ... }
    pub fn insert(&mut self, k: K, v: V) { ... }
}

(See the Map and MutableMap traits for the signatures & method names that are conventionally used.)

@niftynif
Copy link
Contributor Author

niftynif commented Feb 1, 2014

I understand where you're coming from, @huonw, and I'd like to add that this work you see here is not meant to represent a finished product. This would of course be revised extensively to hopefully get rid of every instance of .clone() in the insert method, but the goal of this PR is to add new functionality that hadn't yet existed for this data structure, and to get the algorithm for insertion "on paper." I originally began the implementation of the insert method with &mut self in the method signature, but this was unsuccessful in my first few attempts and @catamorphism suggested that the method should take self, rather than a reference to self.
I understand if you're not convinced by that explanation. Maybe @catamorphism can do a better job of explaining the goals and process behind this PR.

@brson
Copy link
Contributor

brson commented Feb 11, 2014

Needs rebase.

Added new tests for bsearch methods and changed "add" to "insert"

Fixed failure on div_floor.
bors added a commit that referenced this pull request Feb 12, 2014
I implemented an add method for the btree in progress.  It is intended to be refactored later using an alternative to .clone() that passes the borrow checker, but for now, it works as intended. r? @catamorphism
@bors bors merged commit 1843670 into rust-lang:master Feb 12, 2014
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.

5 participants