Skip to content

Commit c41b0eb

Browse files
committed
Use reverse value -> index map for variables to improve
performance of INARRAY command, see #3
1 parent 76b8f99 commit c41b0eb

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

libchecktestdata.cc

+16-7
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ vector<command> program;
8888
// vector of the indices. Plain variables are stored using an index
8989
// vector of zero length.
9090
typedef map<vector<mpz_class>,value_t> indexmap;
91+
typedef map<value_t,set<vector<mpz_class>>> valuemap;
9192
map<string,indexmap> variable, preset;
93+
map<string,valuemap> rev_variable, rev_preset;
9294

9395
// List of loop starting commands like REP, initialized in checksyntax.
9496
set<string> loop_cmds;
@@ -276,11 +278,20 @@ void setvar(expr var, value_t val, int use_preset = 0)
276278
ind.push_back(mpz_class(eval(var.args[i])));
277279
}
278280

281+
map<string,indexmap> *varlist = &variable;
282+
map<string,valuemap> *revlist = &rev_variable;
279283
if ( use_preset ) {
280-
preset[var][ind] = val;
281-
} else {
282-
variable[var][ind] = val;
284+
varlist = &preset;
285+
revlist = &rev_preset;
286+
}
287+
288+
// Remove previously existing value -> index:
289+
if ( (*varlist)[var].count(ind) ) {
290+
(*revlist)[var][val].erase(ind);
291+
if ( (*revlist)[var][val].size()==0 ) (*revlist)[var].erase(val);
283292
}
293+
(*varlist)[var][ind] = val;
294+
(*revlist)[var][val].insert(ind);
284295
}
285296

286297
void setvars(vector<parse_t> varlist, int use_preset = 0)
@@ -294,6 +305,7 @@ void unsetvars(args_t varlist)
294305
{
295306
for(size_t i=0; i<varlist.size(); i++) {
296307
variable.erase(varlist[i].val);
308+
rev_variable.erase(varlist[i].val);
297309
}
298310
}
299311

@@ -578,10 +590,7 @@ bool inarray(expr e, expr array)
578590
exit(exit_failure);
579591
}
580592

581-
for(indexmap::iterator it=variable[var].begin(); it!=variable[var].end(); ++it) {
582-
if ( it->second==val ) return true;
583-
}
584-
return false;
593+
return rev_variable[var].count(val) && rev_variable[var][val].size()>0;
585594
}
586595

587596
bool dotest(test t)

0 commit comments

Comments
 (0)