btree_plus_store/
store.rs

1use crate::node::{Node, NodePtr};
2use rustc_arena_modified::SlabArena;
3
4/// Arena to store nodes from multiple b-trees.
5pub struct BTreeStore<K, V> {
6    pub(crate) nodes: SlabArena<Node<K, V>>,
7}
8
9impl<K, V> BTreeStore<K, V> {
10    #[inline]
11    pub fn new() -> Self {
12        Self {
13            nodes: SlabArena::new(),
14        }
15    }
16
17    #[inline]
18    pub(crate) fn alloc(&self, node: Node<K, V>) -> NodePtr<K, V> {
19        self.nodes.alloc(node).into_unsafe()
20    }
21
22    #[inline]
23    pub(crate) fn dealloc(&self, node: NodePtr<K, V>) {
24        unsafe { node.discard(&self.nodes) }
25    }
26
27    #[allow(unused)]
28    #[inline]
29    pub(crate) fn dealloc_and_return(&self, node: NodePtr<K, V>) -> Node<K, V> {
30        unsafe { node.take(&self.nodes) }
31    }
32
33    #[allow(unused)]
34    #[inline]
35    pub(crate) unsafe fn retain_shared<F>(&self, mut f: F)
36    where
37        F: FnMut(&Node<K, V>) -> bool,
38    {
39        self.nodes.retain_shared(|node| f(node))
40    }
41}
42
43impl<K, V> Default for BTreeStore<K, V> {
44    #[inline]
45    fn default() -> Self {
46        Self::new()
47    }
48}