Connect++ 0.6.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
TermIndex Class Reference

Look after terms, using hash consing to avoid storing copies of terms. More...

#include <TermIndex.hpp>

Collaboration diagram for TermIndex:

Public Member Functions

 TermIndex (const TermIndex &)=delete
 Don't allow copying as this is a terrible idea.
 
 TermIndex (const TermIndex &&)=delete
 
TermIndexoperator= (const TermIndex &)=delete
 
TermIndexoperator= (const TermIndex &&)=delete
 
size_t get_size () const
 Basic get method.
 
Termadd_variable_term (Variable *)
 Self-explanatory: add a Term containing a variable to the index.
 
Termadd_function_term (Function *, const vector< Term * > &)
 Self-explanatory: add a Term containing a function to the index.
 
Termreplace_variable_in_term (Variable *, Variable *, Term *)
 Replace a variable in a term with an alternative, maintaining the structure of the TermIndex.
 
Termreplace_variable_in_term_with_term (Term *, Variable *, Term *)
 Minor variation on replace_variable_in_term.
 

Private Member Functions

Termfind (const Term &)
 Find a term in the index.
 

Private Attributes

unordered_map< Term, Term *, term_hashindex
 Hash table for storing pointers to where Terms are actually stored.
 
vector< Term * > term_pointers
 Used to make destruction easy.
 

Friends

ostream & operator<< (ostream &, const TermIndex &)
 

Detailed Description

Look after terms, using hash consing to avoid storing copies of terms.

Everything is implemented such that this ignores whether or not Variables are substituted. (Because to do otherwise isn't necessary.)

Using the unordered_map means allowing various kinds of copying of Terms, which I'd rather not do.

You should only use this to make Terms. As long as you do that then this class takes all the responsibility for memory allocation and deallocation for Terms.

Definition at line 54 of file TermIndex.hpp.

Constructor & Destructor Documentation

◆ TermIndex() [1/2]

TermIndex::TermIndex ( )
inline

Definition at line 75 of file TermIndex.hpp.

75: index(), term_pointers() {}
vector< Term * > term_pointers
Used to make destruction easy.
Definition TermIndex.hpp:67
unordered_map< Term, Term *, term_hash > index
Hash table for storing pointers to where Terms are actually stored.
Definition TermIndex.hpp:63

◆ ~TermIndex()

TermIndex::~TermIndex ( )

Definition at line 36 of file TermIndex.cpp.

36 {
37 for (size_t i = 0; i < term_pointers.size(); i++)
38 delete term_pointers[i];
39}

◆ TermIndex() [2/2]

TermIndex::TermIndex ( const TermIndex & )
delete

Don't allow copying as this is a terrible idea.

As usual, let the compiler be your friend.

Member Function Documentation

◆ add_function_term()

Term * TermIndex::add_function_term ( Function * fp,
const vector< Term * > & args )

Self-explanatory: add a Term containing a function to the index.

It's only actually added if it's not already present. If present, a pointer to the existing copy is returned.

Parameters
fpPointer to a Function for which an appropriate Term is to be added
argsVector of pointers to Terms constituting the arguments for the new function Term to be added.

Definition at line 56 of file TermIndex.cpp.

57 {
58 for (size_t i = 0; i < args.size(); i++) {
59 if (args[i]->is_subbed()) {
60 cerr << "Don't add substituted functions to the Term Index!" << endl;
61 }
62 }
63 Term t(fp, args);
64 Term* p = find(t);
65 if (p != nullptr) {
66 return p;
67 }
68 p = new Term(fp, args);
69 index.insert(pair<Term, Term*>(t, p));
70 term_pointers.push_back(p);
71 return p;
72}
General representation of terms.
Definition Term.hpp:62
Term * find(const Term &)
Find a term in the index.
Definition TermIndex.cpp:28

◆ add_variable_term()

Term * TermIndex::add_variable_term ( Variable * vp)

Self-explanatory: add a Term containing a variable to the index.

It's only actually added if it's not already present. If present, a pointer to the existing copy is returned.

Parameters
vpPointer to Variable for which an equivalent Term is to be added

Definition at line 41 of file TermIndex.cpp.

41 {
42 if (vp->is_subbed()) {
43 cerr << "Don't add substituted variables to the Term Index!" << endl;
44 }
45 Term t(vp);
46 Term* p = find(t);
47 if (p != nullptr) {
48 return p;
49 }
50 p = new Term(vp);
51 index.insert(pair<Term, Term*>(t, p));
52 term_pointers.push_back(p);
53 return p;
54}
bool is_subbed() const
Is this variable currently substituted?
Definition Variable.hpp:106

◆ find()

Term * TermIndex::find ( const Term & t)
private

Find a term in the index.

Parameters
tThe term you want to look up.

Definition at line 28 of file TermIndex.cpp.

28 {
29 auto i = index.find(t);
30 if (i != index.end()) {
31 return i->second;
32 }
33 return nullptr;
34}

◆ get_size()

size_t TermIndex::get_size ( ) const
inline

Basic get method.

Definition at line 89 of file TermIndex.hpp.

89{ return term_pointers.size(); }

◆ replace_variable_in_term()

Term * TermIndex::replace_variable_in_term ( Variable * new_v,
Variable * old_v,
Term * t )

Replace a variable in a term with an alternative, maintaining the structure of the TermIndex.

The first two arguments are variables and the third a general term. Replace one variable with another while keeping the structure of the index correct. Replacement replaces any substitutions for the variable being replaced with those applied to the new one. You should probably not do that as ideally the index should only contain unsubstituted variables.

Parameters
new_vPointer to a Variable to use as replacement.
old_vPointer to a Variable to be replaced.
tPointer to Term to make the replacement for. This should already be in the index.

Definition at line 74 of file TermIndex.cpp.

76 {
77 if (t->is_variable()) {
78 if (old_v == t->get_v()) {
79 return add_variable_term(new_v);
80 }
81 else
82 return t;
83 }
84 else {
85 Function* f = t->get_f();
86 vector<Term*> new_args;
87 for (size_t i = 0; i < t->arity(); i++)
88 new_args.push_back(replace_variable_in_term(new_v, old_v, (*t)[i]));
89 return add_function_term(f, new_args);
90 }
91}
Basic representation of functions.
Definition Function.hpp:54
Variable * get_v() const
Self-explanatory access function.
Definition Term.hpp:86
Function * get_f() const
Self-explanatory access function.
Definition Term.hpp:90
bool is_variable() const
Self-explanatory.
Definition Term.hpp:94
Arity arity() const
Self-explanatory access function.
Definition Term.hpp:102
Term * add_function_term(Function *, const vector< Term * > &)
Self-explanatory: add a Term containing a function to the index.
Definition TermIndex.cpp:56
Term * add_variable_term(Variable *)
Self-explanatory: add a Term containing a variable to the index.
Definition TermIndex.cpp:41
Term * replace_variable_in_term(Variable *, Variable *, Term *)
Replace a variable in a term with an alternative, maintaining the structure of the TermIndex.
Definition TermIndex.cpp:74

◆ replace_variable_in_term_with_term()

Term * TermIndex::replace_variable_in_term_with_term ( Term * new_t,
Variable * old_v,
Term * t )

Minor variation on replace_variable_in_term.

ONLY use this if the new Term is in the index!

Parameters
new_tPointer to a Term to use as replacement.
old_vPointer to a Variable to be replaced.
tPointer to Term to make the replacement for. This should already be in the index.

Definition at line 95 of file TermIndex.cpp.

97 {
98 if (t->is_variable()) {
99 if (old_v == t->get_v()) {
100 return new_t;
101 }
102 else
103 return t;
104 }
105 else {
106 Function* f = t->get_f();
107 vector<Term*> new_args;
108 for (size_t i = 0; i < t->arity(); i++)
109 new_args.push_back(replace_variable_in_term_with_term(new_t, old_v, (*t)[i]));
110 return add_function_term(f, new_args);
111 }
112}
Term * replace_variable_in_term_with_term(Term *, Variable *, Term *)
Minor variation on replace_variable_in_term.
Definition TermIndex.cpp:95

Friends And Related Symbol Documentation

◆ operator<<

ostream & operator<< ( ostream & out,
const TermIndex & t )
friend

Definition at line 114 of file TermIndex.cpp.

114 {
115 out << "Contents of term index:" << endl;
116 out << "-----------------------" << endl;
117 for (Term* p : t.term_pointers)
118 out << p << " : " << *p << endl;
119 return out;
120}

Member Data Documentation

◆ index

unordered_map<Term, Term*, term_hash> TermIndex::index
private

Hash table for storing pointers to where Terms are actually stored.

The key is the Term itself. term_hash is defined in TermHash.hpp.

Definition at line 63 of file TermIndex.hpp.

◆ term_pointers

vector<Term*> TermIndex::term_pointers
private

Used to make destruction easy.

Definition at line 67 of file TermIndex.hpp.


The documentation for this class was generated from the following files: