fetish_lib/
type_space.rs

1use crate::type_id::*;
2use crate::term::*;
3use crate::term_pointer::*;
4use crate::func_impl::*;
5use rand::Rng;
6use std::collections::HashMap;
7use crate::term_index::*;
8use crate::nonprimitive_term_pointer::*;
9
10///Directory of currently-known non-primitive [`PartiallyAppliedTerm`]s for a given
11///[`TypeId`] in the context of an [`crate::interpreter_state::InterpreterState`].
12pub struct TypeSpace {
13    my_type : TypeId,
14    terms : Vec<PartiallyAppliedTerm>,
15    term_to_index_map : HashMap::<PartiallyAppliedTerm, usize>
16}
17
18impl TypeSpace {
19    ///Constructs a new, initially-empty [`TypeSpace`] for the given
20    ///[`TypeId`]
21    pub fn new(id : TypeId) -> TypeSpace {
22        TypeSpace {
23            my_type : id,
24            terms : Vec::new(),
25            term_to_index_map : HashMap::new()
26        }
27    }
28
29    ///Gets the number of terms in this type-space
30    pub fn get_num_terms(&self) -> usize {
31        self.terms.len()
32    }
33    
34    ///Gets the term with the given index from this type-space
35    pub fn get(&self, term_index : usize) -> &PartiallyAppliedTerm {
36        &self.terms[term_index]
37    }
38
39    ///Adds a given term to this type-space if it doesn't
40    ///already exist in that space, otherwise returns a reference
41    ///to the previously-added term
42    pub fn add(&mut self, term : PartiallyAppliedTerm) -> NonPrimitiveTermPointer {
43        if (self.term_to_index_map.contains_key(&term)) {
44            let index : usize = *(self.term_to_index_map.get(&term).unwrap());
45            NonPrimitiveTermPointer {
46                type_id : self.my_type.clone(),
47                index
48            }
49        } else {
50            let new_ind : usize = self.terms.len();
51            self.terms.push(term.clone());
52            self.term_to_index_map.insert(term, new_ind);
53            NonPrimitiveTermPointer {
54                type_id : self.my_type,
55                index : new_ind
56            }
57        }
58    }
59}