#![allow(unknown_lints)]
use interface;
use interface::Term;
use utils::insert_or_get;
use rc::ATerm;
use std::cell::UnsafeCell;
use std::rc::Rc;
use std::collections::HashSet;
use std::hash::Hash;
use std::marker::PhantomData;
#[derive(Default, Debug)]
pub struct ATermFactory<B: Hash + Eq> {
arena: UnsafeCell<HashSet<Rc<ATerm<B>>>>,
_nothing: PhantomData<B>,
}
impl<B: Hash + Eq> ATermFactory<B> {
pub fn new() -> Self {
ATermFactory {
arena: UnsafeCell::new(HashSet::new()),
_nothing: PhantomData,
}
}
pub fn with_capacity(capacity: usize) -> Self {
ATermFactory {
arena: UnsafeCell::new(HashSet::with_capacity(capacity)),
_nothing: PhantomData,
}
}
#[allow(mut_from_ref)]
fn get_arena(&self) -> &mut HashSet<Rc<ATerm<B>>> {
unsafe { &mut *self.arena.get() }
}
}
impl<B: Clone + Hash + Eq> interface::ATermFactory<B> for ATermFactory<B> {
type ATerm = ATerm<B>;
type ATermRef = Rc<ATerm<B>>;
fn no_annos(&self, term: Term<Rc<ATerm<B>>, B>) -> Self::ATermRef {
insert_or_get(self.get_arena(), Rc::new(ATerm::no_annos(term))).clone()
}
fn with_annos<A>(&self, term: Term<Rc<ATerm<B>>, B>, annos: A) -> Self::ATermRef
where A: IntoIterator<Item = Self::ATermRef>
{
insert_or_get(self.get_arena(), Rc::new(ATerm::with_annos(term, annos))).clone()
}
}
impl<B: Clone + Hash + Eq> interface::SharedATermFactory<B> for ATermFactory<B> {
fn get_shared(&self, value: Self::ATermRef) -> Self::ATermRef
where Self::ATermRef: Clone
{
insert_or_get(self.get_arena(), value.clone()).clone()
}
}