Skip to content

Commit

Permalink
Index with loop_get; tag 0.0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Feb 13, 2024
1 parent 3bf6b55 commit 04f6593
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[package]
name = "im_ternary_tree"
version = "0.0.14"
version = "0.0.15"
edition = "2021"
authors = ["jiyinyiyong <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ where
} else {
match self {
Empty => panic!("list is empty to index"),
Tree(t) => t.ref_get(idx),
Tree(t) => t.loop_get(idx),
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,34 +292,34 @@ where
/// get am element via drilling down the branch with a mutable loop,
/// supposed to be faster than `ref_get` since it's more like VM instructions
pub fn loop_get(&'a self, original_idx: usize) -> &'a T {
let tree_parent = Cell::new(self);
let mut tree_parent = self;
let mut idx = original_idx;
loop {
match tree_parent.get() {
match tree_parent {
Leaf(value) => {
return value;
}
Branch2 { left, middle, .. } => {
if idx < left.len() {
tree_parent.set(left);
tree_parent = left;
} else {
tree_parent.set(middle);
tree_parent = middle;
idx -= left.len();
}
}
Branch3 { left, middle, right, .. } => {
if idx < left.len() {
tree_parent.set(left);
tree_parent = left;
continue;
}
idx -= left.len();

if idx < middle.len() {
tree_parent.set(middle);
tree_parent = middle;
continue;
}

tree_parent.set(right);
tree_parent = right;
idx -= middle.len();
}
}
Expand Down

0 comments on commit 04f6593

Please sign in to comment.