1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! NestedMap is an experimental lockfree map that is not ready for produuction use yet.
//! No guarantees are made at the moment. Use at your own risk.

mod raw;

#[cfg(test)]
mod tests;

use raw::{Table, Bucket, Entry};
pub use raw::TableRef;
use crossbeam_epoch::Owned;
use std::hash::Hash;

pub struct NestedMap<K: Hash + Eq, V> {
    root: Table<K, V>,
}

impl<'a, K: 'a + Hash + Eq, V: 'a> NestedMap<K, V> {
    pub fn new() -> Self {
        Self {
            root: Table::empty(),
        }
    }

    #[inline]
    pub fn insert(&self, key: K, value: V) {
        let bucket = Owned::new(Bucket::Leaf(Entry { key, value }));
        self.root.insert(bucket);
    }

    #[inline]
    pub fn get(&'a self, key: &K) -> Option<TableRef<'a, K, V>> {
        self.root.get(key)
    }

    #[inline]
    pub fn remove(&self, key: &K) {
        self.root.remove(key);
    }
}

impl<'a, K: 'a + Hash + Eq, V: 'a> Default for NestedMap<K, V> {
    fn default() -> Self {
        Self::new()
    }
}