Skip to content

Commit 04f6593

Browse files
committed
Index with loop_get; tag 0.0.15
1 parent 3bf6b55 commit 04f6593

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
[package]
33
name = "im_ternary_tree"
4-
version = "0.0.14"
4+
version = "0.0.15"
55
edition = "2021"
66
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
77
license = "MIT"

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ where
548548
} else {
549549
match self {
550550
Empty => panic!("list is empty to index"),
551-
Tree(t) => t.ref_get(idx),
551+
Tree(t) => t.loop_get(idx),
552552
}
553553
}
554554
}

src/tree.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,34 +292,34 @@ where
292292
/// get am element via drilling down the branch with a mutable loop,
293293
/// supposed to be faster than `ref_get` since it's more like VM instructions
294294
pub fn loop_get(&'a self, original_idx: usize) -> &'a T {
295-
let tree_parent = Cell::new(self);
295+
let mut tree_parent = self;
296296
let mut idx = original_idx;
297297
loop {
298-
match tree_parent.get() {
298+
match tree_parent {
299299
Leaf(value) => {
300300
return value;
301301
}
302302
Branch2 { left, middle, .. } => {
303303
if idx < left.len() {
304-
tree_parent.set(left);
304+
tree_parent = left;
305305
} else {
306-
tree_parent.set(middle);
306+
tree_parent = middle;
307307
idx -= left.len();
308308
}
309309
}
310310
Branch3 { left, middle, right, .. } => {
311311
if idx < left.len() {
312-
tree_parent.set(left);
312+
tree_parent = left;
313313
continue;
314314
}
315315
idx -= left.len();
316316

317317
if idx < middle.len() {
318-
tree_parent.set(middle);
318+
tree_parent = middle;
319319
continue;
320320
}
321321

322-
tree_parent.set(right);
322+
tree_parent = right;
323323
idx -= middle.len();
324324
}
325325
}

0 commit comments

Comments
 (0)