2022-09-01 10:47:48 +00:00
|
|
|
use self::{
|
|
|
|
cursor::SafeCursor,
|
|
|
|
node::{InternalNode, 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-08-12 09:54:47 +00:00
|
|
|
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-08-10 07:41:21 +00:00
|
|
|
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)>,
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:54:47 +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> {
|
2022-08-12 09:54:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-12 03:40:13 +00:00
|
|
|
impl<T: Rle + 'static, A: RleTreeTrait<T> + 'static> Default for RleTree<T, A> {
|
|
|
|
fn default() -> Self {
|
2022-08-12 09:54:47 +00:00
|
|
|
RleTreeBuilder {
|
|
|
|
bump: Bump::new(),
|
2022-08-12 10:22:05 +00:00
|
|
|
tree_builder: |bump| bump.alloc(RleTreeRaw::new(bump)),
|
2022-08-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
.build()
|
2022-08-12 03:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-08-12 16:56:17 +00:00
|
|
|
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) {
|
2022-08-12 05:49:19 +00:00
|
|
|
self.node
|
|
|
|
.as_internal_mut()
|
|
|
|
.unwrap()
|
|
|
|
.insert(index, value)
|
|
|
|
.unwrap();
|
2022-08-05 10:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// return a cursor to the tree
|
2022-09-01 10:47:48 +00:00
|
|
|
pub fn get<'b>(&'b self, mut index: A::Int) -> SafeCursor<'a, 'b, T, A> {
|
|
|
|
let mut node = &self.node;
|
|
|
|
loop {
|
|
|
|
match node {
|
|
|
|
Node::Internal(internal_node) => {
|
|
|
|
let (child_index, next, _) = A::find_pos_internal(internal_node, index);
|
|
|
|
node = internal_node.children[child_index];
|
|
|
|
index = next;
|
|
|
|
}
|
|
|
|
Node::Leaf(leaf) => {
|
|
|
|
let (child_index, _, _) = A::find_pos_leaf(leaf, index);
|
|
|
|
return SafeCursor::new(leaf.into(), child_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get_mut<'b>(&'b mut self, index: A::Int) -> SafeCursor<'a, 'b, T, A> {
|
|
|
|
self.get(index)
|
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>) {
|
|
|
|
self.node.as_internal_mut().unwrap().delete(start, end);
|
2022-08-05 10:47:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 11:09:07 +00:00
|
|
|
pub fn iter_range(&self, _from: A::Int, _to: A::Int) {
|
2022-08-05 10:47:51 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2022-08-12 12:46:38 +00:00
|
|
|
pub fn debug_check(&mut self) {
|
2022-08-11 10:04:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|