4
4
5
5
mod finger;
6
6
7
- use std:: cell:: RefCell ;
7
+ use std:: cell:: Cell ;
8
8
use std:: cmp:: Ordering ;
9
9
use std:: fmt;
10
10
use std:: fmt:: { Debug , Display } ;
@@ -213,7 +213,7 @@ where
213
213
/// internal usages for rebuilding tree
214
214
fn to_leaves ( & self ) -> Vec < TernaryTree < T > > {
215
215
let mut acc: Vec < TernaryTree < T > > = Vec :: with_capacity ( self . len ( ) ) ;
216
- let counter: RefCell < usize > = RefCell :: new ( 0 ) ;
216
+ let counter: Cell < usize > = Cell :: new ( 0 ) ;
217
217
write_leaves ( self , & mut acc, & counter) ;
218
218
assert_eq ! ( acc. len( ) , self . len( ) ) ;
219
219
acc
@@ -254,35 +254,34 @@ where
254
254
/// get am element via drilling down the branch with a mutable loop,
255
255
/// supposed to be faster than `ref_get` since it's more like VM instructions
256
256
pub fn loop_get ( & ' a self , original_idx : usize ) -> & ' a T {
257
- let r = RefCell :: new ( self ) ;
258
- let mut tree_parent = r. borrow_mut ( ) ;
257
+ let tree_parent = Cell :: new ( self ) ;
259
258
let mut idx = original_idx;
260
259
loop {
261
- match * tree_parent {
260
+ match tree_parent. get ( ) {
262
261
Leaf ( value) => {
263
- return value;
262
+ return & * value;
264
263
}
265
264
Branch2 { left, middle, .. } => {
266
265
if idx < left. len ( ) {
267
- * tree_parent = left;
266
+ tree_parent. set ( left) ;
268
267
} else {
269
- * tree_parent = middle;
268
+ tree_parent. set ( middle) ;
270
269
idx -= left. len ( ) ;
271
270
}
272
271
}
273
272
Branch3 { left, middle, right, .. } => {
274
273
if idx < left. len ( ) {
275
- * tree_parent = left;
274
+ tree_parent. set ( left) ;
276
275
continue ;
277
276
}
278
277
idx -= left. len ( ) ;
279
278
280
279
if idx < middle. len ( ) {
281
- * tree_parent = middle;
280
+ tree_parent. set ( middle) ;
282
281
continue ;
283
282
}
284
283
285
- * tree_parent = right;
284
+ tree_parent. set ( right) ;
286
285
idx -= middle. len ( ) ;
287
286
}
288
287
}
@@ -976,7 +975,7 @@ where
976
975
fn next ( & mut self ) -> Option < Self :: Item > {
977
976
if self . index < self . value . len ( ) {
978
977
// println!("get: {} {}", self.value.format_inline(), self.index);
979
- let ret = self . value . loop_get ( self . index ) ;
978
+ let ret = self . value . ref_get ( self . index ) ;
980
979
self . index += 1 ;
981
980
Some ( ret)
982
981
} else {
@@ -992,7 +991,7 @@ impl<T: Clone + Display + Eq + PartialEq + Debug + Ord + PartialOrd + Hash> Part
992
991
}
993
992
994
993
for idx in 0 ..ys. len ( ) {
995
- if self . loop_get ( idx) != ys. loop_get ( idx) {
994
+ if self . ref_get ( idx) != ys. ref_get ( idx) {
996
995
return false ;
997
996
}
998
997
}
@@ -1019,7 +1018,7 @@ where
1019
1018
fn cmp ( & self , other : & Self ) -> Ordering {
1020
1019
if self . len ( ) == other. len ( ) {
1021
1020
for idx in 0 ..self . len ( ) {
1022
- match self . loop_get ( idx) . cmp ( other. loop_get ( idx) ) {
1021
+ match self . ref_get ( idx) . cmp ( other. ref_get ( idx) ) {
1023
1022
Ordering :: Equal => { }
1024
1023
a => return a,
1025
1024
}
@@ -1040,7 +1039,7 @@ where
1040
1039
1041
1040
fn index < ' b > ( & self , idx : usize ) -> & Self :: Output {
1042
1041
// println!("get: {} {}", self.format_inline(), idx);
1043
- self . loop_get ( idx)
1042
+ self . ref_get ( idx)
1044
1043
}
1045
1044
}
1046
1045
@@ -1067,7 +1066,7 @@ where
1067
1066
}
1068
1067
1069
1068
/// internal function for mutable writing
1070
- fn write_leaves < T > ( xs : & TernaryTree < T > , acc : & mut Vec < TernaryTree < T > > , counter : & RefCell < usize > )
1069
+ fn write_leaves < T > ( xs : & TernaryTree < T > , acc : & mut Vec < TernaryTree < T > > , counter : & Cell < usize > )
1071
1070
where
1072
1071
T : Clone + Display + Eq + PartialEq + Debug + Ord + PartialOrd + Hash ,
1073
1072
{
0 commit comments