2022-08-10 10:42:36 +00:00
|
|
|
use std::{fmt::Debug, ops::Range};
|
2022-08-05 12:04:49 +00:00
|
|
|
|
2022-08-11 11:09:07 +00:00
|
|
|
use num::{cast, Integer, NumCast};
|
2022-08-05 10:47:51 +00:00
|
|
|
|
2022-07-26 18:24:25 +00:00
|
|
|
pub trait Mergable<Cfg = ()> {
|
|
|
|
fn is_mergable(&self, _other: &Self, _conf: &Cfg) -> bool
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge(&mut self, _other: &Self, _conf: &Cfg)
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Sliceable {
|
|
|
|
fn slice(&self, from: usize, to: usize) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct Slice<'a, T> {
|
|
|
|
pub value: &'a T,
|
|
|
|
pub start: usize,
|
|
|
|
pub end: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Sliceable> Slice<'_, T> {
|
|
|
|
pub fn into_inner(&self) -> T {
|
|
|
|
self.value.slice(self.start, self.end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 03:31:07 +00:00
|
|
|
#[allow(clippy::len_without_is_empty)]
|
2022-07-26 18:24:25 +00:00
|
|
|
pub trait HasLength {
|
2022-10-11 18:00:31 +00:00
|
|
|
/// if the content is deleted, len should be zero
|
2022-07-26 18:24:25 +00:00
|
|
|
fn len(&self) -> usize;
|
2022-10-11 16:16:38 +00:00
|
|
|
|
2022-10-11 18:00:31 +00:00
|
|
|
/// the actual length of the value, cannot be affected by delete state
|
2022-10-11 16:16:38 +00:00
|
|
|
fn content_len(&self) -> usize {
|
|
|
|
self.len()
|
|
|
|
}
|
2022-07-26 18:24:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 08:50:22 +00:00
|
|
|
pub trait Rle<Cfg = ()>: HasLength + Sliceable + Mergable<Cfg> + Debug + Clone {}
|
2022-07-26 18:24:25 +00:00
|
|
|
|
2022-10-11 08:50:22 +00:00
|
|
|
impl<T: HasLength + Sliceable + Mergable<Cfg> + Debug + Clone, Cfg> Rle<Cfg> for T {}
|
2022-08-05 12:04:49 +00:00
|
|
|
|
|
|
|
impl<T: Integer + NumCast + Copy> Sliceable for Range<T> {
|
|
|
|
fn slice(&self, start: usize, end: usize) -> Self {
|
|
|
|
self.start + cast(start).unwrap()..self.start + cast(end).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: PartialOrd<T> + Copy> Mergable for Range<T> {
|
|
|
|
fn is_mergable(&self, other: &Self, _: &()) -> bool {
|
|
|
|
other.start <= self.end && other.start >= self.start
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge(&mut self, other: &Self, _conf: &())
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
self.end = other.end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: num::Integer + NumCast + Copy> HasLength for Range<T> {
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
cast(self.end - self.start).unwrap()
|
|
|
|
}
|
|
|
|
}
|