diff options
| author | nyamatongwe <devnull@localhost> | 2011-02-10 13:16:30 +1100 | 
|---|---|---|
| committer | nyamatongwe <devnull@localhost> | 2011-02-10 13:16:30 +1100 | 
| commit | a9b43404324187f56e038bab245353ba2d9d850d (patch) | |
| tree | df1344027e8a9809fb757f772702a0fc034bc0b1 /lexlib/SparseState.h | |
| parent | 6e8fc12b830a08904e83050be17b1dd128591979 (diff) | |
| download | scintilla-mirror-a9b43404324187f56e038bab245353ba2d9d850d.tar.gz | |
New class SparseState for storing lexer state that may not
change on most lines.
Diffstat (limited to 'lexlib/SparseState.h')
| -rw-r--r-- | lexlib/SparseState.h | 71 | 
1 files changed, 71 insertions, 0 deletions
| diff --git a/lexlib/SparseState.h b/lexlib/SparseState.h new file mode 100644 index 000000000..39b6e605a --- /dev/null +++ b/lexlib/SparseState.h @@ -0,0 +1,71 @@ +// 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); +		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 | 
