loro/crates/rle/src/rle_tree.rs

337 lines
11 KiB
Rust
Raw Normal View History

2022-10-11 08:50:22 +00:00
use std::{collections::HashMap, ptr::NonNull};
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-09 15:31:49 +00:00
pub use cursor::{SafeCursor, SafeCursorMut, UnsafeCursor};
2022-10-03 09:35:44 +00:00
use num::FromPrimitive;
use ouroboros::self_referencing;
2022-09-09 15:31:49 +00:00
pub use tree_trait::Position;
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
#[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-10-09 08:54:34 +00:00
node: &'this mut Node<'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-10-09 08:54:34 +00:00
node_builder: |bump| bump.alloc(Node::Internal(InternalNode::new(bump, None))),
}
.build()
}
}
2022-10-09 08:54:34 +00:00
impl<T: Rle, A: RleTreeTrait<T>> RleTree<T, A> {
2022-08-09 13:25:24 +00:00
#[inline]
pub fn insert(&mut self, index: A::Int, value: T) {
2022-10-09 08:54:34 +00:00
self.with_node_mut(|node| {
node.as_internal_mut()
.unwrap()
.insert(index, value, &mut |_a, _b| {})
.unwrap();
})
2022-09-06 14:22:54 +00:00
}
/// `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>),
{
2022-10-09 08:54:34 +00:00
self.with_node_mut(|node| {
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-10-09 12:23:37 +00:00
pub fn get(&self, mut index: A::Int) -> Option<SafeCursor<'_, T, A>> {
2022-10-09 08:54:34 +00:00
self.with_node(|mut node| {
loop {
match node {
Node::Internal(internal_node) => {
let result = A::find_pos_internal(internal_node, index);
if !result.found {
return None;
}
node = &internal_node.children[result.child_index];
index = result.offset;
2022-09-06 16:14:35 +00:00
}
2022-10-09 08:54:34 +00:00
Node::Leaf(leaf) => {
let result = A::find_pos_leaf(leaf, index);
if !result.found {
return None;
}
// SAFETY: result is valid
return Some(unsafe {
std::mem::transmute(SafeCursor::new(
leaf.into(),
result.child_index,
result.offset,
result.pos,
0,
))
});
2022-09-06 16:14:35 +00:00
}
}
}
2022-10-09 08:54:34 +00:00
})
2022-09-06 16:14:35 +00:00
}
/// return the first valid cursor after the given index
2022-10-10 09:36:55 +00:00
/// reviewed by @Leeeon233
2022-09-06 16:14:35 +00:00
#[inline]
2022-10-09 12:23:37 +00:00
fn get_cursor_ge(&self, mut index: A::Int) -> Option<SafeCursor<'_, T, A>> {
2022-10-09 08:54:34 +00:00
self.with_node(|mut 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;
}
node = &internal_node.children[result.child_index];
index = result.offset;
2022-09-06 16:14:35 +00:00
}
2022-10-09 08:54:34 +00:00
Node::Leaf(leaf) => {
let result = A::find_pos_leaf(leaf, index);
if result.child_index >= leaf.children.len() {
return None;
}
// SAFETY: result is valid
return Some(unsafe {
std::mem::transmute(SafeCursor::new(
leaf.into(),
result.child_index,
result.offset,
result.pos,
0,
))
});
2022-09-06 16:14:35 +00:00
}
2022-09-01 10:47:48 +00:00
}
}
2022-10-09 08:54:34 +00:00
})
2022-09-01 10:47:48 +00:00
}
#[inline]
2022-10-09 12:23:37 +00:00
pub fn get_mut(&mut self, index: A::Int) -> Option<SafeCursorMut<'_, 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-10-03 09:35:44 +00:00
#[inline]
2022-10-09 12:23:37 +00:00
pub fn iter(&self) -> iter::Iter<'_, T, A> {
2022-10-09 08:54:34 +00:00
// SAFETY: the cursor and iter cannot outlive self
self.with_node(|node| unsafe {
2022-10-09 12:23:37 +00:00
iter::Iter::new(std::mem::transmute(node.get_first_leaf()))
2022-10-09 08:54:34 +00:00
})
2022-08-05 10:47:51 +00:00
}
2022-10-03 09:35:44 +00:00
#[inline]
2022-10-09 12:23:37 +00:00
pub fn iter_mut(&mut self) -> iter::IterMut<'_, T, A> {
2022-10-09 08:54:34 +00:00
// SAFETY: the cursor and iter cannot outlive self
self.with_node_mut(|node| unsafe {
2022-10-09 12:23:37 +00:00
iter::IterMut::new(std::mem::transmute(node.get_first_leaf_mut()))
2022-10-09 08:54:34 +00:00
})
2022-10-03 09:35:44 +00:00
}
#[inline]
pub fn empty(&self) -> bool {
self.len() == A::Int::from_usize(0).unwrap()
}
2022-10-09 08:54:34 +00:00
pub fn iter_mut_in(
&mut self,
2022-10-09 12:23:37 +00:00
start: Option<SafeCursor<'_, T, A>>,
end: Option<SafeCursor<'_, T, A>>,
) -> iter::IterMut<'_, T, A> {
2022-10-03 09:35:44 +00:00
if self.empty() || (start.is_none() && end.is_none()) {
self.iter_mut()
} else {
2022-10-09 08:54:34 +00:00
// SAFETY: the cursor cannot outlive self, so we are safe here
self.with_node_mut(|node| unsafe {
let leaf = node.get_first_leaf().unwrap().into();
// SAFETY: this is safe because we know there are at least one element in the tree
let start = start.unwrap_or_else(|| {
std::mem::transmute(SafeCursor::new(leaf, 0, 0, Position::Start, 0))
});
2022-10-09 12:23:37 +00:00
let start: SafeCursorMut<'_, T, A> = SafeCursorMut(start.0);
std::mem::transmute::<_, iter::IterMut<'_, T, A>>(iter::IterMut::from_cursor(
std::mem::transmute::<_, SafeCursorMut<'_, T, A>>(start),
end,
2022-10-09 08:54:34 +00:00
))
})
2022-10-03 09:35:44 +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-10-09 08:54:34 +00:00
self.with_node_mut(|node| {
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>),
{
2022-10-09 08:54:34 +00:00
self.with_node_mut(|node| {
node.as_internal_mut().unwrap().delete(start, end, notify);
})
2022-09-06 14:22:54 +00:00
}
2022-10-10 09:36:55 +00:00
/// reviewed by @Leeeon233
2022-10-09 12:23:37 +00:00
pub fn iter_range(&self, start: A::Int, end: Option<A::Int>) -> iter::Iter<'_, 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-10-11 08:50:22 +00:00
pub fn update_at_cursors<U, F>(
&mut self,
cursors: Vec<UnsafeCursor<T, A>>,
update_fn: &mut U,
notify: &mut F,
) where
U: FnMut(&mut T),
F: FnMut(&T, *mut LeafNode<T, A>),
{
let mut updates_map: HashMap<NonNull<_>, Vec<(usize, Vec<T>)>> = Default::default();
for cursor in cursors {
// SAFETY: we has the exclusive reference to the tree and the cursor is valid
let updates = unsafe {
cursor
.leaf
.as_ref()
.pure_update(cursor.index, cursor.offset, cursor.len, update_fn)
};
if let Some(update) = updates {
updates_map
.entry(cursor.leaf)
.or_default()
.push((cursor.index, update));
}
}
2022-10-11 10:18:14 +00:00
let mut internal_updates_map: HashMap<NonNull<_>, Vec<(usize, Vec<_>)>> =
Default::default();
2022-10-11 08:50:22 +00:00
for (mut leaf, updates) in updates_map {
// SAFETY: we has the exclusive reference to the tree and the cursor is valid
let leaf = unsafe { leaf.as_mut() };
if let Err(new) = leaf.apply_updates(updates, notify) {
internal_updates_map
.entry(leaf.parent)
.or_default()
.push((leaf.get_index_in_parent().unwrap(), new));
} else {
// insert empty value to trigger cache update
internal_updates_map.insert(leaf.parent, Default::default());
}
}
while !internal_updates_map.is_empty() {
let updates_map = std::mem::take(&mut internal_updates_map);
for (mut node, updates) in updates_map {
// SAFETY: we has the exclusive reference to the tree and the cursor is valid
let node = unsafe { node.as_mut() };
if updates.is_empty() {
A::update_cache_internal(node);
continue;
}
if let Err(new) = node.apply_updates(updates) {
internal_updates_map
.entry(node.parent.unwrap())
.or_default()
.push((node.get_index_in_parent().unwrap(), new));
} else {
// insert empty value to trigger cache update
internal_updates_map.insert(node.parent.unwrap(), Default::default());
}
}
}
2022-10-11 10:18:14 +00:00
#[cfg(test)]
{
self.debug_check();
}
2022-10-11 08:50:22 +00:00
}
pub fn iter_update<U, F>(
&mut self,
start: A::Int,
end: Option<A::Int>,
update_fn: &mut U,
notify: &mut F,
) where
U: FnMut(&mut T),
F: FnMut(&T, *mut LeafNode<'_, T, A>),
{
let mut cursors = Vec::new();
for cursor in self.iter_range(start, end) {
cursors.push(cursor.0);
}
// SAFETY: it's perfectly safe here because we know what we are doing in the update_at_cursors
self.update_at_cursors(unsafe { std::mem::transmute(cursors) }, update_fn, notify);
}
2022-08-12 12:46:38 +00:00
pub fn debug_check(&mut self) {
2022-10-09 08:54:34 +00:00
self.with_node_mut(|node| {
node.as_internal_mut().unwrap().check();
})
2022-08-05 10:47:51 +00:00
}
2022-10-10 13:47:57 +00:00
// pub fn iter_cursor_mut(&mut self) -> impl Iterator<Item = SafeCursorMut<'_, T, A>> {}
2022-08-05 10:47:51 +00:00
}
2022-08-05 12:04:49 +00:00
2022-10-09 08:54:34 +00:00
impl<T: Rle, A: RleTreeTrait<T>> RleTree<T, A> {
2022-09-01 13:32:32 +00:00
#[inline]
pub fn len(&self) -> A::Int {
2022-10-09 08:54:34 +00:00
self.with_node(|node| node.len())
2022-08-05 12:04:49 +00:00
}
}