refactor(tree): remove unused constants, debug prints, and imports

- Removed unused constants and redundant imports.
- Eliminated debug print statements from the `search` module.
- Added `#[allow(dead_code)]` annotations to suppress warnings for unused functions.
- Made `Tree` and `as_subtree_mut` public
This commit is contained in:
Janis 2025-08-15 23:46:43 +02:00
parent 9663e95210
commit 41f8763f18

View file

@ -18,13 +18,6 @@ use core::{marker::PhantomData, mem::MaybeUninit, ptr::NonNull};
mod marker { mod marker {
use core::marker::PhantomData; use core::marker::PhantomData;
#[derive(Debug)]
pub struct LeafOrInternal;
#[derive(Debug)]
pub struct Leaf;
#[derive(Debug)]
pub struct Internal;
#[derive(Debug)] #[derive(Debug)]
pub struct Edge; pub struct Edge;
#[derive(Debug)] #[derive(Debug)]
@ -58,8 +51,6 @@ mod marker {
impl BorrowType for DormantMut {} impl BorrowType for DormantMut {}
} }
const CAPACITY: usize = 16;
#[derive(Debug)] #[derive(Debug)]
struct LeafNode<K, V> { struct LeafNode<K, V> {
parent: Option<BoxedNode<K, V>>, parent: Option<BoxedNode<K, V>>,
@ -163,6 +154,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V> {
/// ///
/// `edge.descend().ascend().unwrap()` and `node.ascend().unwrap().descend()` should /// `edge.descend().ascend().unwrap()` and `node.ascend().unwrap().descend()` should
/// both, upon success, do nothing. /// both, upon success, do nothing.
#[allow(dead_code)]
pub(super) fn ascend(self) -> Result<Handle<NodeRef<BorrowType, K, V>, marker::Edge>, Self> { pub(super) fn ascend(self) -> Result<Handle<NodeRef<BorrowType, K, V>, marker::Edge>, Self> {
const { const {
assert!(BorrowType::TRAVERSAL_PERMIT); assert!(BorrowType::TRAVERSAL_PERMIT);
@ -220,6 +212,7 @@ impl<'a, K, V, HandleType> Handle<NodeRef<marker::Mut<'a>, K, V>, HandleType> {
/// dangerous. /// dangerous.
/// ///
/// For details, see `NodeRef::reborrow_mut`. /// For details, see `NodeRef::reborrow_mut`.
#[allow(dead_code)]
pub(super) unsafe fn reborrow_mut( pub(super) unsafe fn reborrow_mut(
&mut self, &mut self,
) -> Handle<NodeRef<marker::Mut<'_>, K, V>, HandleType> { ) -> Handle<NodeRef<marker::Mut<'_>, K, V>, HandleType> {
@ -266,6 +259,7 @@ impl<K, V> NodeRef<marker::Owned, K, V> {
} }
/// Slightly mutably borrows the owned root node. /// Slightly mutably borrows the owned root node.
#[allow(dead_code)]
pub(super) fn borrow_valmut(&mut self) -> NodeRef<marker::ValMut<'_>, K, V> { pub(super) fn borrow_valmut(&mut self) -> NodeRef<marker::ValMut<'_>, K, V> {
NodeRef { NodeRef {
node: self.node, node: self.node,
@ -275,6 +269,7 @@ impl<K, V> NodeRef<marker::Owned, K, V> {
/// Irreversibly transitions to a reference that permits traversal and offers /// Irreversibly transitions to a reference that permits traversal and offers
/// destructive methods and little else. /// destructive methods and little else.
#[allow(dead_code)]
pub(super) fn into_dying(self) -> NodeRef<marker::Dying, K, V> { pub(super) fn into_dying(self) -> NodeRef<marker::Dying, K, V> {
NodeRef { NodeRef {
node: self.node, node: self.node,
@ -471,6 +466,7 @@ impl<BorrowType, K, V> NodeRef<BorrowType, K, V> {
this.node.as_ptr() this.node.as_ptr()
} }
#[allow(dead_code)]
fn as_leaf(&self) -> &LeafNode<K, V> { fn as_leaf(&self) -> &LeafNode<K, V> {
// SAFETY: the static node type is `Leaf`. // SAFETY: the static node type is `Leaf`.
unsafe { &*Self::as_leaf_ptr(self) } unsafe { &*Self::as_leaf_ptr(self) }
@ -540,6 +536,7 @@ pub(super) enum ForceResult<Node> {
Internal(Node), Internal(Node),
} }
#[allow(dead_code)]
impl<BorrowType, K, V, HandleType> Handle<NodeRef<BorrowType, K, V>, HandleType> { impl<BorrowType, K, V, HandleType> Handle<NodeRef<BorrowType, K, V>, HandleType> {
fn force(self) -> ForceResult<Handle<NodeRef<BorrowType, K, V>, HandleType>> { fn force(self) -> ForceResult<Handle<NodeRef<BorrowType, K, V>, HandleType>> {
match self.node.force() { match self.node.force() {
@ -645,6 +642,7 @@ impl<K, V> NodeRef<marker::Owned, K, V> {
} }
} }
#[allow(dead_code)]
impl<BorrowType, K, V> Handle<NodeRef<BorrowType, K, V>, marker::Edge> { impl<BorrowType, K, V> Handle<NodeRef<BorrowType, K, V>, marker::Edge> {
/// Converts this edge handle into a value handle. /// Converts this edge handle into a value handle.
/// IMPORTANT: this handle points to the value of the node, not the edge. /// IMPORTANT: this handle points to the value of the node, not the edge.
@ -778,17 +776,14 @@ mod search {
match key.cmp(k.borrow()) { match key.cmp(k.borrow()) {
Ordering::Greater => {} Ordering::Greater => {}
Ordering::Equal => { Ordering::Equal => {
std::eprintln!("found key at index {}", start_index + offset);
return IndexResult::Edge(start_index + offset); return IndexResult::Edge(start_index + offset);
} }
Ordering::Less => { Ordering::Less => {
std::eprintln!("insert key at index {}", start_index + offset);
return IndexResult::Insert(start_index + offset); return IndexResult::Insert(start_index + offset);
} }
} }
} }
std::eprintln!("push_back key at index {}", keys.len());
IndexResult::Insert(keys.len()) IndexResult::Insert(keys.len())
} }
} }
@ -796,7 +791,6 @@ mod search {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::super::Tree; use super::super::Tree;
use super::*;
fn insert_and_dbg<'a>( fn insert_and_dbg<'a>(
tree: &'a mut Tree<char, &'static str>, tree: &'a mut Tree<char, &'static str>,
@ -837,9 +831,7 @@ enum HandleOrTree<'a, BorrowType, K, V, HandleType> {
mod entry { mod entry {
use core::marker::PhantomData; use core::marker::PhantomData;
use crate::tree::LeafNode; use super::{Handle, NodeRef, marker};
use super::{Handle, NodeRef, Tree, borrow::DormantMutRef, marker};
pub enum Entry<'a, Q: 'a, K: 'a, V: 'a> pub enum Entry<'a, Q: 'a, K: 'a, V: 'a>
where where
@ -1067,6 +1059,8 @@ mod subtree {
} }
mod borrow { mod borrow {
#![allow(dead_code)]
use core::{marker::PhantomData, ptr::NonNull}; use core::{marker::PhantomData, ptr::NonNull};
/// Models a reborrow of some unique reference, when you know that the reborrow /// Models a reborrow of some unique reference, when you know that the reborrow
@ -1142,7 +1136,7 @@ mod borrow {
type Root<K, V> = NodeRef<marker::Owned, K, V>; type Root<K, V> = NodeRef<marker::Owned, K, V>;
struct Tree<K, V> { pub struct Tree<K, V> {
root: Option<Root<K, V>>, root: Option<Root<K, V>>,
_marker: PhantomData<alloc::boxed::Box<(K, V)>>, _marker: PhantomData<alloc::boxed::Box<(K, V)>>,
} }
@ -1263,7 +1257,7 @@ impl<K, V> Tree<K, V>
where where
K: Ord, K: Ord,
{ {
fn as_subtree_mut<'a>(&'a mut self) -> Option<subtree::Subtree<K, V, marker::Mut<'a>>> { pub fn as_subtree_mut<'a>(&'a mut self) -> Option<subtree::Subtree<K, V, marker::Mut<'a>>> {
let root = self.root.as_mut()?.borrow_mut().dormant(); let root = self.root.as_mut()?.borrow_mut().dormant();
Some(subtree::Subtree { Some(subtree::Subtree {