From 04f659385b2236f3b133f8a589de520217380de1 Mon Sep 17 00:00:00 2001 From: tiye Date: Wed, 14 Feb 2024 00:09:21 +0800 Subject: [PATCH] Index with loop_get; tag 0.0.15 --- Cargo.toml | 2 +- src/lib.rs | 2 +- src/tree.rs | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9c685da..203afc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "im_ternary_tree" -version = "0.0.14" +version = "0.0.15" edition = "2021" authors = ["jiyinyiyong "] license = "MIT" diff --git a/src/lib.rs b/src/lib.rs index 943a9ac..68052d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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), } } } diff --git a/src/tree.rs b/src/tree.rs index aab577d..d12e5ef 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -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(); } }