loro/crates/rle/src/rle_tree.rs

191 lines
5.4 KiB
Rust
Raw Normal View History

2022-09-06 14:22:54 +00:00
use self::node::{InternalNode, LeafNode, Node};
2022-09-01 17:14:39 +00:00
use crate::Rle;
2022-08-05 10:47:51 +00:00
pub(self) use bumpalo::collections::vec::Vec as BumpVec;
2022-08-10 10:42:36 +00:00
use bumpalo::Bump;
2022-09-02 10:41:59 +00:00
pub use cursor::{SafeCursor, SafeCursorMut};
use ouroboros::self_referencing;
2022-09-01 13:32:32 +00:00
use std::marker::{PhantomData, PhantomPinned};
2022-08-05 10:47:51 +00:00
use tree_trait::RleTreeTrait;
2022-08-12 12:46:38 +00:00
2022-09-01 10:47:48 +00:00
mod cursor;
2022-09-06 14:22:54 +00:00
pub mod iter;
2022-08-12 12:46:38 +00:00
pub mod node;
2022-08-10 07:41:21 +00:00
#[cfg(test)]
mod test;
2022-08-12 12:46:38 +00:00
pub mod tree_trait;
2022-08-05 10:47:51 +00:00
#[derive(Debug)]
2022-08-05 12:04:49 +00:00
pub struct RleTreeRaw<'a, T: Rle, A: RleTreeTrait<T>> {
2022-08-05 10:47:51 +00:00
node: Node<'a, T, A>,
_pin: PhantomPinned,
_a: PhantomData<(A, T)>,
}
#[self_referencing]
2022-09-01 16:59:02 +00:00
#[derive(Debug)]
2022-08-09 13:25:24 +00:00
pub struct RleTree<T: Rle + 'static, A: RleTreeTrait<T> + 'static> {
bump: Bump,
#[borrows(bump)]
2022-08-12 12:46:38 +00:00
pub tree: &'this mut RleTreeRaw<'this, T, A>,
2022-08-05 12:04:49 +00:00
}
impl<T: Rle + 'static, A: RleTreeTrait<T> + 'static> Default for RleTree<T, A> {
fn default() -> Self {
RleTreeBuilder {
bump: Bump::new(),
2022-08-12 10:22:05 +00:00
tree_builder: |bump| bump.alloc(RleTreeRaw::new(bump)),
}
.build()
}
}
2022-08-05 12:04:49 +00:00
impl<'a, T: Rle, A: RleTreeTrait<T>> RleTreeRaw<'a, T, A> {
#[inline]
fn new(bump: &'a Bump) -> Self {
2022-08-05 10:47:51 +00:00
Self {
node: Node::Internal(InternalNode::new(bump, None)),
2022-08-05 10:47:51 +00:00
_pin: PhantomPinned,
_a: PhantomData,
}
}
2022-08-09 13:25:24 +00:00
#[inline]
pub fn insert(&mut self, index: A::Int, value: T) {
self.node
.as_internal_mut()
.unwrap()
2022-09-06 14:22:54 +00:00
.insert(index, value, &mut |_a, _b| {})
.unwrap();
}
/// `notify` would be invoke if a new element is inserted/moved to a new leaf node.
#[inline]
pub fn insert_notify<F>(&mut self, index: A::Int, value: T, notify: &mut F)
where
F: FnMut(&T, *mut LeafNode<'_, T, A>),
{
self.node
.as_internal_mut()
.unwrap()
.insert(index, value, notify)
.unwrap();
2022-08-05 10:47:51 +00:00
}
2022-09-06 16:14:35 +00:00
/// return a cursor at the given index
2022-09-02 10:41:59 +00:00
#[inline]
2022-09-06 16:14:35 +00:00
pub fn get<'b>(&'b self, mut index: A::Int) -> Option<SafeCursor<'a, 'b, T, A>> {
2022-09-01 10:47:48 +00:00
let mut node = &self.node;
loop {
match node {
Node::Internal(internal_node) => {
2022-09-06 14:33:18 +00:00
let result = A::find_pos_internal(internal_node, index);
2022-09-06 16:14:35 +00:00
if !result.found {
return None;
}
node = internal_node.children[result.child_index];
index = result.offset;
}
Node::Leaf(leaf) => {
let result = A::find_pos_leaf(leaf, index);
if !result.found {
return None;
}
2022-09-07 10:43:21 +00:00
return Some(SafeCursor::new(leaf.into(), result.child_index, result.pos));
2022-09-06 16:14:35 +00:00
}
}
}
}
/// return the first valid cursor after the given index
#[inline]
2022-09-07 10:43:21 +00:00
fn get_cursor_ge<'b>(&'b self, mut index: A::Int) -> Option<SafeCursor<'a, 'b, T, A>> {
2022-09-06 16:14:35 +00:00
let mut node = &self.node;
loop {
match node {
Node::Internal(internal_node) => {
let result = A::find_pos_internal(internal_node, index);
if result.child_index >= internal_node.children.len() {
return None;
}
2022-09-06 14:33:18 +00:00
node = internal_node.children[result.child_index];
2022-09-06 14:41:50 +00:00
index = result.offset;
2022-09-01 10:47:48 +00:00
}
Node::Leaf(leaf) => {
2022-09-06 16:14:35 +00:00
let result = A::find_pos_leaf(leaf, index);
if result.child_index >= leaf.children.len() {
return None;
}
2022-09-07 10:43:21 +00:00
return Some(SafeCursor::new(leaf.into(), result.child_index, result.pos));
2022-09-01 10:47:48 +00:00
}
}
}
}
#[inline]
2022-09-06 16:14:35 +00:00
pub fn get_mut<'b>(&'b mut self, index: A::Int) -> Option<SafeCursorMut<'a, 'b, T, A>> {
2022-09-02 10:41:59 +00:00
let cursor = self.get(index);
2022-09-06 16:14:35 +00:00
cursor.map(|x| SafeCursorMut(x.0))
2022-08-05 10:47:51 +00:00
}
2022-08-10 07:41:21 +00:00
pub fn iter(&self) -> iter::Iter<'_, 'a, T, A> {
iter::Iter::new(self.node.get_first_leaf())
2022-08-05 10:47:51 +00:00
}
2022-08-11 12:12:47 +00:00
pub fn delete_range(&mut self, start: Option<A::Int>, end: Option<A::Int>) {
2022-09-06 14:22:54 +00:00
self.node
.as_internal_mut()
.unwrap()
.delete(start, end, &mut |_, _| {});
2022-08-05 10:47:51 +00:00
}
2022-09-06 14:22:54 +00:00
pub fn delete_range_notify<F>(
&mut self,
start: Option<A::Int>,
end: Option<A::Int>,
notify: &mut F,
) where
F: FnMut(&T, *mut LeafNode<'_, T, A>),
{
self.node
.as_internal_mut()
.unwrap()
.delete(start, end, notify);
}
pub fn iter_range(&self, start: A::Int, end: Option<A::Int>) -> iter::Iter<'_, 'a, T, A> {
2022-09-07 10:43:21 +00:00
let cursor_from = self.get_cursor_ge(start);
2022-09-06 16:14:35 +00:00
if cursor_from.is_none() {
return iter::Iter::new(None);
}
let cursor_from = cursor_from.unwrap();
2022-09-07 10:43:21 +00:00
if let Some(ans) = {
if let Some(end) = end {
let cursor_to = self.get_cursor_ge(end);
iter::Iter::from_cursor(cursor_from, cursor_to)
2022-09-06 16:14:35 +00:00
} else {
2022-09-07 10:43:21 +00:00
None
2022-09-06 14:22:54 +00:00
}
2022-09-07 10:43:21 +00:00
} {
ans
} else {
iter::Iter::from_cursor(cursor_from, None).unwrap()
2022-09-06 14:22:54 +00:00
}
2022-08-05 10:47:51 +00:00
}
2022-08-12 12:46:38 +00:00
pub fn debug_check(&mut self) {
self.node.as_internal_mut().unwrap().check();
2022-08-05 10:47:51 +00:00
}
}
2022-08-05 12:04:49 +00:00
2022-09-01 13:32:32 +00:00
impl<'a, T: Rle, A: RleTreeTrait<T>> RleTreeRaw<'a, T, A> {
#[inline]
pub fn len(&self) -> A::Int {
2022-08-10 07:41:21 +00:00
self.node.len()
2022-08-05 12:04:49 +00:00
}
}