1
#![allow(dead_code)]
2

            
3
use std::hash::Hash;
4

            
5
use equivalent::Equivalent;
6
use merc_unsafety::AllocBlock;
7
use merc_unsafety::StablePointer;
8
use merc_unsafety::StablePointerSet;
9
use rustc_hash::FxBuildHasher;
10

            
11
use crate::ATermIndex;
12
use crate::SymbolRef;
13
use crate::storage::SharedTerm;
14

            
15
/// Storage for ATerms with a fixed number of arguments.
16
///
17
/// Should be the same layout as `SharedTerm`.
18
#[repr(C)]
19
#[derive(Hash, Eq, PartialEq)]
20
struct SharedTermFixed<const N: usize> {
21
    symbol: SymbolRef<'static>,
22
    args: [ATermIndex; N],
23
}
24

            
25
/// Storage for ATerms with a fixed number of arguments.
26
///
27
/// Should be the same layout as `SharedTerm`.
28
#[repr(C)]
29
#[derive(Hash, Eq, PartialEq)]
30
pub(crate) struct SharedTermInt {
31
    symbol: SymbolRef<'static>,
32
    args: [usize; 1],
33
}
34

            
35
pub(crate) struct ATermStorage {
36
    terms: StablePointerSet<SharedTerm>,
37

            
38
    int_terms: StablePointerSet<SharedTermFixed<1>, FxBuildHasher, AllocBlock<SharedTermFixed<1>, 1024>>,
39
}
40

            
41
impl ATermStorage {
42
533
    pub fn new() -> Self {
43
533
        Self {
44
533
            terms: StablePointerSet::new(),
45
533
            int_terms: StablePointerSet::with_capacity_in(1000, AllocBlock::new()),
46
533
        }
47
533
    }
48

            
49
    /// Returns the number of stored terms.
50
    pub fn len(&self) -> usize {
51
        self.int_terms.len() + self.terms.len()
52
    }
53

            
54
    pub fn retain<F>(&self, mut f: F)
55
    where
56
        F: FnMut(&StablePointer<SharedTerm>) -> bool,
57
    {
58
        // self.int_terms.retain(|term| f(term));
59
        self.terms.retain(|term| f(term));
60
    }
61

            
62
53794955
    pub unsafe fn insert_equiv_dst<'a, Q, C>(
63
53794955
        &self,
64
53794955
        value: &'a Q,
65
53794955
        length: usize,
66
53794955
        construct: C,
67
53794955
    ) -> (StablePointer<SharedTerm>, bool)
68
53794955
    where
69
53794955
        Q: Hash + Equivalent<SharedTerm>,
70
53794955
        C: Fn(*mut SharedTerm, &'a Q),
71
    {
72
53794955
        unsafe { self.terms.insert_equiv_dst(value, length, construct) }
73
53794955
    }
74
}