diff options
author | nyamatongwe <unknown> | 2011-02-10 13:16:30 +1100 |
---|---|---|
committer | nyamatongwe <unknown> | 2011-02-10 13:16:30 +1100 |
commit | 7ae8e9fe7634a2a8e015d9b0c60e85727130a256 (patch) | |
tree | 468f00990fa8788868fcd537085933a1a0dde430 | |
parent | 30d0696a6846758ec1c98a6919e525711780fe65 (diff) | |
download | scintilla-mirror-7ae8e9fe7634a2a8e015d9b0c60e85727130a256.tar.gz |
New class SparseState for storing lexer state that may not
change on most lines.
-rw-r--r-- | lexlib/SparseState.h | 71 | ||||
-rw-r--r-- | test/unit/testSparseState.cxx | 138 |
2 files changed, 209 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 diff --git a/test/unit/testSparseState.cxx b/test/unit/testSparseState.cxx new file mode 100644 index 000000000..f16f829d4 --- /dev/null +++ b/test/unit/testSparseState.cxx @@ -0,0 +1,138 @@ +// Unit Tests for Scintilla internal data structures + +#include <string> +#include <vector> +#include <algorithm> + +#include "Platform.h" + +#include "SparseState.h" + +#include <gtest/gtest.h> + +// Test SparseState. + +class SparseStateTest : public ::testing::Test { +protected: + virtual void SetUp() { + pss = new SparseState<int>(); + } + + virtual void TearDown() { + delete pss; + pss = 0; + } + + SparseState<int> *pss; +}; + +TEST_F(SparseStateTest, IsEmptyInitially) { + EXPECT_EQ(0u, pss->size()); + int val = pss->ValueAt(0); + EXPECT_EQ(0, val); +} + +TEST_F(SparseStateTest, SimpleSetAndGet) { + pss->Set(0, 22); + pss->Set(1, 23); + EXPECT_EQ(2u, pss->size()); + EXPECT_EQ(0, pss->ValueAt(-1)); + EXPECT_EQ(22, pss->ValueAt(0)); + EXPECT_EQ(23, pss->ValueAt(1)); + EXPECT_EQ(23, pss->ValueAt(2)); +} + +TEST_F(SparseStateTest, RetrieveBetween) { + pss->Set(0, 10); + pss->Set(2, 12); + EXPECT_EQ(2u, pss->size()); + EXPECT_EQ(0, pss->ValueAt(-1)); + EXPECT_EQ(10, pss->ValueAt(0)); + EXPECT_EQ(10, pss->ValueAt(1)); + EXPECT_EQ(12, pss->ValueAt(2)); +} + +TEST_F(SparseStateTest, RetrieveBefore) { + pss->Set(2, 12); + EXPECT_EQ(1u, pss->size()); + EXPECT_EQ(0, pss->ValueAt(-1)); + EXPECT_EQ(0, pss->ValueAt(0)); + EXPECT_EQ(0, pss->ValueAt(1)); + EXPECT_EQ(12, pss->ValueAt(2)); +} + +TEST_F(SparseStateTest, Delete) { + pss->Set(0, 30); + pss->Set(2, 32); + pss->Delete(2); + EXPECT_EQ(1u, pss->size()); + EXPECT_EQ(0, pss->ValueAt(-1)); + EXPECT_EQ(30, pss->ValueAt(0)); + EXPECT_EQ(30, pss->ValueAt(1)); + EXPECT_EQ(30, pss->ValueAt(2)); +} + +TEST_F(SparseStateTest, DeleteBetweeen) { + pss->Set(0, 30); + pss->Set(2, 32); + pss->Delete(1); + EXPECT_EQ(1u, pss->size()); + EXPECT_EQ(0, pss->ValueAt(-1)); + EXPECT_EQ(30, pss->ValueAt(0)); + EXPECT_EQ(30, pss->ValueAt(1)); + EXPECT_EQ(30, pss->ValueAt(2)); +} + +TEST_F(SparseStateTest, ReplaceLast) { + pss->Set(0, 30); + pss->Set(2, 31); + pss->Set(2, 32); + EXPECT_EQ(2u, pss->size()); + EXPECT_EQ(0, pss->ValueAt(-1)); + EXPECT_EQ(30, pss->ValueAt(0)); + EXPECT_EQ(30, pss->ValueAt(1)); + EXPECT_EQ(32, pss->ValueAt(2)); + EXPECT_EQ(32, pss->ValueAt(3)); +} + +class SparseStateStringTest : public ::testing::Test { +protected: + virtual void SetUp() { + pss = new SparseState<std::string>(); + } + + virtual void TearDown() { + delete pss; + pss = 0; + } + + SparseState<std::string> *pss; +}; + +TEST_F(SparseStateStringTest, IsEmptyInitially) { + EXPECT_EQ(0u, pss->size()); + std::string val = pss->ValueAt(0); + EXPECT_EQ("", val); +} + +TEST_F(SparseStateStringTest, SimpleSetAndGet) { + EXPECT_EQ(0u, pss->size()); + pss->Set(0, "22"); + pss->Set(1, "23"); + EXPECT_EQ(2u, pss->size()); + EXPECT_EQ("", pss->ValueAt(-1)); + EXPECT_EQ("22", pss->ValueAt(0)); + EXPECT_EQ("23", pss->ValueAt(1)); + EXPECT_EQ("23", pss->ValueAt(2)); +} + +TEST_F(SparseStateStringTest, DeleteBetweeen) { + pss->Set(0, "30"); + pss->Set(2, "32"); + pss->Delete(1); + EXPECT_EQ(1u, pss->size()); + EXPECT_EQ("", pss->ValueAt(-1)); + EXPECT_EQ("30", pss->ValueAt(0)); + EXPECT_EQ("30", pss->ValueAt(1)); + EXPECT_EQ("30", pss->ValueAt(2)); +} |