This commit is contained in:
Antonio Scandurra 2021-12-09 18:27:54 +01:00
parent 91a7bbbba2
commit e9c385e7a6
2 changed files with 17 additions and 8 deletions

View File

@ -2,23 +2,28 @@ use smallvec::{smallvec, SmallVec};
use std::iter;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Locator(SmallVec<[u8; 4]>);
pub struct Locator(SmallVec<[u64; 4]>);
impl Locator {
pub fn min() -> Self {
Self(smallvec![u8::MIN])
Self(smallvec![u64::MIN])
}
pub fn max() -> Self {
Self(smallvec![u8::MAX])
Self(smallvec![u64::MAX])
}
pub fn assign(&mut self, other: &Self) {
self.0.resize(other.0.len(), 0);
self.0.copy_from_slice(&other.0);
}
pub fn between(lhs: &Self, rhs: &Self) -> Self {
let lhs = lhs.0.iter().copied().chain(iter::repeat(u8::MIN));
let rhs = rhs.0.iter().copied().chain(iter::repeat(u8::MAX));
let lhs = lhs.0.iter().copied().chain(iter::repeat(u64::MIN));
let rhs = rhs.0.iter().copied().chain(iter::repeat(u64::MAX));
let mut location = SmallVec::new();
for (lhs, rhs) in lhs.zip(rhs) {
let mid = lhs + (rhs.saturating_sub(lhs)) / 2;
let mid = lhs + ((rhs.saturating_sub(lhs)) >> 48);
location.push(mid);
if mid > lhs {
break;
@ -26,6 +31,10 @@ impl Locator {
}
Self(location)
}
pub fn len(&self) -> usize {
self.0.len()
}
}
impl Default for Locator {

View File

@ -2002,7 +2002,7 @@ impl sum_tree::Summary for FragmentSummary {
type Context = Option<clock::Global>;
fn add_summary(&mut self, other: &Self, _: &Self::Context) {
self.max_id = other.max_id.clone();
self.max_id.assign(&other.max_id);
self.text.visible += &other.text.visible;
self.text.deleted += &other.text.deleted;
self.max_version.join(&other.max_version);
@ -2113,7 +2113,7 @@ impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FullOffset {
impl<'a> sum_tree::Dimension<'a, FragmentSummary> for Locator {
fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
*self = summary.max_id.clone();
self.assign(&summary.max_id);
}
}