From bb6b18c8a8bfd58a575763b8344f1bbd637a4a3c Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Tue, 5 Mar 2019 15:50:51 +0000 Subject: [PATCH] Use C array instead of vector (of known size) to avoid iterator invalidation warnings. --- re2c/lib/regex_impl.h | 8 ++++---- re2c/src/dfa/determinization.cc | 5 +++-- re2c/src/dfa/determinization.h | 2 +- re2c/src/dfa/posix_precedence.h | 23 +++++++++-------------- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/re2c/lib/regex_impl.h b/re2c/lib/regex_impl.h index 11ba095b..f7419545 100644 --- a/re2c/lib/regex_impl.h +++ b/re2c/lib/regex_impl.h @@ -74,7 +74,7 @@ struct simctx_t int32_t *newprectbl; int32_t *oldprectbl; size_t oldprecdim; - std::vector histlevel; + histleaf_t *histlevel; std::vector sortcores; std::vector fincount; std::vector worklist; @@ -122,7 +122,7 @@ simctx_t::simctx_t(const nfa_t &nfa, size_t re_nsub, int flags) , newprectbl(NULL) , oldprectbl(NULL) , oldprecdim(0) - , histlevel() + , histlevel(NULL) , sortcores() , fincount() , worklist() @@ -152,7 +152,7 @@ simctx_t::simctx_t(const nfa_t &nfa, size_t re_nsub, int flags) if (!(flags & REG_LEFTMOST) && !(flags & REG_TRIE)) { newprectbl = new int32_t[ncores * ncores]; oldprectbl = new int32_t[ncores * ncores]; - histlevel.reserve(ncores); + histlevel = new histleaf_t[ncores]; sortcores.reserve(ncores); fincount.resize(ncores + 1); worklist.reserve(nstates); @@ -179,6 +179,7 @@ simctx_t::~simctx_t() if (!(flags & REG_LEFTMOST) && !(flags & REG_TRIE)) { delete[] newprectbl; delete[] oldprectbl; + delete[] histlevel; } } @@ -192,7 +193,6 @@ void init(simctx_t &ctx, const char *string) ctx.step = 0; ctx.rule = Rule::NONE; ctx.cursor = ctx.marker = string; - ctx.histlevel.clear(); ctx.sortcores.clear(); DASSERT(ctx.worklist.empty()); DASSERT(ctx.gor1_topsort.empty()); diff --git a/re2c/src/dfa/determinization.cc b/re2c/src/dfa/determinization.cc index e1a7a8c8..879db8c3 100644 --- a/re2c/src/dfa/determinization.cc +++ b/re2c/src/dfa/determinization.cc @@ -267,7 +267,7 @@ determ_context_t::determ_context_t(const opt_t *opts, Msg &msg , newprectbl(NULL) , oldprectbl(NULL) , oldprecdim(0) - , histlevel() + , histlevel(NULL) , sortcores() , fincount() , worklist() @@ -286,7 +286,7 @@ determ_context_t::determ_context_t(const opt_t *opts, Msg &msg if (SEMA == POSIX) { newprectbl = new prectable_t[ncores * ncores]; - histlevel.reserve(ncores); + histlevel = new histleaf_t[ncores]; sortcores.reserve(ncores); fincount.resize(ncores + 1); worklist.reserve(nstates); @@ -306,6 +306,7 @@ determ_context_t::~determ_context_t() { if (SEMA == POSIX) { delete[] newprectbl; + delete[] histlevel; } } diff --git a/re2c/src/dfa/determinization.h b/re2c/src/dfa/determinization.h index 55e12675..7f86015f 100644 --- a/re2c/src/dfa/determinization.h +++ b/re2c/src/dfa/determinization.h @@ -170,7 +170,7 @@ struct determ_context_t int32_t *newprectbl; const int32_t *oldprectbl; size_t oldprecdim; - std::vector histlevel; + histleaf_t *histlevel; std::vector sortcores; std::vector fincount; std::vector worklist; diff --git a/re2c/src/dfa/posix_precedence.h b/re2c/src/dfa/posix_precedence.h index 332257d2..a55d8542 100644 --- a/re2c/src/dfa/posix_precedence.h +++ b/re2c/src/dfa/posix_precedence.h @@ -159,12 +159,7 @@ void compute_prectable(ctx_t &ctx) std::vector &sortcores = ctx.sortcores; std::vector &fcount = ctx.fincount; std::vector &stack = ctx.worklist; - std::vector &level = ctx.histlevel; - std::vector::reverse_iterator li, lj, lk, le; - - level.clear(); - level.reserve(newdim); - sortcores.resize(newdim); + histleaf_t *level = ctx.histlevel, *li, *lj, *lk, *le = level; // Group core configurations by their history tree index, so that later // while traversing the tree we will know at once which configurations @@ -172,6 +167,7 @@ void compute_prectable(ctx_t &ctx) // requires additional memory, but is fast and conveniently creates an // array of boundaries in the sorted configuration array. uint32_t maxfin = 0; + sortcores.resize(newdim); for (typename ctx_t::cconfiter_t c = state.begin(), e = state.end(); c != e; ++c) { typename ctx_t::history_t::node_t &n = history.node(c->thist); if (n.finidx >= USED) { @@ -225,21 +221,20 @@ void compute_prectable(ctx_t &ctx) // all subtrees visited, it's time to process this node const int32_t h = n == 0 ? MAX_RHO : tags[node.info.idx].height; - li = level.rbegin(); - le = level.rend(); + li = level - 1; if (fidx < USED) { // this node has leaf configurations, add them to level for (uint32_t k = fcount[fidx], e = fcount[fidx + 1]; k < e; ++k) { const uint32_t j = sortcores[k]; const histleaf_t l = {j, state[j].origin, HROOT, h}; - level.push_back(l); + *level++ = l; } // compute precedence for newly added configurations const int32_t p0 = pack(h, 0); - for (lj = level.rbegin(); lj != li; ++lj) { - for (lk = lj; lk != li; ++lk) { + for (lj = level - 1; lj > li; --lj) { + for (lk = lj; lk > li; --lk) { const uint32_t cj = lj->coreid, ck = lk->coreid; const uint32_t oj = lj->origin, ok = lk->origin; const bool fork = n != 0 || oj == ok; @@ -270,13 +265,13 @@ void compute_prectable(ctx_t &ctx) a = arc.prev; // for all the items of this subtree - for (lk = li; li != le && li->hidx == arc.node; ++li) { + for (lk = li; li >= le && li->hidx == arc.node; --li) { // update height of each item coming from subtree li->height = std::min(li->height, h); // for all the level items to the right of this subtree - for (lj = level.rbegin(); lj != lk; ++lj) { + for (lj = level - 1; lj > lk; --lj) { const uint32_t ci = li->coreid, cj = lj->coreid; const uint32_t oi = li->origin, oj = lj->origin; @@ -311,7 +306,7 @@ void compute_prectable(ctx_t &ctx) // finally, downgrade tree index of all subtree items, making their // origins indistinguishable from each other for the previous level - for (lj = level.rbegin(); lj != li; ++lj) { + for (lj = level - 1; lj > li; --lj) { lj->hidx = n; } -- 2.50.1