Files
obiskim/src/obikrope/src/rope.rs
T
Eric Coissac 0dcb5dd6c2 ♻️ refactor rope implementation to use obikrope
- rename `obirope` → `obikroper`
- replace legacy rope with new in-place, Cell-based implementation
  - add ForwardCursor/Backward Cursor & SeekMode support (no more BytesMut)
- update all dependents:
  - obiread: switch to Rope + cursors, remove tape.rs
    • chunk iterator yields `Rope` instead of Vec<Bytes>
  - obiskbuilder: use ForwardCursor over Rope
- remove bytes dependency from affected crates
2026-04-19 21:23:10 +02:00

115 lines
3.2 KiB
Rust

use crate::{BackwardCursor, ForwardCursor, RopeError};
use std::cell::Cell;
pub struct Rope {
pub(crate) blocks: Vec<Vec<Cell<u8>>>,
pub(crate) length: usize,
pub(crate) start_block_idx: Vec<usize>,
}
impl Rope {
pub fn new() -> Self {
Self {
blocks: Vec::new(),
length: 0,
start_block_idx: Vec::new(),
}
}
pub fn push(&mut self, block: Vec<u8>) {
let block_len = block.len();
self.start_block_idx.push(self.length);
// Safety: Cell<u8> has the same memory layout as u8 (guaranteed by the language)
let cell_block: Vec<Cell<u8>> = unsafe {
let mut v = std::mem::ManuallyDrop::new(block);
Vec::from_raw_parts(v.as_mut_ptr() as *mut Cell<u8>, v.len(), v.capacity())
};
self.blocks.push(cell_block);
self.length += block_len;
}
pub fn n_blocks(&self) -> usize {
self.blocks.len()
}
pub(crate) fn get_block(&self, block_idx: usize) -> Option<&[Cell<u8>]> {
self.blocks.get(block_idx).map(Vec::as_slice)
}
pub fn len(&self) -> usize {
self.length
}
pub(crate) fn lookup(&self, i: usize) -> Option<(usize, usize, usize)> {
if i >= self.length || self.blocks.is_empty() {
return None;
}
let block_idx = self.start_block_idx.partition_point(|&s| s <= i) - 1;
let from = self.start_block_idx[block_idx];
let to = if block_idx + 1 < self.blocks.len() {
self.start_block_idx[block_idx + 1]
} else {
self.length
};
Some((block_idx, from, to))
}
pub fn split_off(&mut self, pos: usize) -> Result<Rope, RopeError> {
if pos > self.length {
return Err(RopeError::OutOfBounds(format!(
"split_at: pos={} > length={}",
pos, self.length
)));
}
// pos == length: tail is empty.
if pos == self.length {
return Ok(Rope::new());
}
let (block_idx, from, _) = self.lookup(pos).ok_or_else(|| {
RopeError::OutOfBounds(format!("split_at: lookup failed at pos={}", pos))
})?;
let cut_offset = pos - from;
// Keep block_idx in self temporarily, split it, move remainder to tail.
let mut tail_blocks = self.blocks.split_off(block_idx + 1);
self.start_block_idx.truncate(block_idx + 1);
let tail_part = self.blocks[block_idx].split_off(cut_offset);
if !tail_part.is_empty() {
tail_blocks.insert(0, tail_part);
}
let mut tail_length = 0;
let tail_starts: Vec<usize> = tail_blocks
.iter()
.map(|b| {
let s = tail_length;
tail_length += b.len();
s
})
.collect();
self.length = pos;
Ok(Rope {
blocks: tail_blocks,
length: tail_length,
start_block_idx: tail_starts,
})
}
pub fn is_empty(&self) -> bool {
self.blocks.is_empty()
}
pub fn fw_cursor(&self) -> ForwardCursor<'_> {
ForwardCursor::new(self)
}
pub fn bw_cursor(&self) -> BackwardCursor<'_> {
BackwardCursor::new(self)
}
}