]> granicus.if.org Git - re2c/commitdiff
Use C array instead of vector (of known size) to avoid iterator invalidation warnings.
authorUlya Trofimovich <skvadrik@gmail.com>
Tue, 5 Mar 2019 15:50:51 +0000 (15:50 +0000)
committerUlya Trofimovich <skvadrik@gmail.com>
Tue, 5 Mar 2019 15:50:51 +0000 (15:50 +0000)
re2c/lib/regex_impl.h
re2c/src/dfa/determinization.cc
re2c/src/dfa/determinization.h
re2c/src/dfa/posix_precedence.h

index 11ba095b8f6cb5dfee0566c9a8d698382cdc7b13..f74195453352b52d5e786a307b15a24d866dbc6a 100644 (file)
@@ -74,7 +74,7 @@ struct simctx_t
     int32_t *newprectbl;
     int32_t *oldprectbl;
     size_t oldprecdim;
-    std::vector<histleaf_t> histlevel;
+    histleaf_t *histlevel;
     std::vector<uint32_t> sortcores;
     std::vector<uint32_t> fincount;
     std::vector<int32_t> worklist;
@@ -122,7 +122,7 @@ simctx_t<SEMA, EVAL>::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<SEMA, EVAL>::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<SEMA, EVAL>::~simctx_t()
     if (!(flags & REG_LEFTMOST) && !(flags & REG_TRIE)) {
         delete[] newprectbl;
         delete[] oldprectbl;
+        delete[] histlevel;
     }
 }
 
@@ -192,7 +193,6 @@ void init(simctx_t<SEMA, EVAL> &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());
index e1a7a8c862c6f2b1ac346758a2f79479baff9f5e..879db8c3f97d881127ddae06e9cccd3491b76b77 100644 (file)
@@ -267,7 +267,7 @@ determ_context_t<SEMA>::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<SEMA>::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<SEMA>::~determ_context_t()
 {
     if (SEMA == POSIX) {
         delete[] newprectbl;
+        delete[] histlevel;
     }
 }
 
index 55e12675b5420213366981c0f4eb88127f11528b..7f86015fad6502b3432762dc4868790a693296d1 100644 (file)
@@ -170,7 +170,7 @@ struct determ_context_t
     int32_t *newprectbl;
     const int32_t *oldprectbl;
     size_t oldprecdim;
-    std::vector<histleaf_t> histlevel;
+    histleaf_t *histlevel;
     std::vector<uint32_t> sortcores;
     std::vector<uint32_t> fincount;
     std::vector<int32_t> worklist;
index 332257d22a06a21a87c0c47952a69e40c8881aee..a55d8542c7e8163897865b51ff4a6b8e4a94e507 100644 (file)
@@ -159,12 +159,7 @@ void compute_prectable(ctx_t &ctx)
     std::vector<uint32_t> &sortcores = ctx.sortcores;
     std::vector<uint32_t> &fcount = ctx.fincount;
     std::vector<int32_t> &stack = ctx.worklist;
-    std::vector<histleaf_t> &level = ctx.histlevel;
-    std::vector<histleaf_t>::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;
         }