loro/crates/rle/src/rle_trait.rs

123 lines
2.8 KiB
Rust
Raw Normal View History

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};
use smallvec::{Array, SmallVec};
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 18:00:31 +00:00
/// the actual length of the value, cannot be affected by delete state
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-12 17:24:47 +00:00
pub trait ZeroElement {
fn zero_element() -> Self;
}
impl<T: Default> ZeroElement for T {
fn zero_element() -> Self {
Default::default()
}
}
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()
}
}
2022-10-18 13:54:32 +00:00
/// this can make iter return type has len
impl<A, T: HasLength> HasLength for (A, T) {
fn len(&self) -> usize {
self.1.len()
}
}
2022-10-20 08:29:38 +00:00
/// this can make iter return type has len
impl<T: HasLength> HasLength for &T {
fn len(&self) -> usize {
(*self).len()
}
}
impl<T: HasLength + Sliceable, A: Array<Item = T>> Sliceable for SmallVec<A> {
fn slice(&self, from: usize, to: usize) -> Self {
let mut index = 0;
2022-10-21 18:07:26 +00:00
let mut ans: SmallVec<A> = smallvec::smallvec![];
if to == from {
return ans;
}
for item in self.iter() {
if index < to && from < index + item.content_len() {
let start = if index < from { from - index } else { 0 };
2022-10-21 18:07:26 +00:00
ans.push(item.slice(start, item.content_len().min(to - index)));
}
index += item.content_len();
}
ans
}
}