From e92a20e5fac3bb42936439363fd4c47a9c6237fe Mon Sep 17 00:00:00 2001 From: Justin Viiret Date: Mon, 4 Jan 2016 15:33:05 +1100 Subject: [PATCH] ComponentRepeat: remove firsts_cache, precalc code Firsts are easy to compute in ComponentRepeat::first() now. --- src/parser/ComponentRepeat.cpp | 44 +++++++++++++--------------------- src/parser/ComponentRepeat.h | 4 ---- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/parser/ComponentRepeat.cpp b/src/parser/ComponentRepeat.cpp index 670cee37..ff02703c 100644 --- a/src/parser/ComponentRepeat.cpp +++ b/src/parser/ComponentRepeat.cpp @@ -87,8 +87,7 @@ ComponentRepeat::ComponentRepeat(const ComponentRepeat &other) type(other.type), sub_comp(unique_ptr(other.sub_comp->clone())), m_min(other.m_min), m_max(other.m_max), m_firsts(other.m_firsts), m_lasts(other.m_lasts), - posFirst(other.posFirst), posLast(other.posLast), - firsts_cache(other.firsts_cache) {} + posFirst(other.posFirst), posLast(other.posLast) {} bool ComponentRepeat::empty() const { return m_min == 0 || sub_comp->empty(); @@ -175,14 +174,24 @@ void ComponentRepeat::notePositions(GlushkovBuildState &bs) { } recordPosBounds(posFirst, bs.getBuilder().numVertices()); - precalc_firsts(); /* ComponentRepeat requires firsts to be calculated ahead - * of time and cached due to expense */ + + // Each optional repeat has an epsilon at the end of its firsts list. + for (u32 i = m_min; i < m_firsts.size(); i++) { + m_firsts[i].push_back(GlushkovBuildState::POS_EPSILON); + } + } vector ComponentRepeat::first() const { - DEBUG_PRINTF("firsts = %s\n", dumpPositions(firsts_cache.begin(), - firsts_cache.end()).c_str()); - return firsts_cache; + if (!m_max) { + return {}; + } + + assert(!m_firsts.empty()); // notePositions should already have run + const vector &firsts = m_firsts.front(); + DEBUG_PRINTF("firsts = %s\n", + dumpPositions(begin(firsts), end(firsts)).c_str()); + return firsts; } void ComponentRepeat::buildFollowSet(GlushkovBuildState &bs, @@ -331,27 +340,6 @@ inf_check: } } -void ComponentRepeat::precalc_firsts() { - DEBUG_PRINTF("building firsts for {%u,%u} repeat with %s sub\n", m_min, - m_max, sub_comp->empty() ? "emptiable" : "non-emptiable"); - - /* For normal repeat, our optional repeats each have an epsilon at the end - * of their firsts lists. - */ - for (u32 i = m_min; i < m_firsts.size(); i++) { - m_firsts[i].push_back(GlushkovBuildState::POS_EPSILON); - } - - firsts_cache.clear(); - if (!m_max) { - return; - } - - assert(!m_firsts.empty()); // notePositions should already have run - const vector &f = m_firsts.front(); - firsts_cache.insert(firsts_cache.end(), f.begin(), f.end()); -} - static bool hasPositionFlags(const Component &c) { for (const auto &e : c.first()) { diff --git a/src/parser/ComponentRepeat.h b/src/parser/ComponentRepeat.h index b708e062..8905bfcf 100644 --- a/src/parser/ComponentRepeat.h +++ b/src/parser/ComponentRepeat.h @@ -120,8 +120,6 @@ public: enum RepeatType type; protected: - /** Called by \ref buildFollowSet to connect up the various repeats. */ - void precalc_firsts(); void postSubNotePositionHook(); void wireRepeats(GlushkovBuildState &bs); @@ -134,8 +132,6 @@ protected: Position posFirst; Position posLast; - std::vector firsts_cache; - ComponentRepeat(const ComponentRepeat &other); };