From: Ulya Trofimovich Date: Mon, 28 Mar 2016 18:59:23 +0000 (+0100) Subject: Use 'std::valarray' instead of 'std::vector' to avoid copy semantics. X-Git-Tag: 1.0~39^2~344 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7204740ab2e5c365ddcb9e60b59833560fed0298;p=re2c Use 'std::valarray' instead of 'std::vector' to avoid copy semantics. Had to rename 'INFINITY' to 'SCC_INF': inclusion of '' pulled '' and introduced name collision. --- diff --git a/re2c/src/ir/adfa/adfa.cc b/re2c/src/ir/adfa/adfa.cc index 16d59a98..414e1d6d 100644 --- a/re2c/src/ir/adfa/adfa.cc +++ b/re2c/src/ir/adfa/adfa.cc @@ -35,8 +35,8 @@ DFA::DFA , ubChar(charset.back()) , nStates(0) , head(NULL) - , contexts(dfa.contexts) , rules(dfa.rules) + , contexts(dfa.contexts) // statistics , max_fill (0) diff --git a/re2c/src/ir/adfa/adfa.h b/re2c/src/ir/adfa/adfa.h index 6e6fe970..f8279028 100644 --- a/re2c/src/ir/adfa/adfa.h +++ b/re2c/src/ir/adfa/adfa.h @@ -5,6 +5,7 @@ #include "src/util/c99_stdint.h" #include #include +#include #include "src/codegen/go.h" #include "src/codegen/label.h" @@ -67,8 +68,8 @@ public: uint32_t ubChar; uint32_t nStates; State * head; + std::valarray &rules; std::vector &contexts; - std::vector &rules; // statistics size_t max_fill; diff --git a/re2c/src/ir/dfa/determinization.cc b/re2c/src/ir/dfa/determinization.cc index e0c7dcb0..cad20b0c 100644 --- a/re2c/src/ir/dfa/determinization.cc +++ b/re2c/src/ir/dfa/determinization.cc @@ -94,8 +94,8 @@ dfa_t::dfa_t( const std::string &cond) : states() , nchars(charset.size() - 1) // (n + 1) bounds for n ranges - , contexts(nfa.contexts) , rules(nfa.rules) + , contexts(nfa.contexts) { std::map > s2rules; ord_hash_set_t kernels; diff --git a/re2c/src/ir/dfa/dfa.h b/re2c/src/ir/dfa/dfa.h index 71d99af4..494b43af 100644 --- a/re2c/src/ir/dfa/dfa.h +++ b/re2c/src/ir/dfa/dfa.h @@ -2,6 +2,7 @@ #define _RE2C_IR_DFA_DFA_ #include "src/util/c99_stdint.h" +#include #include #include @@ -40,8 +41,8 @@ struct dfa_t std::vector states; const size_t nchars; + std::valarray &rules; std::vector &contexts; - std::vector &rules; dfa_t(const nfa_t &nfa, const charset_t &charset, uint32_t line, const std::string &cond); diff --git a/re2c/src/ir/dfa/fillpoints.cc b/re2c/src/ir/dfa/fillpoints.cc index f4488ea7..9a0b8394 100644 --- a/re2c/src/ir/dfa/fillpoints.cc +++ b/re2c/src/ir/dfa/fillpoints.cc @@ -7,8 +7,8 @@ namespace re2c { -static const size_t INFINITY = std::numeric_limits::max(); -static const size_t UNDEFINED = INFINITY - 1; +static const size_t SCC_INF = std::numeric_limits::max(); +static const size_t SCC_UND = SCC_INF - 1; static bool loopback(size_t node, size_t narcs, const size_t *arcs) { @@ -37,13 +37,13 @@ static bool loopback(size_t node, size_t narcs, const size_t *arcs) * We use lowlink to hold different kinds of information: * - values in range [0 .. stack size] mean that this node is on stack * (link to a node with the smallest index reachable from this one) - * - UNDEFINED means that this node has not been visited yet - * - INFINITY means that this node has already been popped off stack + * - SCC_UND means that this node has not been visited yet + * - SCC_INF means that this node has already been popped off stack * * We use stack size (rather than topological sort index) as unique index * of a node on stack. This is safe because indices of nodes on stack are * still unique and less than indices of nodes that have been popped off - * stack (INFINITY). + * stack (SCC_INF). * */ static void scc( @@ -63,7 +63,7 @@ static void scc( const size_t j = arcs[c]; if (j != dfa_t::NIL) { - if (lowlink[j] == UNDEFINED) + if (lowlink[j] == SCC_UND) { scc(dfa, stack, lowlink, trivial, j); } @@ -87,7 +87,7 @@ static void scc( { j = stack.top(); stack.pop(); - lowlink[j] = INFINITY; + lowlink[j] = SCC_INF; } while (j != i); } @@ -99,7 +99,7 @@ static void calc_fill( std::vector &fill, size_t i) { - if (fill[i] == UNDEFINED) + if (fill[i] == SCC_UND) { fill[i] = 0; const size_t *arcs = dfa.states[i]->arcs; @@ -129,13 +129,13 @@ void fillpoints(const dfa_t &dfa, std::vector &fill) // find DFA states that belong to non-trivial SCC std::stack stack; - std::vector lowlink(size, UNDEFINED); + std::vector lowlink(size, SCC_UND); std::vector trivial(size, false); scc(dfa, stack, lowlink, trivial, 0); // for each DFA state, calculate YYFILL argument: // maximal path length to the next YYFILL state - fill.resize(size, UNDEFINED); + fill.resize(size, SCC_UND); calc_fill(dfa, trivial, fill, 0); // The following states must trigger YYFILL: diff --git a/re2c/src/ir/nfa/nfa.cc b/re2c/src/ir/nfa/nfa.cc index cb4500a9..b4646094 100644 --- a/re2c/src/ir/nfa/nfa.cc +++ b/re2c/src/ir/nfa/nfa.cc @@ -120,7 +120,8 @@ static bool nullable(const RegExp *re) static nfa_state_t *compile_rule( const RegExpRule *rule, - nfa_t &nfa) + nfa_t &nfa, + size_t nrule) { const std::vector &rs = rule->regexps; const std::vector &ctxnames = rule->ctxnames; @@ -149,10 +150,11 @@ static nfa_state_t *compile_rule( } } + Rule &r = nfa.rules[nrule]; + r.info = rule->info; + nfa_state_t *t = &nfa.states[nfa.size++]; - const size_t nrule = nfa.rules.size(); t->fin(nrule); - Rule r(rule->info); std::vector &ctxvar = r.ctxvar; std::vector &ctxfix = r.ctxfix; @@ -191,8 +193,6 @@ static nfa_state_t *compile_rule( } r.nullable = null; - nfa.rules.push_back(r); - return t; } @@ -203,10 +203,10 @@ static nfa_state_t *compile_rules( nfa_state_t *s = NULL; const size_t nrs = rs.size(); if (nrs > 0) { - s = compile_rule(rs[0], nfa); + s = compile_rule(rs[0], nfa, 0); for (size_t i = 1; i < nrs; ++i) { nfa_state_t *q = &nfa.states[nfa.size++]; - q->alt(s, compile_rule(rs[i], nfa)); + q->alt(s, compile_rule(rs[i], nfa, i)); s = q; } } @@ -217,7 +217,7 @@ nfa_t::nfa_t(const std::vector &rs) : max_size(calc_size_all(rs)) , size(0) , states(new nfa_state_t[max_size]) - , rules(*new std::vector) + , rules(*new std::valarray(rs.size())) , contexts(*new std::vector) , root(compile_rules(rs, *this)) {} diff --git a/re2c/src/ir/nfa/nfa.h b/re2c/src/ir/nfa/nfa.h index 916a4c80..daaedee1 100644 --- a/re2c/src/ir/nfa/nfa.h +++ b/re2c/src/ir/nfa/nfa.h @@ -3,6 +3,7 @@ #include #include "src/util/c99_stdint.h" +#include #include #include "src/ir/rule.h" @@ -82,7 +83,7 @@ struct nfa_t const size_t max_size; size_t size; nfa_state_t *states; - std::vector &rules; + std::valarray &rules; std::vector &contexts; nfa_state_t *root; diff --git a/re2c/src/ir/rule.h b/re2c/src/ir/rule.h index f2cb7907..9fb7ff7e 100644 --- a/re2c/src/ir/rule.h +++ b/re2c/src/ir/rule.h @@ -43,8 +43,8 @@ struct Rule std::set shadow; bool reachable; - explicit Rule(const RuleInfo *i) - : info(i) + Rule() + : info(NULL) , ctxvar() , ctxfix() , trail() @@ -52,28 +52,8 @@ struct Rule , shadow() , reachable(false) {} - Rule (const Rule &rule) - : info(rule.info) - , ctxvar(rule.ctxvar) - , ctxfix(rule.ctxfix) - , trail(rule.trail) - , nullable(rule.nullable) - , shadow(rule.shadow) - , reachable(rule.reachable) - {} - Rule& operator=(const Rule &rule) - { - info = rule.info; - ctxvar = rule.ctxvar; - ctxfix = rule.ctxfix; - trail = rule.trail; - nullable = rule.nullable; - shadow = rule.shadow; - reachable = rule.reachable; - return *this; - } -// FORBID_COPY(Rule); + FORBID_COPY(Rule); }; } // namespace re2c diff --git a/re2c/src/ir/skeleton/skeleton.h b/re2c/src/ir/skeleton/skeleton.h index cd840081..3690d631 100644 --- a/re2c/src/ir/skeleton/skeleton.h +++ b/re2c/src/ir/skeleton/skeleton.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -55,7 +56,7 @@ struct Skeleton Node *nodes; size_t sizeof_key; - std::vector &rules; + std::valarray &rules; const size_t defrule; std::vector &contexts; diff --git a/re2c/src/ir/skeleton/unreachable_nullable.cc b/re2c/src/ir/skeleton/unreachable_nullable.cc index 6e7bfcec..5bbb4951 100644 --- a/re2c/src/ir/skeleton/unreachable_nullable.cc +++ b/re2c/src/ir/skeleton/unreachable_nullable.cc @@ -44,7 +44,7 @@ void warn_unreachable_nullable_rules(const Skeleton &skel) std::vector > reachs(nnodes); calc_reachable(skel, loops, reachs, 0); - std::vector &rules = skel.rules; + std::valarray &rules = skel.rules; const size_t nrules = rules.size(); for (size_t i = 0; i < nnodes; ++i) {