Skip to content

Commit 30c0db7

Browse files
committed
faster vector, better loops
1 parent d232b58 commit 30c0db7

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/nfa/operations.cc

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <algorithm>
55
#include <list>
6+
#include <locale>
67
#include <unordered_set>
78
#include <iterator>
89
#include <unordered_map>
@@ -85,7 +86,7 @@ namespace {
8586
result_sim_tmp[i].resize(no_states, true);
8687
}
8788

88-
// TODO this is very memory inefficient
89+
// This is very memory inefficient
8990
for (size_t x = 0; x < alph_syms.size(); x++){ // Indexing every Symbol with an number
9091
if (index_map.size() <= alph_syms[x]){
9192
index_map.resize(alph_syms[x] + 1 ,0);
@@ -106,25 +107,40 @@ namespace {
106107
result_sim_tmp[p][q] = false;
107108
}
108109
}
109-
for (SymbolPost symbol_q : aut.delta[q]) {
110+
111+
auto symbol_q = aut.delta[q].begin();
112+
auto sym_end = aut.delta[q].end();
113+
for (size_t x = 0; x < alph_syms.size(); x++) {
114+
if (symbol_q == sym_end){ // If we searched all symbols
115+
break;
116+
}
117+
110118
size_t q_size;
111-
Symbol active_sym = symbol_q.symbol; // Get the active symbol
119+
Symbol active_sym = (*symbol_q).symbol; // Get the active symbol
112120
usage_map[index_map[active_sym]] = true; // Mark the symbol as used
113121

114-
q_size = symbol_q.num_of_targets(); // Compute lenght and store it
122+
q_size = (*symbol_q).num_of_targets(); // Compute lenght and store it
115123
matrix[index_fn(index_map[active_sym], p, q, alph_syms.size(), no_states)] = q_size;
124+
std::advance(symbol_q, 1);
116125
}
117-
for(SymbolPost active_sym : aut.delta[p]){
118-
bool is_present = usage_map[index_map[active_sym.symbol]]; // get the index of the symbol
126+
127+
auto active_sym = aut.delta[p].begin();
128+
sym_end = aut.delta[p].end();
129+
for(size_t x = 0; x < alph_syms.size(); x++){
130+
if (active_sym == sym_end){ // If we searched all symbols
131+
break;
132+
}
133+
bool is_present = usage_map[index_map[(*active_sym).symbol]]; // get the index of the symbol
119134
if (is_present == false){
120135
if (result_sim_tmp[p][q] != false) {
121136
worklist.push_back(std::pair(p,q)); // worklist append
122137
result_sim_tmp[p][q] = false;
123138
}
124139
}
140+
std::advance(active_sym, 1);
125141
}
126-
usage_map.clear(); // Reset the usage map
127-
usage_map.resize(alph_syms.size(), false);
142+
143+
std::fill(usage_map.begin(), usage_map.end(), false);
128144
}
129145
}
130146
// ! End of initial refinement
@@ -136,9 +152,14 @@ namespace {
136152
working_pair = worklist[worklist_size - 1];
137153
worklist.pop_back();
138154

139-
for (SymbolPost symbol_q_ : reverted_nfa.delta[working_pair.second]) {
140-
Symbol active_sym = symbol_q_.symbol;
141-
for (State q: symbol_q_.targets.to_vector()) {
155+
auto symbol_q_ = reverted_nfa.delta[working_pair.second].begin();
156+
auto sym_end = reverted_nfa.delta[working_pair.second].end();
157+
for (size_t x = 0; x < alph_syms.size(); x++) {
158+
if (symbol_q_ == sym_end){ // If we searched all symbols
159+
break;
160+
}
161+
Symbol active_sym = (*symbol_q_).symbol;
162+
for (State q: (*symbol_q_).targets.to_vector()) {
142163
if (--matrix[index_fn(index_map[active_sym], working_pair.first, q, alph_syms.size(), no_states)] == 0) {
143164
auto symbol_p_ = reverted_nfa.delta[working_pair.first].find(active_sym);
144165
if (symbol_p_ == reverted_nfa.delta[working_pair.first].end()) {
@@ -152,6 +173,7 @@ namespace {
152173
}
153174
}
154175
}
176+
std::advance(symbol_q_, 1);
155177
}
156178
}
157179
// ! End of Propagate until fixpoint

0 commit comments

Comments
 (0)