Skip to main content

BlockOffsetIndex

Struct BlockOffsetIndex 

Source
pub struct BlockOffsetIndex {
    pub entries: Arc<Vec<(OffsetMarker, u32)>>,
    pub total_bytes: u32,
    /* private fields */
}

Fields§

§entries: Arc<Vec<(OffsetMarker, u32)>>

(marker, byte_start) pairs sorted by byte_start ascending.

Wrapped in Arc so the per-edit snapshot path (which clones BlockOffsetIndex as part of Store::snapshot) does an O(1) pointer bump instead of an O(N) memcpy of the underlying Vec. Mutators in this impl block go through Arc::make_mut, which deep-clones the Vec only on the first write after the Arc has been shared (i.e., after a snapshot was taken). External callers see Arc<Vec<...>> which derefs transparently to &[...] for reads.

§total_bytes: u32

Total byte length of the rope this index describes. The last entry extends from its byte_start to this value.

Implementations§

Source§

impl BlockOffsetIndex

Source

pub fn new() -> Self

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn total_bytes(&self) -> u32

Source

pub fn set_total_bytes(&mut self, total: u32)

Source

pub fn table_anchor_count(&self) -> usize

Number of OffsetMarker::TableAnchor entries currently indexed. O(1) via a maintained counter. rope_positions_match_flow subtracts it from len() to count the blocks the rope mirrors without walking the entries.

Source

pub fn insert_at( &mut self, position: usize, marker: OffsetMarker, byte_start: u32, )

Insert a marker at a given byte position. The caller is responsible for keeping the byte_start ordered relative to neighbours — this method does NOT re-sort.

Source

pub fn insert_run_at(&mut self, position: usize, run: &[(OffsetMarker, u32)])

Insert consecutive markers starting at position, in order. The result is exactly what calling insert_at once per marker at position, position + 1, … would leave, but the marker index is walked once for the whole run instead of once per marker: inserting a run of N markers one at a time costs N walks of every marker in the document. The caller keeps the byte_starts ordered, as for insert_at.

Source

pub fn reorder_run(&mut self, position: usize, run: &[(OffsetMarker, u32)])

Replace the entries from position on with run, one for one: the markers the run holds are the markers those entries held, in another order and at other byte starts. The caller keeps the byte_starts ordered, as for insert_at. Nothing outside the run moves, so only the run’s own markers are re-indexed.

Source

pub fn remove_bytes_after(&mut self, cuts: &[(usize, u32)])

Take bytes out after several entries at once: for each (position, bytes), every entry past position moves back by bytes, and so does total_bytes. The result is exactly what calling shift_after(entries[position].1 + 1, -bytes) once per pair leaves, provided each entry at position starts before the entry after it, as an entry with content or a boundary after it does. The entries are walked once for the whole list instead of once per pair. cuts is in increasing position order.

Source

pub fn shift_after_each(&mut self, shifts: &[(u32, i32)])

Shift the entries past several thresholds at once: for each (threshold, delta), every entry starting at or past threshold moves by delta, and so does total_bytes. The result is exactly what calling shift_after once per pair leaves, every threshold read in the offsets as they stand before any of them moves, as the edits of one text applied from its end back do. The entries are walked once for the whole list instead of once per pair. shifts is in increasing threshold order.

Source

pub fn remove_entries(&mut self, dropped: &[bool], cuts: &[(u32, u32)])

Drop the entries dropped flags (one flag per entry, by position) and take the byte ranges cuts out of the text they index: every kept entry moves back by the bytes cut in front of it, and total_bytes by all of them. cuts is disjoint and in increasing order, and no cut straddles a kept entry’s first byte: each ends at or before it, or starts at or after it. A cut starting exactly there is cut after the entry, which keeps its offset: that is how the trailing entries of the rope go when the entry before them is empty. One walk of the entries, however many are dropped: removing them one at a time walked the index once each.

Source

pub fn push(&mut self, marker: OffsetMarker, byte_start: u32)

Append a marker at the end (its byte_start must be ≥ the last entry’s byte_start).

Source

pub fn push_block(&mut self, block_id: EntityId, byte_start: u32)

Convenience: register a block by id. Equivalent to push(OffsetMarker::Block(id), byte_start).

Source

pub fn remove_at(&mut self, position: usize) -> (OffsetMarker, u32)

Remove the entry at the given position. Panics if out of bounds.

Source

pub fn drain_inclusive( &mut self, start: usize, end_inclusive: usize, ) -> Vec<(OffsetMarker, u32)>

Remove a contiguous range of entries, equivalent to entries.drain(start..=end_inclusive) plus the matching marker_index maintenance. Returns the removed entries.

Source

pub fn clear(&mut self)

Drop every entry and reset total_bytes to zero. Equivalent to *self = Self::default() but expressed as a method so callers don’t need to depend on Default.

Source

pub fn rebuild_marker_index(&mut self)

Rebuild marker_index and table_anchor_count from entries. Use after bulk mutations that bypassed the maintenance methods (e.g. raw entries.push in test setup).

Source

pub fn range_of(&self, marker: OffsetMarker) -> Option<(u32, u32)>

Byte range (start, end) of a marker. end is the next marker’s byte_start (or total_bytes for the last entry). Returns None if the marker is not indexed.

O(1) average via the marker_index map.

Source

pub fn range_of_block(&self, block_id: EntityId) -> Option<(u32, u32)>

Byte range for a block-id specifically. Convenience for the common case.

Source

pub fn range_with_successor( &self, marker: OffsetMarker, ) -> Option<(u32, u32, bool)>

Like range_of, but also reports whether the marker has a successor entry. Callers that need to strip the trailing inter-block \n boundary (e.g. block_content_via_store, block_char_length) use this to distinguish “end == next marker’s byte_start, with a real \n between us” from “end == total_bytes, no separator after”.

O(1) average via marker_index.

Source

pub fn position_of(&self, marker: OffsetMarker) -> Option<usize>

Position of marker in entries. O(1) average via the marker_index cache. Returns None if the marker is not indexed.

Source

pub fn marker_at_byte(&self, byte: u32) -> Option<OffsetMarker>

Marker whose byte range covers byte. Returns None if the index is empty or byte falls past total_bytes.

byte == total_bytes is treated as belonging to the last entry (this is the cursor-at-end-of-document case).

Source

pub fn block_at_byte(&self, byte: u32) -> Option<EntityId>

Block id whose byte range covers byte, ignoring table-anchor markers. Returns None if no block covers the byte.

Source

pub fn byte_to_marker_byte(&self, byte: u32) -> Option<(OffsetMarker, u32)>

Convert an absolute rope byte offset into (marker, byte_in_marker). Returns None for offsets past the end or for an empty index.

Source

pub fn byte_to_block_byte(&self, byte: u32) -> Option<(EntityId, u32)>

Block-only variant of byte_to_marker_byte.

Source

pub fn shift_after(&mut self, threshold: u32, delta: i32)

Shift every entry whose byte_start ≥ threshold by delta bytes, and adjust total_bytes by delta. Used after a rope insert (positive delta) or delete (negative delta) to keep the index in sync without a full rebuild.

entries is sorted by byte_start, so the affected suffix is located via partition_point (O(log n)) and only that suffix is walked.

Trait Implementations§

Source§

impl Clone for BlockOffsetIndex

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BlockOffsetIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BlockOffsetIndex

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for BlockOffsetIndex

Source§

impl PartialEq for BlockOffsetIndex

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for BlockOffsetIndex

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.