# HG changeset patch # User Neil # Date 1517436441 -39600 # Node ID 1bd57324aa36e3fce1ed8a2371001b062322884b # Parent ab4efcbfdae68d1ec053db212edb1440326a7f1c Templatize Partitioning so it can hold different types. diff -r ab4efcbfdae6 -r 1bd57324aa36 src/CellBuffer.h --- a/src/CellBuffer.h Wed Jan 31 17:08:48 2018 +1100 +++ b/src/CellBuffer.h Thu Feb 01 09:07:21 2018 +1100 @@ -24,7 +24,7 @@ */ class LineVector { - Partitioning starts; + Partitioning starts; PerLine *perLine; public: diff -r ab4efcbfdae6 -r 1bd57324aa36 src/ContractionState.cxx --- a/src/ContractionState.cxx Wed Jan 31 17:08:48 2018 +1100 +++ b/src/ContractionState.cxx Thu Feb 01 09:07:21 2018 +1100 @@ -39,7 +39,7 @@ expanded = new RunStyles(); heights = new RunStyles(); foldDisplayTexts = new SparseVector(); - displayLines = new Partitioning(4); + displayLines = new Partitioning(4); InsertLines(0, linesInDocument); } } diff -r ab4efcbfdae6 -r 1bd57324aa36 src/ContractionState.h --- a/src/ContractionState.h Wed Jan 31 17:08:48 2018 +1100 +++ b/src/ContractionState.h Thu Feb 01 09:07:21 2018 +1100 @@ -21,7 +21,7 @@ RunStyles *expanded; RunStyles *heights; SparseVector *foldDisplayTexts; - Partitioning *displayLines; + Partitioning *displayLines; Sci::Line linesInDocument; void EnsureData(); diff -r ab4efcbfdae6 -r 1bd57324aa36 src/Partitioning.h --- a/src/Partitioning.h Wed Jan 31 17:08:48 2018 +1100 +++ b/src/Partitioning.h Thu Feb 01 09:07:21 2018 +1100 @@ -14,29 +14,30 @@ /// in a range. /// Used by the Partitioning class. -class SplitVectorWithRangeAdd : public SplitVector { +template +class SplitVectorWithRangeAdd : public SplitVector { public: explicit SplitVectorWithRangeAdd(ptrdiff_t growSize_) { - SetGrowSize(growSize_); - ReAllocate(growSize_); + this->SetGrowSize(growSize_); + this->ReAllocate(growSize_); } ~SplitVectorWithRangeAdd() { } - void RangeAddDelta(ptrdiff_t start, ptrdiff_t end, int delta) { + void RangeAddDelta(ptrdiff_t start, ptrdiff_t end, T delta) { // end is 1 past end, so end-start is number of elements to change ptrdiff_t i = 0; const ptrdiff_t rangeLength = end - start; ptrdiff_t range1Length = rangeLength; - const ptrdiff_t part1Left = part1Length - start; + const ptrdiff_t part1Left = this->part1Length - start; if (range1Length > part1Left) range1Length = part1Left; while (i < range1Length) { - body[start++] += delta; + this->body[start++] += delta; i++; } - start += gapLength; + start += this->gapLength; while (i < rangeLength) { - body[start++] += delta; + this->body[start++] += delta; i++; } } @@ -52,16 +53,17 @@ /// When needed, positions after the interval are considered part of the last partition /// but the end of the last partition can be found with PositionFromPartition(last+1). +template class Partitioning { private: // To avoid calculating all the partition positions whenever any text is inserted // there may be a step somewhere in the list. - int stepPartition; - int stepLength; - SplitVectorWithRangeAdd *body; + T stepPartition; + T stepLength; + SplitVectorWithRangeAdd *body; // Move step forward - void ApplyStep(int partitionUpTo) { + void ApplyStep(T partitionUpTo) { if (stepLength != 0) { body->RangeAddDelta(stepPartition+1, partitionUpTo + 1, stepLength); } @@ -73,7 +75,7 @@ } // Move step backward - void BackStep(int partitionDownTo) { + void BackStep(T partitionDownTo) { if (stepLength != 0) { body->RangeAddDelta(partitionDownTo+1, stepPartition+1, -stepLength); } @@ -81,7 +83,7 @@ } void Allocate(ptrdiff_t growSize) { - body = new SplitVectorWithRangeAdd(growSize); + body = new SplitVectorWithRangeAdd(growSize); stepPartition = 0; stepLength = 0; body->Insert(0, 0); // This value stays 0 for ever @@ -100,11 +102,11 @@ ~Partitioning() { } - int Partitions() const { - return static_cast(body->Length()-1); + T Partitions() const { + return static_cast(body->Length())-1; } - void InsertPartition(int partition, int pos) { + void InsertPartition(T partition, T pos) { if (stepPartition < partition) { ApplyStep(partition); } @@ -112,7 +114,7 @@ stepPartition++; } - void SetPartitionStartPosition(int partition, int pos) { + void SetPartitionStartPosition(T partition, T pos) { ApplyStep(partition+1); if ((partition < 0) || (partition > body->Length())) { return; @@ -120,7 +122,7 @@ body->SetValueAt(partition, pos); } - void InsertText(int partitionInsert, int delta) { + void InsertText(T partitionInsert, T delta) { // Point all the partitions after the insertion point further along in the buffer if (stepLength != 0) { if (partitionInsert >= stepPartition) { @@ -142,7 +144,7 @@ } } - void RemovePartition(int partition) { + void RemovePartition(T partition) { if (partition > stepPartition) { ApplyStep(partition); stepPartition--; @@ -152,29 +154,29 @@ body->Delete(partition); } - int PositionFromPartition(int partition) const { + T PositionFromPartition(T partition) const { PLATFORM_ASSERT(partition >= 0); PLATFORM_ASSERT(partition < body->Length()); if ((partition < 0) || (partition >= body->Length())) { return 0; } - int pos = body->ValueAt(partition); + T pos = body->ValueAt(partition); if (partition > stepPartition) pos += stepLength; return pos; } /// Return value in range [0 .. Partitions() - 1] even for arguments outside interval - int PartitionFromPosition(int pos) const { + T PartitionFromPosition(T pos) const { if (body->Length() <= 1) return 0; if (pos >= (PositionFromPartition(Partitions()))) return Partitions() - 1; - int lower = 0; - int upper = Partitions(); + T lower = 0; + T upper = Partitions(); do { - const int middle = (upper + lower + 1) / 2; // Round high - int posMiddle = body->ValueAt(middle); + const T middle = (upper + lower + 1) / 2; // Round high + T posMiddle = body->ValueAt(middle); if (middle > stepPartition) posMiddle += stepLength; if (pos < posMiddle) { diff -r ab4efcbfdae6 -r 1bd57324aa36 src/RunStyles.cxx --- a/src/RunStyles.cxx Wed Jan 31 17:08:48 2018 +1100 +++ b/src/RunStyles.cxx Thu Feb 01 09:07:21 2018 +1100 @@ -70,7 +70,7 @@ } RunStyles::RunStyles() { - starts = new Partitioning(8); + starts = new Partitioning(8); styles = new SplitVector(); styles->InsertValue(0, 2, 0); } @@ -204,7 +204,7 @@ starts = NULL; delete styles; styles = NULL; - starts = new Partitioning(8); + starts = new Partitioning(8); styles = new SplitVector(); styles->InsertValue(0, 2, 0); } diff -r ab4efcbfdae6 -r 1bd57324aa36 src/RunStyles.h --- a/src/RunStyles.h Wed Jan 31 17:08:48 2018 +1100 +++ b/src/RunStyles.h Thu Feb 01 09:07:21 2018 +1100 @@ -14,7 +14,7 @@ class RunStyles { private: - Partitioning *starts; + Partitioning *starts; SplitVector *styles; int RunFromPosition(int position) const; int SplitRun(int position); diff -r ab4efcbfdae6 -r 1bd57324aa36 src/SparseVector.h --- a/src/SparseVector.h Wed Jan 31 17:08:48 2018 +1100 +++ b/src/SparseVector.h Thu Feb 01 09:07:21 2018 +1100 @@ -15,7 +15,7 @@ template class SparseVector { private: - Partitioning *starts; + Partitioning *starts; SplitVector *values; // Private so SparseVector objects can not be copied SparseVector(const SparseVector &); @@ -26,7 +26,7 @@ } public: SparseVector() { - starts = new Partitioning(8); + starts = new Partitioning(8); values = new SplitVector(); values->InsertValue(0, 2, T()); } diff -r ab4efcbfdae6 -r 1bd57324aa36 test/unit/testPartitioning.cxx --- a/test/unit/testPartitioning.cxx Wed Jan 31 17:08:48 2018 +1100 +++ b/test/unit/testPartitioning.cxx Thu Feb 01 09:07:21 2018 +1100 @@ -26,7 +26,7 @@ TEST_CASE("SplitVectorWithRangeAdd") { - SplitVectorWithRangeAdd svwra(growSize); + SplitVectorWithRangeAdd svwra(growSize); SECTION("IsEmptyInitially") { REQUIRE(0 == svwra.Length()); @@ -49,7 +49,7 @@ TEST_CASE("Partitioning") { - Partitioning part(growSize); + Partitioning part(growSize); SECTION("IsEmptyInitially") { REQUIRE(1 == part.Partitions());