diff --git a/crates/rle/src/range_map.rs b/crates/rle/src/range_map.rs index 3f2b29f6..314c62d2 100644 --- a/crates/rle/src/range_map.rs +++ b/crates/rle/src/range_map.rs @@ -2,23 +2,23 @@ use std::fmt::Debug; use crate::{ rle_trait::ZeroElement, - rle_tree::tree_trait::{GlobalIndex, GlobalTreeTrait, HasGlobalIndex}, + rle_tree::tree_trait::{GlobalIndex, GlobalTreeTrait, HasIndex}, HasLength, Mergable, Rle, RleTree, Sliceable, }; #[derive(Debug, Clone)] -pub(crate) struct WithGlobalIndex { +pub(crate) struct WithIndex { pub(crate) value: Value, pub(crate) index: Index, } -impl HasLength for WithGlobalIndex { +impl HasLength for WithIndex { fn len(&self) -> usize { self.value.len() } } -impl Sliceable for WithGlobalIndex { +impl Sliceable for WithIndex { fn slice(&self, from: usize, to: usize) -> Self { Self { value: self.value.slice(from, to), @@ -27,7 +27,7 @@ impl Sliceable for WithGlobalIndex } } -impl Mergable for WithGlobalIndex { +impl Mergable for WithIndex { fn is_mergable(&self, other: &Self, conf: &()) -> bool { self.value.is_mergable(&other.value, conf) && self.index + Index::from_usize(self.value.len()).unwrap() == other.index @@ -38,10 +38,10 @@ impl Mergable for WithGlobalIndex } } -impl HasGlobalIndex for WithGlobalIndex { +impl HasIndex for WithIndex { type Int = Index; - fn get_global_start(&self) -> Self::Int { + fn get_start_index(&self) -> Self::Int { self.index } } @@ -49,8 +49,7 @@ impl HasGlobalIndex for WithGlobalIndex { - pub(crate) tree: - RleTree, GlobalTreeTrait, 10>>, + pub(crate) tree: RleTree, GlobalTreeTrait, 10>>, } impl Default @@ -71,7 +70,7 @@ impl RangeMap< ); self.tree.insert( start, - WithGlobalIndex { + WithIndex { value, index: start, }, diff --git a/crates/rle/src/rle_tree/tree_trait.rs b/crates/rle/src/rle_tree/tree_trait.rs index 84a80a77..2f1a11a6 100644 --- a/crates/rle/src/rle_tree/tree_trait.rs +++ b/crates/rle/src/rle_tree/tree_trait.rs @@ -246,13 +246,13 @@ pub trait GlobalIndex: impl> GlobalIndex for T {} -pub trait HasGlobalIndex: HasLength { +pub trait HasIndex: HasLength { type Int: GlobalIndex; - fn get_global_start(&self) -> Self::Int; + fn get_start_index(&self) -> Self::Int; #[inline] - fn get_global_end(&self) -> Self::Int { - self.get_global_start() + Self::Int::from_usize(self.len()).unwrap() + fn get_end_index(&self) -> Self::Int { + self.get_start_index() + Self::Int::from_usize(self.len()).unwrap() } } @@ -263,7 +263,7 @@ pub struct Cache { } #[inline] -fn get_cache( +fn get_cache( node: &Node<'_, T, GlobalTreeTrait>, ) -> Cache { match node { @@ -272,9 +272,7 @@ fn get_cache( } } -impl RleTreeTrait - for GlobalTreeTrait -{ +impl RleTreeTrait for GlobalTreeTrait { const MAX_CHILDREN_NUM: usize = MAX_CHILD; const MIN_CHILDREN_NUM: usize = Self::MAX_CHILDREN_NUM / 2; @@ -294,10 +292,10 @@ impl RleTreeTrait node.cache.end = node .children() .iter() - .map(|x| x.get_global_end()) + .map(|x| x.get_end_index()) .max() .unwrap(); - node.cache.start = node.children()[0].get_global_start(); + node.cache.start = node.children()[0].get_start_index(); } fn update_cache_internal(node: &mut InternalNode<'_, T, Self>) { @@ -347,8 +345,8 @@ impl RleTreeTrait fn find_pos_leaf(node: &LeafNode<'_, T, Self>, index: Self::Int) -> FindPosResult { for (i, child) in node.children().iter().enumerate() { let cache = Cache { - start: child.get_global_start(), - end: child.get_global_end(), + start: child.get_start_index(), + end: child.get_end_index(), }; if index <= cache.end { @@ -359,7 +357,7 @@ impl RleTreeTrait // prefer Start than End if index == cache.end && i + 1 < node.children.len() - && index == node.children[i + 1].get_global_start() + && index == node.children[i + 1].get_start_index() { return FindPosResult::new(i + 1, 0, Position::Start); } @@ -392,11 +390,11 @@ impl RleTreeTrait node.cache.end, node.children() .iter() - .map(|x| x.get_global_end()) + .map(|x| x.get_end_index()) .max() .unwrap() ); - assert_eq!(node.cache.start, node.children()[0].get_global_start()); + assert_eq!(node.cache.start, node.children()[0].get_start_index()); } fn check_cache_internal(node: &InternalNode<'_, T, Self>) { @@ -412,7 +410,7 @@ impl RleTreeTrait } fn get_index(node: &LeafNode<'_, T, Self>, child_index: usize) -> Self::Int { - node.children[child_index].get_global_start() + node.children[child_index].get_start_index() } }