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

If vectors are concatenated, tree must be leftwise dense if balanced #18

Merged
merged 1 commit into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
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
41 changes: 29 additions & 12 deletions src/pvec/rrbtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ use std::cmp;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::mem;

#[cfg(feature = "arc")]
use std::sync::Arc;

#[cfg(not(feature = "arc"))]
use std::rc::Rc;
#[cfg(feature = "arc")]
use std::sync::Arc;

#[cfg(feature = "arc")]
type SharedPtr<K> = Arc<K>;
Expand Down Expand Up @@ -192,25 +190,43 @@ impl<T: Clone + Debug> Leaf<T> {
let mut index_l = leaf_l.len;
let mut index_r = 0;

while index_l < BRANCH_FACTOR && index_r < that.len {
let that_len = that.len;

while index_l < BRANCH_FACTOR && index_r < that_len {
leaf_l.add(that.take(index_r));

index_l += 1;
index_r += 1;
}

for _ in 0..that.len {
leaf_r.add(that.take(index_r));
let that_len = that.len;

for _ in 0..that_len {
leaf_r.add(that.take(index_r));
index_r += 1;
}

let mut children = new_branch!();
if leaf_l.is_full() && leaf_r.is_full() {
let mut children = new_branch!();
children[0] = Some(Node::Leaf(SharedPtr::new(leaf_l)));
children[1] = Some(Node::Leaf(SharedPtr::new(leaf_r)));

children[0] = Some(Node::Leaf(SharedPtr::new(leaf_l)));
children[1] = Some(Node::Leaf(SharedPtr::new(leaf_r)));
Node::Branch(SharedPtr::new(Branch { children, len: 2 }))
} else {
let mut sizes = new_branch!();
sizes[0] = Some(leaf_l.len);
sizes[1] = Some(leaf_l.len + leaf_r.len);

Node::Branch(SharedPtr::new(Branch { children, len: 2 }))
let mut children = new_branch!();
children[0] = Some(Node::Leaf(SharedPtr::new(leaf_l)));
children[1] = Some(Node::Leaf(SharedPtr::new(leaf_r)));

Node::RelaxedBranch(SharedPtr::new(RelaxedBranch {
children,
sizes,
len: 2,
}))
}
}

#[inline(always)]
Expand Down Expand Up @@ -1125,11 +1141,12 @@ mod serializer {
extern crate serde;
extern crate serde_json;

use self::serde::ser::{Serialize, SerializeSeq, SerializeStruct, Serializer};
use super::SharedPtr;
use super::BRANCH_FACTOR;
use super::{Branch, Leaf, Node, RelaxedBranch, RrbTree};

use self::serde::ser::{Serialize, SerializeSeq, SerializeStruct, Serializer};

impl<T> RelaxedBranch<T>
where
T: Serialize,
Expand Down
23 changes: 23 additions & 0 deletions tests/pvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,29 @@ fn interleaving_different_operations_must_maintain_correct_internal_state_for_va
interleaving_different_operations_must_maintain_correct_internal_state(33);
}

#[test]
fn interleaving_push_and_append_operations_must_maintain_correct_internal_state_for_var_sizes_32() {
let mut vec_one = PVec::new();

for i in 0..32 {
vec_one.push(i);
}

let mut vec_two = PVec::new();

for i in 0..1024 {
if i % 2 == 0 {
vec_two.push(i);
} else {
vec_two.append(&mut vec_one.clone());
}

for k in 0..vec_two.len() {
vec_two.get(k).unwrap();
}
}
}

#[test]
fn zero_sized_values() {
let mut v = PVec::new();
Expand Down