diff options
author | Neil <nyamatongwe@gmail.com> | 2019-12-01 19:53:48 +1100 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2019-12-01 19:53:48 +1100 |
commit | 1e5534dde930d2b7eb2580f2c459d1553f3a5971 (patch) | |
tree | 23dff96fa1a4b856388ce42f420ed4a9797b7108 /src | |
parent | 7daa6e47979594525c3dd82bb1afce44ac28b246 (diff) | |
download | scintilla-mirror-1e5534dde930d2b7eb2580f2c459d1553f3a5971.tar.gz |
Fix a bug with deleting the first element in SparseVector that left an extra
empty partition.
Add extra checking to Partitioning and turn on checking for UnitTester.
Diffstat (limited to 'src')
-rw-r--r-- | src/Partitioning.h | 32 | ||||
-rw-r--r-- | src/SparseVector.h | 18 |
2 files changed, 44 insertions, 6 deletions
diff --git a/src/Partitioning.h b/src/Partitioning.h index c6951388e..608a3a58c 100644 --- a/src/Partitioning.h +++ b/src/Partitioning.h @@ -110,6 +110,10 @@ public: return static_cast<T>(body->Length())-1; } + T Length() const noexcept { + return PositionFromPartition(Partitions()); + } + void InsertPartition(T partition, T pos) { if (stepPartition < partition) { ApplyStep(partition); @@ -196,6 +200,34 @@ public: void DeleteAll() { Allocate(body->GetGrowSize()); } + + void Check() const { +#ifdef CHECK_CORRECTNESS + if (Length() < 0) { + throw std::runtime_error("Partitioning: Length can not be negative."); + } + if (Partitions() < 1) { + throw std::runtime_error("Partitioning: Must always have 1 or more partitions."); + } + if (Length() == 0) { + if ((PositionFromPartition(0) != 0) || (PositionFromPartition(1) != 0)) { + throw std::runtime_error("Partitioning: Invalid empty partitioning."); + } + } else { + // Positions should be a strictly ascending sequence + for (T i = 0; i < Partitions(); i++) { + const T pos = PositionFromPartition(i); + const T posNext = PositionFromPartition(i+1); + if (pos > posNext) { + throw std::runtime_error("Partitioning: Negative partition."); + } else if (pos == posNext) { + throw std::runtime_error("Partitioning: Empty partition."); + } + } + } +#endif + } + }; diff --git a/src/SparseVector.h b/src/SparseVector.h index 2d3e9cfae..97e35d342 100644 --- a/src/SparseVector.h +++ b/src/SparseVector.h @@ -120,6 +120,14 @@ public: if (startPartition == position) { if (partition == 0) { ClearValue(0); + if (starts->PositionFromPartition(1) == 1) { + // Removing all space of first partition, so remove next partition + // and move value if not last + if (Elements() > 1) { + starts->RemovePartition(partition + 1); + values->Delete(partition); + } + } } else if (partition == starts->Partitions()) { // This should not be possible ClearValue(partition); @@ -133,14 +141,11 @@ public: } } starts->InsertText(partition, -1); + Check(); } void Check() const { - if (Length() < 0) { - throw std::runtime_error("SparseVector: Length can not be negative."); - } - if (starts->Partitions() < 1) { - throw std::runtime_error("SparseVector: Must always have 1 or more partitions."); - } +#ifdef CHECK_CORRECTNESS + starts->Check(); if (starts->Partitions() != values->Length() - 1) { throw std::runtime_error("SparseVector: Partitions and values different lengths."); } @@ -148,6 +153,7 @@ public: if (values->ValueAt(values->Length() - 1) != T()) { throw std::runtime_error("SparseVector: Unused style at end changed."); } +#endif } }; |