From 7f0bbcfef3b79dbf801a49f264d1cc41f9709eaa Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Wed, 17 Aug 2022 17:31:32 +0200 Subject: [PATCH] rulegen3:: faster get_roadsign --- rulegen3.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/rulegen3.cpp b/rulegen3.cpp index f03976ba..c572f77a 100644 --- a/rulegen3.cpp +++ b/rulegen3.cpp @@ -110,30 +110,33 @@ EX int get_roadsign(twalker what) { tail.push_back(t->c.spin(t->parent_dir)); t = t->move(t->parent_dir); } - map visited; - queue vqueue; + /* we reuse known_sides */ + vector vqueue; auto visit = [&] (tcell *c, int dir) { - if(visited.count(c)) return; - visited[c] = dir; - vqueue.push(c); + if(c->known_sides) return; + c->known_sides = dir + 1; + vqueue.push_back(c); }; visit(s, MYSTERY); - while(true) { - if(vqueue.empty()) throw hr_exception("vqueue empty"); - tcell *c = vqueue.front(); + for(int i=0;; i++) { + if(i == isize(vqueue)) throw hr_exception("vqueue empty"); + tcell *c = vqueue[i]; if(c == t) break; - vqueue.pop(); - for(int i=0; itype; i++) - if(c->move(i) && c->move(i)->dist <= dlimit) - visit(c->move(i), c->c.spin(i)); + for(int i=0; itype; i++) { + tcell *c1 = c->move(i); + if(c1 && c1->dist <= dlimit) + visit(c1, c->c.spin(i)); + if(c1 == t) break; + } } while(t != s) { add_road_shortcut(s, t); - int d = visited.at(t); + int d = t->known_sides-1; tail.push_back(t->dist - dlimit); tail.push_back(t->c.spin(d)); t = t->move(d); } + for(auto c: vqueue) c->known_sides = 0; reverse(tail.begin(), tail.end()); for(auto t: tail) result.push_back(t); if(roadsign_id.count(result)) return roadsign_id[result];