feat: init

This commit is contained in:
Zixuan Chen 2022-08-05 18:47:51 +08:00
parent 7704ce2939
commit 9ecd0417bd
8 changed files with 153 additions and 0 deletions

View file

@ -6,4 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bumpalo = { version = "3.10.0", features = ["collections"] }
num = "0.4.0"
enum-as-inner = { path = "../enum-as-inner-fork" }
owning_ref = "0.4.1"

View file

@ -19,6 +19,7 @@
//! - slice(from, to) returns a slice of atom elements from the index from to the index to.
//!
mod rle_trait;
mod rle_tree;
mod rle_vec;
pub use crate::rle_trait::{HasLength, Mergable, Rle, Slice, Sliceable};
pub use crate::rle_vec::{RleVec, SearchResult, SliceIterator};

View file

@ -1,3 +1,5 @@
use num::{FromPrimitive, Integer, Num, NumCast};
pub trait Mergable<Cfg = ()> {
fn is_mergable(&self, _other: &Self, _conf: &Cfg) -> bool
where

View file

@ -0,0 +1,56 @@
pub(self) use bumpalo::collections::vec::Vec as BumpVec;
use std::marker::{PhantomData, PhantomPinned};
use crate::Rle;
use bumpalo::Bump;
use tree_trait::RleTreeTrait;
use self::node::{InternalNode, Node};
mod node;
mod tree_trait;
#[derive(Debug)]
pub struct RleTree<'a, T: Rle, A: RleTreeTrait<T>> {
bump: &'a Bump,
node: Node<'a, T, A>,
_pin: PhantomPinned,
_a: PhantomData<(A, T)>,
}
impl<'a, T: Rle, A: RleTreeTrait<T>> RleTree<'a, T, A> {
pub fn new(bump: &'a Bump) -> Self {
Self {
bump,
node: Node::Internal(InternalNode::new(bump)),
_pin: PhantomPinned,
_a: PhantomData,
}
}
fn insert(&mut self, index: A::Int, value: T) {
self.node.insert(index, value);
}
/// return a cursor to the tree
fn get(&self, index: A::Int) {
todo!()
}
fn iter(&self) {
todo!()
}
fn delete_range(&mut self, from: A::Int, to: A::Int) {
todo!()
}
fn iter_range(&self, from: A::Int, to: A::Int) {
todo!()
}
#[cfg(test)]
fn debug_check(&self) {
todo!()
}
}

View file

@ -0,0 +1,44 @@
use std::marker::{PhantomData, PhantomPinned};
use crate::Rle;
use super::{tree_trait::RleTreeTrait, BumpVec, RleTree};
use bumpalo::Bump;
use enum_as_inner::EnumAsInner;
mod internal_impl;
mod leaf_impl;
#[derive(Debug, EnumAsInner)]
pub enum Node<'a, T: Rle, A: RleTreeTrait<T>> {
Internal(InternalNode<'a, T, A>),
Leaf(LeafNode<'a, T, A>),
}
#[derive(Debug)]
pub struct InternalNode<'a, T: Rle, A: RleTreeTrait<T>> {
bump: &'a Bump,
parent: Option<&'a InternalNode<'a, T, A>>,
children: BumpVec<'a, Node<'a, T, A>>,
_pin: PhantomPinned,
_a: PhantomData<A>,
}
#[derive(Debug)]
pub struct LeafNode<'a, T: Rle, A: RleTreeTrait<T>> {
bump: &'a Bump,
parent: &'a InternalNode<'a, T, A>,
children: BumpVec<'a, T>,
prev: Option<&'a LeafNode<'a, T, A>>,
next: Option<&'a LeafNode<'a, T, A>>,
_pin: PhantomPinned,
_a: PhantomData<A>,
}
impl<'a, T: Rle, A: RleTreeTrait<T>> Node<'a, T, A> {
pub(super) fn insert(&mut self, index: A::Int, value: T) {
match self {
Node::Internal(node) => {}
Node::Leaf(node) => {}
}
}
}

View file

@ -0,0 +1,13 @@
use super::*;
impl<'a, T: Rle, A: RleTreeTrait<T>> InternalNode<'a, T, A> {
pub fn new(bump: &'a Bump) -> Self {
Self {
bump,
parent: None,
children: BumpVec::with_capacity_in(A::max_children(), bump),
_pin: PhantomPinned,
_a: PhantomData,
}
}
}

View file

@ -0,0 +1,15 @@
use super::*;
impl<'a, T: Rle, A: RleTreeTrait<T>> LeafNode<'a, T, A> {
pub fn new(bump: &'a Bump, parent: &'a InternalNode<'a, T, A>) -> Self {
Self {
bump,
parent,
children: BumpVec::with_capacity_in(A::max_children(), bump),
prev: None,
next: None,
_pin: PhantomPinned,
_a: PhantomData,
}
}
}

View file

@ -0,0 +1,19 @@
use crate::Rle;
use super::node::{InternalNode, Node};
pub trait RleTreeTrait<T: Rle>: Sized {
type Int: num::Integer;
type InternalCache;
fn update_cache();
fn min_children() -> usize;
#[inline]
fn max_children() -> usize {
Self::min_children() * 2
}
fn before_insert_internal(node: InternalNode<'_, T, Self>);
fn find_insert_pos_internal(node: InternalNode<'_, T, Self>, index: Self::Int) -> usize;
}