aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/SparseVector.h
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2019-12-02 08:39:52 +1100
committerNeil <nyamatongwe@gmail.com>2019-12-02 08:39:52 +1100
commitba3b2d185d77289a760bfd513611dec4f1b5e564 (patch)
tree32f1197e8d6a43c82371ea40c2c024d79ec76acd /src/SparseVector.h
parent8922ca5662974913ef0ebc93d5d029d281505a32 (diff)
downloadscintilla-mirror-ba3b2d185d77289a760bfd513611dec4f1b5e564.tar.gz
Add SparseVector::DeleteRange for efficiently removing ranges.
Diffstat (limited to 'src/SparseVector.h')
-rw-r--r--src/SparseVector.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/SparseVector.h b/src/SparseVector.h
index 3fa1ac95b..b0cfe2ded 100644
--- a/src/SparseVector.h
+++ b/src/SparseVector.h
@@ -154,6 +154,43 @@ public:
starts->InsertText(partition, -1);
Check();
}
+ void DeleteRange(Sci::Position position, Sci::Position deleteLength) {
+ // For now, delete elements in range - may want to leave value at start
+ // or combine onto position.
+ if (position > Length() || (deleteLength == 0)) {
+ return;
+ }
+ const Sci::Position positionEnd = position + deleteLength;
+ assert(positionEnd <= Length());
+ if (position == 0) {
+ // Remove all partitions in range, moving values to start
+ while ((Elements() > 1) && (starts->PositionFromPartition(1) <= deleteLength)) {
+ starts->RemovePartition(1);
+ values->Delete(0);
+ }
+ starts->InsertText(0, -deleteLength);
+ if (Length() == 0) {
+ ClearValue(0);
+ }
+ } else {
+ const Sci::Position partition = starts->PartitionFromPosition(position);
+ const bool atPartitionStart = position == starts->PositionFromPartition(partition);
+ const Sci::Position partitionDelete = partition + (atPartitionStart ? 0 : 1);
+ assert(partitionDelete > 0);
+ for (;;) {
+ const Sci::Position positionAtIndex = starts->PositionFromPartition(partitionDelete);
+ assert(position <= positionAtIndex);
+ if (positionAtIndex >= positionEnd) {
+ break;
+ }
+ assert(partitionDelete <= Elements());
+ starts->RemovePartition(partitionDelete);
+ values->Delete(partitionDelete);
+ }
+ starts->InsertText(partition - (atPartitionStart ? 1 : 0), -deleteLength);
+ }
+ Check();
+ }
Sci::Position IndexAfter(Sci::Position position) const noexcept {
assert(position < Length());
if (position < 0)