aboutsummaryrefslogtreecommitdiffhomepage
path: root/lexlib/SparseState.h
blob: 953800a446b46ebf142711aa4dc887acdc37042b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Scintilla source code edit control
/** @file SparseState.h
 ** Hold lexer state that may change rarely.
 ** This is often per-line state such as whether a particular type of section has been entered.
 ** A state continues until it is changed.
 **/
// Copyright 2011 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.

#ifndef SPARSESTATE_H
#define SPARSESTATE_H

#ifdef SCI_NAMESPACE
namespace Scintilla {
#endif

template <typename T>
class SparseState {
	struct State {
		int position;
		T value;
		State(int position_, T value_) : position(position_), value(value_) {
		}
		inline bool operator<(const State &other) const {
			return position < other.position;
		}
	};
	typedef std::vector<State> stateVector;
	stateVector states;
public:
	void Set(int position, T value) {
		Delete(position);
		if ((states.size() == 0) || (value != states[states.size()-1].value)) {
			states.push_back(State(position, value));
		}
	}
	T ValueAt(int position) {
		if (!states.size())
			return T();
		if (position < states[0].position)
			return T();
		State searchValue(position, T());
		typename stateVector::iterator low =
			lower_bound(states.begin(), states.end(), searchValue);
		if (low == states.end()) {
			return states[states.size()-1].value;
		} else {
			if (low->position > position) {
				low--;
			}
			return low->value;
		}
	}
	bool Delete(int position) {
		State searchValue(position, T());
		typename stateVector::iterator low =
			lower_bound(states.begin(), states.end(), searchValue);
		if (low != states.end()) {
			states.erase(low, states.end());
			return true;
		}
		return false;
	}
	size_t size() {
		return states.size();
	}
};

#ifdef SCI_NAMESPACE
}
#endif

#endif