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: u32Total byte length of the rope this index describes. The last
entry extends from its byte_start to this value.
Implementations§
Source§impl BlockOffsetIndex
impl BlockOffsetIndex
pub fn new() -> Self
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn total_bytes(&self) -> u32
pub fn set_total_bytes(&mut self, total: u32)
Sourcepub fn table_anchor_count(&self) -> usize
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.
Sourcepub fn insert_at(
&mut self,
position: usize,
marker: OffsetMarker,
byte_start: u32,
)
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.
Sourcepub fn insert_run_at(&mut self, position: usize, run: &[(OffsetMarker, u32)])
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.
Sourcepub fn reorder_run(&mut self, position: usize, run: &[(OffsetMarker, u32)])
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.
Sourcepub fn remove_bytes_after(&mut self, cuts: &[(usize, u32)])
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.
Sourcepub fn shift_after_each(&mut self, shifts: &[(u32, i32)])
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.
Sourcepub fn remove_entries(&mut self, dropped: &[bool], cuts: &[(u32, u32)])
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.
Sourcepub fn push(&mut self, marker: OffsetMarker, byte_start: u32)
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).
Sourcepub fn push_block(&mut self, block_id: EntityId, byte_start: u32)
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).
Sourcepub fn remove_at(&mut self, position: usize) -> (OffsetMarker, u32)
pub fn remove_at(&mut self, position: usize) -> (OffsetMarker, u32)
Remove the entry at the given position. Panics if out of bounds.
Sourcepub fn drain_inclusive(
&mut self,
start: usize,
end_inclusive: usize,
) -> Vec<(OffsetMarker, u32)>
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.
Sourcepub fn clear(&mut self)
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.
Sourcepub fn rebuild_marker_index(&mut self)
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).
Sourcepub fn range_of(&self, marker: OffsetMarker) -> Option<(u32, u32)>
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.
Sourcepub fn range_of_block(&self, block_id: EntityId) -> Option<(u32, u32)>
pub fn range_of_block(&self, block_id: EntityId) -> Option<(u32, u32)>
Byte range for a block-id specifically. Convenience for the common case.
Sourcepub fn range_with_successor(
&self,
marker: OffsetMarker,
) -> Option<(u32, u32, bool)>
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.
Sourcepub fn position_of(&self, marker: OffsetMarker) -> Option<usize>
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.
Sourcepub fn marker_at_byte(&self, byte: u32) -> Option<OffsetMarker>
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).
Sourcepub fn block_at_byte(&self, byte: u32) -> Option<EntityId>
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.
Sourcepub fn byte_to_marker_byte(&self, byte: u32) -> Option<(OffsetMarker, u32)>
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.
Sourcepub fn byte_to_block_byte(&self, byte: u32) -> Option<(EntityId, u32)>
pub fn byte_to_block_byte(&self, byte: u32) -> Option<(EntityId, u32)>
Block-only variant of byte_to_marker_byte.
Sourcepub fn shift_after(&mut self, threshold: u32, delta: i32)
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.