Skip to content

Commit

Permalink
move Anchor and AnchorBias to the new anchor module
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Jan 8, 2024
1 parent 2d7601b commit a7b06c9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 86 deletions.
72 changes: 72 additions & 0 deletions src/anchor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::*;

/// TODO: docs
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(
any(feature = "encode", feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
pub struct Anchor {
/// TODO: docs
replica_id: ReplicaId,

/// The [`RunTs`] of the [`EditRun`] containing this [`Anchor`].
contained_in: RunTs,

/// TODO: docs
offset: Length,
}

impl core::fmt::Debug for Anchor {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self == &Self::zero() {
write!(f, "zero")
} else {
write!(f, "{:x}.{}", self.replica_id, self.offset)
}
}
}

impl Anchor {
#[inline(always)]
pub(crate) fn is_zero(&self) -> bool {
self.replica_id == 0
}

#[inline(always)]
pub(crate) fn new(
replica_id: ReplicaId,
offset: Length,
run_ts: RunTs,
) -> Self {
Self { replica_id, offset, contained_in: run_ts }
}

#[inline(always)]
pub(crate) fn offset(&self) -> Length {
self.offset
}

#[inline(always)]
pub(crate) fn replica_id(&self) -> ReplicaId {
self.replica_id
}

#[inline(always)]
pub(crate) fn run_ts(&self) -> RunTs {
self.contained_in
}

/// A special value used to create an anchor at the start of the document.
#[inline]
pub const fn zero() -> Self {
Self { replica_id: 0, offset: 0, contained_in: 0 }
}
}

/// TODO: docs
#[derive(PartialEq, Eq)]
pub(crate) enum AnchorBias {
Left,
Right,
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@

extern crate alloc;

mod anchor;
mod backlog;
mod crdt_edit;
mod gtree;
Expand All @@ -137,6 +138,7 @@ mod text_edit;
mod utils;
mod version_map;

use anchor::*;
use backlog::Backlog;
pub use backlog::{BackloggedDeletions, BackloggedInsertions};
pub use crdt_edit::{Deletion, Insertion};
Expand All @@ -145,7 +147,7 @@ pub use replica::Replica;
use replica::*;
pub use replica_id::ReplicaId;
use replica_id::{ReplicaIdMap, ReplicaIdMapValuesMut};
use run_indices::{AnchorBias, RunIndices};
use run_indices::RunIndices;
use run_tree::*;
pub use text_edit::Text;
use utils::*;
Expand Down
2 changes: 1 addition & 1 deletion src/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ impl Replica {
/// somewhere in its Gtree.
#[inline]
fn has_anchor(&self, anchor: Anchor) -> bool {
self.version_map.get(anchor.replica_id()) >= anchor.character_ts()
self.version_map.get(anchor.replica_id()) >= anchor.offset()
}

/// Returns `true` if this `Replica` has already merged the given
Expand Down
7 changes: 0 additions & 7 deletions src/run_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ impl core::fmt::Debug for RunIndices {
}
}

/// TODO: docs
#[derive(PartialEq, Eq)]
pub(crate) enum AnchorBias {
Left,
Right,
}

impl RunIndices {
pub fn assert_invariants(&self, run_tree: &RunTree) {
for (&replica_id, indices) in self.map.iter() {
Expand Down
85 changes: 8 additions & 77 deletions src/run_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,10 @@ impl RunTree {
let delete_from = if deletion.start().is_zero() {
0
} else {
deletion.start().offset - run.start()
deletion.start().offset() - run.start()
};

let delete_up_to = deletion.end().offset - run.start();
let delete_up_to = deletion.end().offset() - run.start();
let delete_range = (delete_from..delete_up_to).into();
self.delete_leaf_range(start_idx, leaf_offset, delete_range);
ranges.push((delete_range + leaf_offset).into());
Expand Down Expand Up @@ -512,7 +512,7 @@ impl RunTree {
let delete_from = if deletion.start().is_zero() {
0
} else {
deletion.start().offset - start.start()
deletion.start().offset() - start.start()
};

let deleted_up_to = deletion.version_map().get(start.replica_id());
Expand Down Expand Up @@ -565,7 +565,7 @@ impl RunTree {
ranges.push(start_offset..visible_offset);
}
} else {
let delete_up_to = deletion.end().offset - run.start();
let delete_up_to = deletion.end().offset() - run.start();

self.delete_leaf_range(
end_idx,
Expand Down Expand Up @@ -680,8 +680,8 @@ impl RunTree {
// If the insertion is anchored in the middle of the anchor run then
// there can't be any other runs that are tied with it. In this case we
// can just split the anchor run and insert the new run after it.
if insertion.anchor().offset < anchor.end() {
let insert_at = insertion.anchor().offset - anchor.start();
if insertion.anchor().offset() < anchor.end() {
let insert_at = insertion.anchor().offset() - anchor.start();
return self.split_run_with_another(run, anchor_idx, insert_at);
}

Expand Down Expand Up @@ -873,8 +873,8 @@ impl EditRun {
debug_assert!(!anchor.is_zero());

self.replica_id() == anchor.replica_id()
&& self.text.start() < anchor.offset
&& self.text.end() >= anchor.offset
&& self.text.start() < anchor.offset()
&& self.text.end() >= anchor.offset()
}

#[inline(always)]
Expand Down Expand Up @@ -1015,75 +1015,6 @@ impl EditRun {
}
}

/// TODO: docs
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(
any(feature = "encode", feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
pub struct Anchor {
/// TODO: docs
replica_id: ReplicaId,

/// The [`RunTs`] of the [`EditRun`] containing this [`Anchor`].
contained_in: RunTs,

/// TODO: docs
offset: Length,
}

impl core::fmt::Debug for Anchor {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self == &Self::zero() {
write!(f, "zero")
} else {
write!(f, "{:x}.{}", self.replica_id, self.offset)
}
}
}

impl Anchor {
#[inline(always)]
pub(crate) fn character_ts(&self) -> Length {
self.offset
}

#[inline(always)]
pub(crate) fn is_zero(&self) -> bool {
self.replica_id == 0
}

#[inline(always)]
pub(crate) fn new(
replica_id: ReplicaId,
offset: Length,
run_ts: RunTs,
) -> Self {
Self { replica_id, offset, contained_in: run_ts }
}

#[inline(always)]
pub(crate) fn offset(&self) -> Length {
self.offset
}

#[inline(always)]
pub(crate) fn replica_id(&self) -> ReplicaId {
self.replica_id
}

#[inline(always)]
pub(crate) fn run_ts(&self) -> RunTs {
self.contained_in
}

/// A special value used to create an anchor at the start of the document.
#[inline]
pub const fn zero() -> Self {
Self { replica_id: 0, offset: 0, contained_in: 0 }
}
}

#[derive(Debug, Clone, Copy)]
pub enum Diff {
Add(Length),
Expand Down

0 comments on commit a7b06c9

Please sign in to comment.