Connect++ 0.5.0
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.
 
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.
 
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 &, const VariableIndex &)
 

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()
30, name_index()
31, next_index(0)
35, all_names_added(false)
37{}
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 * > 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 39 of file VariableIndex.cpp.

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

◆ 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 73 of file VariableIndex.cpp.

73 {
74 if (!all_names_added) {
76 }
77 Variable* new_var = nullptr;
78 // You can recycle an anonymous variable that was used
79 // earlier then freed up under backtracking.
81 new_var = vars[next_index++];
82 new_var->remove_substitution();
83 }
84 // You need to actually generate a new anonymous variable.
85 else {
87 string name("_");
88 name += to_string(next_index);
89 new_var = new Variable(next_index++, name);
90 vars.push_back(new_var);
91 }
92 return new_var;
93}
Basic representation of variables.
Definition Variable.hpp:58
void remove_substitution()
Self-explanatory, but be careful!
Definition Variable.hpp:128
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 125 of file VariableIndex.cpp.

125 {
126 backtrack_points.push_back(next_index);
127}

◆ 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 129 of file VariableIndex.cpp.

129 {
130 backtrack_points.push_back(next_index);
131 return next_index;
132}

◆ 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 44 of file VariableIndex.cpp.

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

◆ add_unique_var()

Variable * VariableIndex::add_unique_var ( )

Add a unique variable when converting to CNF.

Does not immediately deny addition of further named variables.

Definition at line 95 of file VariableIndex.cpp.

95 {
96 string name(params::unique_var_prefix);
97 name += to_string(next_unique_index);
99 return add_named_var(name);
100}
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 134 of file VariableIndex.cpp.

134 {
135 if (backtrack_points.size() > 0) {
137 backtrack_points.pop_back();
138 }
139 else
140 cerr << "There's nowhere left to backtrack to." << endl;
141}

◆ backtrack_to_named_point()

void VariableIndex::backtrack_to_named_point ( ID id)

Backtrack, possibly multiple times, to the specified point.

Definition at line 143 of file VariableIndex.cpp.

143 {
144 while (backtrack_points.back() != id) {
145 backtrack_points.pop_back();
146 }
147 next_index = id;
148 backtrack_points.pop_back();
149}

◆ clear_substitutions()

void VariableIndex::clear_substitutions ( )

Undo all substitutions made, to anything, ever.

Definition at line 68 of file VariableIndex.cpp.

68 {
69 for (size_t i = 0; i < vars.size(); i++)
70 vars[i]->remove_substitution();
71}

◆ 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 102 of file VariableIndex.cpp.

102 {
103 Variable* var = nullptr;
104 istringstream st(var_name);
105 char c;
106 st >> c;
107 if (c == '_') {
108 ID id;
109 st >> id;
110 if (id < next_index)
111 var = vars[id];
112 else
113 cerr << "That anonymous variable doesn't exist" << endl;
114 }
115 else {
116 auto i = name_index.find(var_name);
117 if (i != name_index.end())
118 var = i->second;
119 else
120 cerr << "That named variable doesn't exist" << endl;
121 }
122 return var;
123}

◆ get_all_names_added()

bool VariableIndex::get_all_names_added ( ) const
inline

Straightforward access function.

Definition at line 136 of file VariableIndex.hpp.

136{ return all_names_added; }

◆ get_first_anon_variable()

ID VariableIndex::get_first_anon_variable ( ) const
inline

Straightforward access function.

Definition at line 128 of file VariableIndex.hpp.

128{ return first_anon_variable; }

◆ get_highest_anon_variable()

ID VariableIndex::get_highest_anon_variable ( ) const
inline

Straightforward access function.

Definition at line 132 of file VariableIndex.hpp.

132{ return highest_anon_variable; }

◆ get_next_index()

ID VariableIndex::get_next_index ( ) const
inline

Straightforward access function.

Definition at line 124 of file VariableIndex.hpp.

124{ return next_index; }

◆ get_num_backtracks()

size_t VariableIndex::get_num_backtracks ( ) const
inline

Straightforward access function.

Definition at line 140 of file VariableIndex.hpp.

140{ return backtrack_points.size(); }

◆ reset()

void VariableIndex::reset ( )

Get rid of all the markers used for backtracking.

Clears all backtracking information.

Definition at line 151 of file VariableIndex.cpp.

151 {
152 if (all_names_added) {
153 backtrack_points.clear();
155 }
156}

◆ 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 63 of file VariableIndex.cpp.

63 {
64 all_names_added = true;
66}

Friends And Related Symbol Documentation

◆ operator<<

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

Definition at line 158 of file VariableIndex.cpp.

158 {
159 out << "Variable Index" << endl;
160 out << "--------------" << endl;
161 ID id = 0;
162 auto i = vi.backtrack_points.begin();
163 for (Variable* v : vi.vars) {
164 if (id == vi.next_index)
165 break;
166 out << *v << " ";
167 if (i != vi.backtrack_points.end()) {
168 if (id == *i) {
169 out << " <- Backtrack point";
170 i++;
171 }
172 }
173 out << endl;
174 id++;
175 }
176 out << "Highest anon id: " << vi.highest_anon_variable;
177 return out;
178}

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 102 of file VariableIndex.hpp.

◆ backtrack_points

vector<ID> VariableIndex::backtrack_points
private

Backtrack points stored as indices in the vars vector.

Definition at line 107 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 89 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 94 of file VariableIndex.hpp.

◆ name_index

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

Fast lookup of Variables by name.

Definition at line 70 of file VariableIndex.hpp.

◆ next_index

ID VariableIndex::next_index
private

Keep track of automatically generated Variable indices.

Definition at line 75 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.

Definition at line 81 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: