//! Generic B-Tree implementation. //! //! This module defines a `Btree` type which provides similar functionality to //! `BtreeMap`, but with some important differences in the implementation: //! //! 1. Memory is allocated from a `NodePool` instead of the global heap. //! 2. The footprint of a BTree is only 4 bytes. //! 3. A BTree doesn't implement `Drop`, leaving it to the pool to manage memory. //! //! The node pool is intended to be used as a LIFO allocator. After building up a larger data //! structure with many list references, the whole thing can be discarded quickly by clearing the //! pool. use std::marker::PhantomData; // A Node reference is a direct index to an element of the pool. type NodeRef = u32; /// A B-tree data structure which nodes are allocated from a pool. pub struct BTree { index: NodeRef, unused1: PhantomData, unused2: PhantomData, } /// An enum representing a B-tree node. /// Keys and values are required to implement Default. enum Node { Inner { size: u8, keys: [K; 7], nodes: [NodeRef; 8], }, Leaf { size: u8, keys: [K; 7], values: [V; 7], }, } /// Memory pool for nodes. struct NodePool { // The array containing the nodes. data: Vec>, // A free list freelist: Vec, } impl NodePool { /// Create a new NodePool. pub fn new() -> NodePool { NodePool { data: Vec::new(), freelist: Vec::new(), } } /// Get a B-tree node. pub fn get(&self, index: u32) -> Option<&Node> { unimplemented!() } } impl BTree { /// Search for `key` and return a `Cursor` that either points at `key` or the position /// where it would be inserted. pub fn search(&mut self, key: K) -> Cursor { unimplemented!() } } pub struct Cursor<'a, K: 'a, V: 'a> { pool: &'a mut NodePool, height: usize, path: [(NodeRef, u8); 16], } impl<'a, K: Default, V: Default> Cursor<'a, K, V> { /// The key at the cursor position. Returns `None` when the cursor points off the end. pub fn key(&self) -> Option { unimplemented!() } /// The value at the cursor position. Returns `None` when the cursor points off the end. pub fn value(&self) -> Option<&V> { unimplemented!() } /// Move to the next element. /// Returns `false` if that moves the cursor off the end. pub fn next(&mut self) -> bool { unimplemented!() } /// Move to the previous element. /// Returns `false` if this moves the cursor before the beginning. pub fn prev(&mut self) -> bool { unimplemented!() } /// Insert a `(key, value)` pair at the cursor position. /// It is an error to insert a key that would be out of order at this position. pub fn insert(&mut self, key: K, value: V) { unimplemented!() } /// Remove the current element. pub fn remove(&mut self) { unimplemented!() } }