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

Storage of named variables, and management of new, anonymous and unique variables. More...

#include <VariableIndex.hpp>

Collaboration diagram for VariableIndex:

Public Member Functions

 VariableIndex (const VariableIndex &)=delete
 Copying this would be a very silly idea, so explicitly disallow it.
 
 VariableIndex (const VariableIndex &&)=delete
 
VariableIndexoperator= (const VariableIndex &)=delete
 
VariableIndexoperator= (const VariableIndex &&)=delete
 
ID get_next_index () const
 Straightforward access function.
 
ID get_first_anon_variable () const
 Straightforward access function.
 
ID get_highest_anon_variable () const
 Straightforward access function.
 
bool get_all_names_added () const
 Straightforward access function.
 
size_t get_num_backtracks () const
 Straightforward access function.
 
Variableadd_named_var (const string &)
 Add a variable with the specified name to the index.
 
Variableadd_anon_var ()
 Add an anonymous variable.
 
Variableadd_unique_var ()
 Add a unique variable when converting to CNF.
 
Variableadd_cached_var ()
 Cached variables are dealt with separately as there's no need to keep track of them like the others.
 
Variablefind_variable (const string &)
 Look up a variable by name.
 
void set_all_names_added ()
 Call this to indicate that only anonymous variables can now be created.
 
void clear_substitutions ()
 Undo all substitutions made, to anything, ever.
 
void add_backtrack_point ()
 Add a backtrack point.
 
ID add_named_backtrack_point ()
 Same as add_backtrack_point, but return an identifier allowing multiple levels of backtracking using backtrack_to_named_point.
 
void backtrack ()
 Backtrack to the last marker.
 
void backtrack_to_named_point (ID)
 Backtrack, possibly multiple times, to the specified point.
 
void reset ()
 Get rid of all the markers used for backtracking.
 

Private Attributes

vector< Variable * > vars
 All the variables made, named, unique and anonymous.
 
vector< Variable * > cached_vars
 All the cached variables made.
 
unordered_map< string, Variable * > name_index
 Fast lookup of Variables by name.
 
ID next_index
 Keep track of automatically generated Variable indices.
 
ID next_unique_index
 When converting to CNF you need new, unique variables, but they need to be treated separately from anonymous variables.
 
ID first_anon_variable
 Set after you've added all the named variables for the problem.
 
ID highest_anon_variable
 Keep track of how many anonymous Variables you currently have available.
 
bool all_names_added
 Set after you've added all the named variables for the problem.
 
vector< ID > backtrack_points
 Backtrack points stored as indices in the vars vector.
 

Friends

ostream & operator<< (ostream &out, const VariableIndex &vi)
 

Detailed Description

Storage of named variables, and management of new, anonymous and unique variables.

Variables are only stored once, and I want backtracking to be easy while recycling old anonymous variables. You should only add named variables once, at the beginning of the process.

You should only make variables using this index. If you do then it takes care of allocation/deallocation for you.

Definition at line 61 of file VariableIndex.hpp.

Constructor & Destructor Documentation

◆ VariableIndex() [1/2]

VariableIndex::VariableIndex ( )

Definition at line 28 of file VariableIndex.cpp.

29: vars()
31, name_index()
32, next_index(0)
36, all_names_added(false)
38{}
ID next_unique_index
When converting to CNF you need new, unique variables, but they need to be treated separately from an...
vector< ID > backtrack_points
Backtrack points stored as indices in the vars vector.
ID next_index
Keep track of automatically generated Variable indices.
ID first_anon_variable
Set after you've added all the named variables for the problem.
ID highest_anon_variable
Keep track of how many anonymous Variables you currently have available.
vector< Variable * > cached_vars
All the cached variables made.
vector< Variable * > vars
All the variables made, named, unique and anonymous.
unordered_map< string, Variable * > name_index
Fast lookup of Variables by name.
bool all_names_added
Set after you've added all the named variables for the problem.

◆ ~VariableIndex()

VariableIndex::~VariableIndex ( )

Definition at line 40 of file VariableIndex.cpp.

40 {
41 for (size_t i = 0; i < vars.size(); i++)
42 delete vars[i];
43 for (size_t i = 0; i < cached_vars.size(); i++)
44 delete cached_vars[i];
45}

◆ VariableIndex() [2/2]

VariableIndex::VariableIndex ( const VariableIndex & )
delete

Copying this would be a very silly idea, so explicitly disallow it.

As usual, let the compiler be your friend.

Member Function Documentation

◆ add_anon_var()

Variable * VariableIndex::add_anon_var ( )

Add an anonymous variable.

Anonymous variables are recycled after backtracking. Also denies addition of new named variables if you didn't do that yourself. Anonymous variables do get names of the form "_next_index" but are not currently included in the map as they can be found by other means if you absolutely need to, which you shouldn't.

Definition at line 76 of file VariableIndex.cpp.

76 {
77 if (!all_names_added) {
79 }
80 Variable* new_var = nullptr;
81 // You can recycle an anonymous variable that was used
82 // earlier then freed up under backtracking.
84 new_var = vars[next_index++];
85 new_var->remove_substitution();
86 }
87 // You need to actually generate a new anonymous variable.
88 else {
90 string name("_");
91 name += to_string(next_index);
92 new_var = new Variable(next_index++, name);
93 vars.push_back(new_var);
94 }
95 return new_var;
96}
Basic representation of variables.
Definition Variable.hpp:58
void remove_substitution()
Self-explanatory, but be careful!
Definition Variable.hpp:121
void set_all_names_added()
Call this to indicate that only anonymous variables can now be created.

◆ add_backtrack_point()

void VariableIndex::add_backtrack_point ( )

Add a backtrack point.

This is straightforward. All we have to do is remember a starting point so that all anonymous variables after that can easily be reused.

It is the responsibility of the user to insert and maintain an initial backtrack point at the first anonymous variable.

Definition at line 138 of file VariableIndex.cpp.

138 {
139 backtrack_points.push_back(next_index);
140}

◆ add_cached_var()

Variable * VariableIndex::add_cached_var ( )

Cached variables are dealt with separately as there's no need to keep track of them like the others.

Definition at line 105 of file VariableIndex.cpp.

105 {
106 string name(params::unique_var_prefix);
107 name += to_string(next_unique_index);
109 Variable* new_var = new Variable(0, name);
110 cached_vars.push_back(new_var);
111 name_index.insert(MapType(name, new_var));
112 return new_var;
113}

◆ add_named_backtrack_point()

ID VariableIndex::add_named_backtrack_point ( )

Same as add_backtrack_point, but return an identifier allowing multiple levels of backtracking using backtrack_to_named_point.

Definition at line 142 of file VariableIndex.cpp.

142 {
143 backtrack_points.push_back(next_index);
144 return next_index;
145}

◆ add_named_var()

Variable * VariableIndex::add_named_var ( const string & name)

Add a variable with the specified name to the index.

Only allow a named variable to be added once, and before any anonymous variables. If you add something twice you'll just get a pointer to the first copy.

Parameters
nameName of Variable to add.

Definition at line 47 of file VariableIndex.cpp.

47 {
48 Variable* new_var = nullptr;
49 if (!all_names_added) {
50 auto i = name_index.find(name);
51 if (i == name_index.end()) {
52 new_var = new Variable(next_index++, name);
53 vars.push_back(new_var);
54 name_index.insert(MapType(name, new_var));
55 }
56 else {
57 new_var = i->second;
58 }
59 }
60 else {
61 cerr << "You can't add any more named variables." << endl;
62 }
63 return new_var;
64}

◆ add_unique_var()

Variable * VariableIndex::add_unique_var ( )

Add a unique variable when converting to CNF.

These are also used to cache copies of clauses.

Does not immediately deny addition of further named variables.

Definition at line 98 of file VariableIndex.cpp.

98 {
99 string name(params::unique_var_prefix);
100 name += to_string(next_unique_index);
102 return add_named_var(name);
103}
Variable * add_named_var(const string &)
Add a variable with the specified name to the index.

◆ backtrack()

void VariableIndex::backtrack ( )

Backtrack to the last marker.

This is straightforward. Substitutions do not have to be undone here as that will happen if a variable is reused.

Definition at line 147 of file VariableIndex.cpp.

147 {
148 if (backtrack_points.size() > 0) {
150 backtrack_points.pop_back();
151 }
152 else
153 cerr << "There's nowhere left to backtrack to." << endl;
154}

◆ backtrack_to_named_point()

void VariableIndex::backtrack_to_named_point ( ID id)

Backtrack, possibly multiple times, to the specified point.

Definition at line 156 of file VariableIndex.cpp.

156 {
157 while (backtrack_points.back() != id) {
158 backtrack_points.pop_back();
159 }
160 next_index = id;
161 backtrack_points.pop_back();
162}

◆ clear_substitutions()

void VariableIndex::clear_substitutions ( )

Undo all substitutions made, to anything, ever.

Definition at line 71 of file VariableIndex.cpp.

71 {
72 for (size_t i = 0; i < vars.size(); i++)
73 vars[i]->remove_substitution();
74}

◆ find_variable()

Variable * VariableIndex::find_variable ( const string & var_name)

Look up a variable by name.

Parameters
var_nameName of Variable to find.

Definition at line 115 of file VariableIndex.cpp.

115 {
116 Variable* var = nullptr;
117 istringstream st(var_name);
118 char c;
119 st >> c;
120 if (c == '_') {
121 ID id;
122 st >> id;
123 if (id < next_index)
124 var = vars[id];
125 else
126 cerr << "That anonymous variable doesn't exist" << endl;
127 }
128 else {
129 auto i = name_index.find(var_name);
130 if (i != name_index.end())
131 var = i->second;
132 else
133 cerr << "That named variable doesn't exist" << endl;
134 }
135 return var;
136}

◆ get_all_names_added()

bool VariableIndex::get_all_names_added ( ) const
inline

Straightforward access function.

Definition at line 142 of file VariableIndex.hpp.

142{ return all_names_added; }

◆ get_first_anon_variable()

ID VariableIndex::get_first_anon_variable ( ) const
inline

Straightforward access function.

Definition at line 134 of file VariableIndex.hpp.

134{ return first_anon_variable; }

◆ get_highest_anon_variable()

ID VariableIndex::get_highest_anon_variable ( ) const
inline

Straightforward access function.

Definition at line 138 of file VariableIndex.hpp.

138{ return highest_anon_variable; }

◆ get_next_index()

ID VariableIndex::get_next_index ( ) const
inline

Straightforward access function.

Definition at line 130 of file VariableIndex.hpp.

130{ return next_index; }

◆ get_num_backtracks()

size_t VariableIndex::get_num_backtracks ( ) const
inline

Straightforward access function.

Definition at line 146 of file VariableIndex.hpp.

146{ return backtrack_points.size(); }

◆ reset()

void VariableIndex::reset ( )

Get rid of all the markers used for backtracking.

Clears all backtracking information.

Definition at line 164 of file VariableIndex.cpp.

164 {
165 if (all_names_added) {
166 backtrack_points.clear();
168 }
169}

◆ set_all_names_added()

void VariableIndex::set_all_names_added ( )

Call this to indicate that only anonymous variables can now be created.

Note: this also adds a backtrack point at the first (currently not created) anonymous variable.

Definition at line 66 of file VariableIndex.cpp.

66 {
67 all_names_added = true;
69}

Friends And Related Symbol Documentation

◆ operator<<

ostream & operator<< ( ostream & out,
const VariableIndex & vi )
friend

Definition at line 171 of file VariableIndex.cpp.

171 {
172 out << "Variable Index" << endl;
173 out << "--------------" << endl;
174 ID id = 0;
175 auto i = vi.backtrack_points.begin();
176 for (Variable* v : vi.vars) {
177 if (id == vi.next_index)
178 break;
179 out << *v << " ";
180 if (i != vi.backtrack_points.end()) {
181 if (id == *i) {
182 out << " <- Backtrack point";
183 i++;
184 }
185 }
186 out << endl;
187 id++;
188 }
189 out << "Highest anon id: " << vi.highest_anon_variable;
190 return out;
191}

Member Data Documentation

◆ all_names_added

bool VariableIndex::all_names_added
private

Set after you've added all the named variables for the problem.

Used to detect erronious addition of further named Variables.

Definition at line 108 of file VariableIndex.hpp.

◆ backtrack_points

vector<ID> VariableIndex::backtrack_points
private

Backtrack points stored as indices in the vars vector.

Definition at line 113 of file VariableIndex.hpp.

◆ cached_vars

vector<Variable*> VariableIndex::cached_vars
private

All the cached variables made.

Definition at line 70 of file VariableIndex.hpp.

◆ first_anon_variable

ID VariableIndex::first_anon_variable
private

Set after you've added all the named variables for the problem.

Allows you to know where in vars the problem variables stop and the anonymous variables start.

Definition at line 95 of file VariableIndex.hpp.

◆ highest_anon_variable

ID VariableIndex::highest_anon_variable
private

Keep track of how many anonymous Variables you currently have available.

Definition at line 100 of file VariableIndex.hpp.

◆ name_index

unordered_map<string, Variable*> VariableIndex::name_index
private

Fast lookup of Variables by name.

Definition at line 74 of file VariableIndex.hpp.

◆ next_index

ID VariableIndex::next_index
private

Keep track of automatically generated Variable indices.

Definition at line 79 of file VariableIndex.hpp.

◆ next_unique_index

ID VariableIndex::next_unique_index
private

When converting to CNF you need new, unique variables, but they need to be treated separately from anonymous variables.

These are also used to cache copies of clauses.

Definition at line 87 of file VariableIndex.hpp.

◆ vars

vector<Variable*> VariableIndex::vars
private

All the variables made, named, unique and anonymous.

Definition at line 66 of file VariableIndex.hpp.


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