work on iterating

This commit is contained in:
janis 2025-09-17 18:06:36 +02:00
parent c2cecdf85f
commit 81f92738db
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -183,6 +183,16 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V> {
})
.ok_or(self)
}
pub(crate) fn first_edge(self) -> Handle<NodeRef<BorrowType, K, V>, marker::Edge> {
unsafe { Handle::new_edge(self, 0) }
}
pub(crate) fn last_edge(self) -> Handle<NodeRef<BorrowType, K, V>, marker::Edge> {
let len = self.len();
assert!(len > 0);
unsafe { Handle::new_edge(self, len) }
}
}
impl<BorrowType: marker::BorrowType, K, V> Handle<NodeRef<BorrowType, K, V>, marker::Edge> {
@ -616,6 +626,27 @@ impl<BorrowType, K, V> NodeRef<BorrowType, K, V> {
}
}
}
impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V> {
fn first_leaf_edge(self) -> Handle<NodeRef<BorrowType, K, V>, marker::Edge> {
let mut node = self;
loop {
match node.force() {
ForceResult::Leaf(leaf) => break leaf.first_edge(),
ForceResult::Internal(internal) => node = internal.first_edge().descend(),
}
}
}
fn last_leaf_edge(self) -> Handle<NodeRef<BorrowType, K, V>, marker::Edge> {
let mut node = self;
loop {
match node.force() {
ForceResult::Leaf(leaf) => break leaf.last_edge(),
ForceResult::Internal(internal) => node = internal.last_edge().descend(),
}
}
}
}
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V> {
pub(super) fn grow_node(&mut self) {
@ -1433,6 +1464,73 @@ unsafe fn slice_insert<T>(slice: &mut [MaybeUninit<T>], idx: usize, value: T) {
}
}
mod range {
#![allow(dead_code)]
use core::ptr;
use super::marker;
use crate::tree::{Handle, NodeRef};
enum LeafHandle<BorrowType, K, V> {
Root(NodeRef<BorrowType, K, V>),
Edge(Handle<NodeRef<BorrowType, K, V>, marker::Edge>),
}
pub(crate) struct LeafRange<BorrowType, K, V> {
front: Option<LeafHandle<BorrowType, K, V>>,
back: Option<LeafHandle<BorrowType, K, V>>,
}
impl<'a, K: 'a, V: 'a> Clone for LeafHandle<marker::Immut<'a>, K, V> {
fn clone(&self) -> Self {
match self {
LeafHandle::Root(node) => LeafHandle::Root(*node),
LeafHandle::Edge(handle) => LeafHandle::Edge(*handle),
}
}
}
impl<BorrowType, K, V> LeafHandle<BorrowType, K, V> {
fn reborrow(&self) -> LeafHandle<marker::Immut<'_>, K, V> {
match self {
Self::Root(node) => LeafHandle::Root(node.reborrow()),
Self::Edge(handle) => LeafHandle::Edge(handle.reborrow()),
}
}
}
impl<BorrowType: marker::BorrowType, K, V> LeafRange<BorrowType, K, V> {
fn init_front(&mut self) -> Option<&mut Handle<NodeRef<BorrowType, K, V>, marker::Edge>> {
if let Some(LeafHandle::Root(root)) = &self.front {
self.front = Some(LeafHandle::Edge(
unsafe { ptr::read(root) }.first_leaf_edge(),
));
}
match &mut self.front {
None => None,
Some(LeafHandle::Edge(edge)) => Some(edge),
Some(LeafHandle::Root(_)) => unreachable!(),
}
}
fn init_back(&mut self) -> Option<&mut Handle<NodeRef<BorrowType, K, V>, marker::Edge>> {
if let Some(LeafHandle::Root(root)) = &self.back {
self.back = Some(LeafHandle::Edge(
unsafe { ptr::read(root) }.last_leaf_edge(),
));
}
match &mut self.back {
None => None,
Some(LeafHandle::Edge(edge)) => Some(edge),
Some(LeafHandle::Root(_)) => unreachable!(),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;