From 0e4ae90f839d6ab1bfbd84327ee498a96390172d Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Sun, 13 Feb 2011 10:08:08 +1100 Subject: Added Merge method to SparseState to make it possible to detect significant changes in lexers which will require styling beyond the end of the current range. --- lexlib/SparseState.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'lexlib/SparseState.h') diff --git a/lexlib/SparseState.h b/lexlib/SparseState.h index c84fdc861..bd74ecef2 100644 --- a/lexlib/SparseState.h +++ b/lexlib/SparseState.h @@ -28,6 +28,7 @@ class SparseState { return (position == other.position) && (value == other.value); } }; + int positionFirst; typedef std::vector stateVector; stateVector states; @@ -37,6 +38,9 @@ class SparseState { } public: + SparseState(int positionFirst_=-1) { + positionFirst = positionFirst_; + } void Set(int position, T value) { Delete(position); if ((states.size() == 0) || (value != states[states.size()-1].value)) { @@ -69,6 +73,34 @@ public: size_t size() const { return states.size(); } + + // Returns true if Merge caused a significant change + bool Merge(const SparseState &other, int ignoreAfter) { + // Changes caused beyond ignoreAfter are not significant + Delete(ignoreAfter+1); + + bool different = true; + bool changed = false; + typename stateVector::iterator low = Find(other.positionFirst); + if (static_cast(states.end() - low) == other.states.size()) { + // Same number in other as after positionFirst in this + different = !std::equal(low, states.end(), other.states.begin()); + } + if (different) { + if (low != states.end()) { + states.erase(low, states.end()); + changed = true; + } + typename stateVector::const_iterator startOther = other.states.begin(); + if (!states.empty() && states.back().value == startOther->value) + startOther++; + if (startOther != other.states.end()) { + states.insert(states.end(), startOther, other.states.end()); + changed = true; + } + } + return changed; + } }; #ifdef SCI_NAMESPACE -- cgit v1.2.3