feat: add withstartend

This commit is contained in:
Zixuan Chen 2022-09-07 18:57:41 +08:00
parent 6209de97a3
commit 8f005180a4
4 changed files with 47 additions and 41 deletions

View file

@ -104,6 +104,44 @@ impl<Index: GlobalIndex + 'static, Value: Rle + 'static> RangeMap<Index, Value>
}
}
pub struct WithStartEnd<Index: GlobalIndex, T> {
start: Index,
end: Index,
value: T,
}
impl<Index: GlobalIndex, T: Clone> Sliceable for WithStartEnd<Index, T> {
fn slice(&self, from: usize, to: usize) -> Self {
Self {
start: self.start + Index::from_usize(from).unwrap(),
end: Index::min(self.end, self.start + Index::from_usize(to).unwrap()),
value: self.value.clone(),
}
}
}
impl<Index: GlobalIndex, T> HasLength for WithStartEnd<Index, T> {
fn len(&self) -> usize {
Index::as_(self.end - self.start)
}
}
impl<Index: GlobalIndex, T: PartialEq + Eq> Mergable for WithStartEnd<Index, T> {
fn is_mergable(&self, other: &Self, _conf: &()) -> bool
where
Self: Sized,
{
self.end == other.start && self.value == other.value
}
fn merge(&mut self, other: &Self, _conf: &())
where
Self: Sized,
{
self.end = other.end;
}
}
#[cfg(test)]
mod test {
use super::*;

View file

@ -10,7 +10,6 @@ use tree_trait::RleTreeTrait;
mod cursor;
pub mod iter;
pub mod node;
pub mod nonnull;
#[cfg(test)]
mod test;
pub mod tree_trait;

View file

@ -1,31 +0,0 @@
use std::ptr::NonNull;
use crate::{HasLength, Mergable, Sliceable};
impl<T> Mergable for NonNull<T> {
fn is_mergable(&self, _other: &Self, _conf: &()) -> bool
where
Self: Sized,
{
false
}
fn merge(&mut self, _other: &Self, _conf: &())
where
Self: Sized,
{
unreachable!()
}
}
impl<T> Sliceable for NonNull<T> {
fn slice(&self, _from: usize, _to: usize) -> Self {
*self
}
}
impl<T> HasLength for NonNull<T> {
fn len(&self) -> usize {
1
}
}

View file

@ -30,15 +30,15 @@ impl Interaction {
}
fn test(tree: &mut RleTree<Range<usize>, RangeTreeTrait>, interactions: &[Interaction]) {
let mut range_map: RangeMap<usize, NonNull<LeafNode<Range<usize>, RangeTreeTrait>>> =
Default::default();
let mut func = |range: &Range<usize>, node: *mut LeafNode<'_, Range<usize>, RangeTreeTrait>| {
let ptr = unsafe { NonNull::new_unchecked(node as usize as *mut _) };
range_map.set(range.start, ptr);
};
for interaction in interactions.iter() {
interaction.apply(tree, &mut func);
}
// let mut range_map: RangeMap<usize, NonNull<LeafNode<Range<usize>, RangeTreeTrait>>> =
// Default::default();
// let mut func = |range: &Range<usize>, node: *mut LeafNode<'_, Range<usize>, RangeTreeTrait>| {
// let ptr = unsafe { NonNull::new_unchecked(node as usize as *mut _) };
// range_map.set(range.start, ptr);
// };
// for interaction in interactions.iter() {
// interaction.apply(tree, &mut func);
// }
}
prop_compose! {