Debian Codegraph @

scintilla/lexilla/lexlib/SparseState.h

raw
1 // Scintilla source code edit control
2 /** @file SparseState.h
3 ** Hold lexer state that may change rarely.
4 ** This is often per-line state such as whether a particular type of section has been entered.
5 ** A state continues until it is changed.
6 **/
7 // Copyright 2011 by Neil Hodgson <[email protected]>
8 // The License.txt file describes the conditions under which this software may be distributed.
9
10 #ifndef SPARSESTATE_H
11 #define SPARSESTATE_H
12
13 namespace Lexilla {
14
15 template <typename T>
16 class SparseState {
17 struct State {
18 Sci_Position position;
19 T value;
20 constexpr State(Sci_Position position_, T value_) noexcept :
21 position(position_), value(std::move(value_)) {
22 }
23 inline bool operator<(const State &other) const noexcept {
24 return position < other.position;
25 }
26 inline bool operator==(const State &other) const noexcept {
27 return (position == other.position) && (value == other.value);
28 }
29 };
30 Sci_Position positionFirst;
31 typedef std::vector<State> stateVector;
32 stateVector states;
33
34 typename stateVector::iterator Find(Sci_Position position) {
35 const State searchValue(position, T());
36 return std::lower_bound(states.begin(), states.end(), searchValue);
37 }
38
39 public:
40 explicit SparseState(Sci_Position positionFirst_=-1) {
41 positionFirst = positionFirst_;
42 }
43 void Set(Sci_Position position, T value) {
44 Delete(position);
45 if (states.empty() || (value != states[states.size()-1].value)) {
46 states.push_back(State(position, value));
47 }
48 }
49 T ValueAt(Sci_Position position) {
50 if (states.empty())
51 return T();
52 if (position < states[0].position)
53 return T();
54 typename stateVector::iterator low = Find(position);
55 if (low == states.end()) {
56 return states[states.size()-1].value;
57 } else {
58 if (low->position > position) {
59 --low;
60 }
61 return low->value;
62 }
63 }
64 bool Delete(Sci_Position position) {
65 typename stateVector::iterator low = Find(position);
66 if (low != states.end()) {
67 states.erase(low, states.end());
68 return true;
69 }
70 return false;
71 }
72 size_t size() const {
73 return states.size();
74 }
75
76 // Returns true if Merge caused a significant change
77 bool Merge(const SparseState<T> &other, Sci_Position ignoreAfter) {
78 // Changes caused beyond ignoreAfter are not significant
79 Delete(ignoreAfter+1);
80
81 bool different = true;
82 bool changed = false;
83 typename stateVector::iterator low = Find(other.positionFirst);
84 if (static_cast<size_t>(states.end() - low) == other.states.size()) {
85 // Same number in other as after positionFirst in this
86 different = !std::equal(low, states.end(), other.states.begin());
87 }
88 if (different) {
89 if (low != states.end()) {
90 states.erase(low, states.end());
91 changed = true;
92 }
93 typename stateVector::const_iterator startOther = other.states.begin();
94 if (!states.empty() && !other.states.empty() && states.back().value == startOther->value)
95 ++startOther;
96 if (startOther != other.states.end()) {
97 states.insert(states.end(), startOther, other.states.end());
98 changed = true;
99 }
100 }
101 return changed;
102 }
103 };
104
105 }
106
107 #endif