aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2019-12-02 08:34:47 +1100
committerNeil <nyamatongwe@gmail.com>2019-12-02 08:34:47 +1100
commit8922ca5662974913ef0ebc93d5d029d281505a32 (patch)
treed04ec8635fcfe32ec071567867ad54dff6429c6c
parentda8c1844da6e593644f6ae056560553bb409b354 (diff)
downloadscintilla-mirror-8922ca5662974913ef0ebc93d5d029d281505a32.tar.gz
Add SparseVector::IndexAfter for efficiently finding elements in a range.
-rw-r--r--src/SparseVector.h7
-rw-r--r--test/unit/testSparseVector.cxx17
2 files changed, 24 insertions, 0 deletions
diff --git a/src/SparseVector.h b/src/SparseVector.h
index e4e0e3f69..3fa1ac95b 100644
--- a/src/SparseVector.h
+++ b/src/SparseVector.h
@@ -154,6 +154,13 @@ public:
starts->InsertText(partition, -1);
Check();
}
+ Sci::Position IndexAfter(Sci::Position position) const noexcept {
+ assert(position < Length());
+ if (position < 0)
+ return 0;
+ const Sci::Position partition = starts->PartitionFromPosition(position);
+ return partition + 1;
+ }
void Check() const {
#ifdef CHECK_CORRECTNESS
starts->Check();
diff --git a/test/unit/testSparseVector.cxx b/test/unit/testSparseVector.cxx
index 67b8a05f8..ae2e239cf 100644
--- a/test/unit/testSparseVector.cxx
+++ b/test/unit/testSparseVector.cxx
@@ -249,6 +249,23 @@ TEST_CASE("SparseTextInt") {
REQUIRE(0 == st.ValueAt(4));
st.Check();
}
+
+ SECTION("IndexAfter") {
+ st.InsertSpace(0, 5);
+ REQUIRE(1 == st.Elements());
+ REQUIRE(0 == st.IndexAfter(-1));
+ REQUIRE(0 == st.PositionOfElement(0));
+ REQUIRE(1 == st.IndexAfter(0));
+ REQUIRE(5 == st.PositionOfElement(1));
+ st.SetValueAt(3, 3);
+ REQUIRE(2 == st.Elements());
+ REQUIRE(0 == st.IndexAfter(-1));
+ REQUIRE(0 == st.PositionOfElement(0));
+ REQUIRE(1 == st.IndexAfter(0));
+ REQUIRE(3 == st.PositionOfElement(1));
+ REQUIRE(2 == st.IndexAfter(3));
+ REQUIRE(5 == st.PositionOfElement(2));
+ }
}
TEST_CASE("SparseTextString") {