diff options
author | Neil <nyamatongwe@gmail.com> | 2022-07-29 10:16:34 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2022-07-29 10:16:34 +1000 |
commit | cb3ef02e9c483596f3c146bc72325457e61d0b19 (patch) | |
tree | ef80dee71ea9af538e7bf7ec4bc8ac1592a506ea /src | |
parent | 5218b62655b877af64f9980efa668a1dcb82ed43 (diff) | |
download | scintilla-mirror-cb3ef02e9c483596f3c146bc72325457e61d0b19.tar.gz |
Flatten Partitioning and RunStyles to avoid a level of indirection by containing
SplitVector and Partitioning directly instead of using std::unique_ptr.
This will enable more value semantics and reduces potential memory allocation
failure scenarios.
Diffstat (limited to 'src')
-rw-r--r-- | src/Partitioning.h | 50 | ||||
-rw-r--r-- | src/RunStyles.cxx | 110 | ||||
-rw-r--r-- | src/RunStyles.h | 4 | ||||
-rw-r--r-- | src/SplitVector.h | 4 |
4 files changed, 84 insertions, 84 deletions
diff --git a/src/Partitioning.h b/src/Partitioning.h index da68dfb56..358fbd028 100644 --- a/src/Partitioning.h +++ b/src/Partitioning.h @@ -24,8 +24,8 @@ public: // Deleted so SplitVectorWithRangeAdd objects can not be copied. SplitVectorWithRangeAdd(const SplitVectorWithRangeAdd &) = delete; SplitVectorWithRangeAdd(SplitVectorWithRangeAdd &&) = delete; - void operator=(const SplitVectorWithRangeAdd &) = delete; - void operator=(SplitVectorWithRangeAdd &&) = delete; + SplitVectorWithRangeAdd &operator=(const SplitVectorWithRangeAdd &) = delete; + SplitVectorWithRangeAdd &operator=(SplitVectorWithRangeAdd &&) = default; ~SplitVectorWithRangeAdd() { } void RangeAddDelta(T start, T end, T delta) noexcept { @@ -63,12 +63,12 @@ private: // there may be a step somewhere in the list. T stepPartition; T stepLength; - std::unique_ptr<SplitVectorWithRangeAdd<T>> body; + SplitVectorWithRangeAdd<T> body; // Move step forward void ApplyStep(T partitionUpTo) noexcept { if (stepLength != 0) { - body->RangeAddDelta(stepPartition+1, partitionUpTo + 1, stepLength); + body.RangeAddDelta(stepPartition+1, partitionUpTo + 1, stepLength); } stepPartition = partitionUpTo; if (stepPartition >= Partitions()) { @@ -80,17 +80,17 @@ private: // Move step backward void BackStep(T partitionDownTo) noexcept { if (stepLength != 0) { - body->RangeAddDelta(partitionDownTo+1, stepPartition+1, -stepLength); + body.RangeAddDelta(partitionDownTo+1, stepPartition+1, -stepLength); } stepPartition = partitionDownTo; } void Allocate(ptrdiff_t growSize) { - body = std::make_unique<SplitVectorWithRangeAdd<T>>(growSize); + body = SplitVectorWithRangeAdd<T>(growSize); stepPartition = 0; stepLength = 0; - body->Insert(0, 0); // This value stays 0 for ever - body->Insert(1, 0); // This is the end of the first partition and will be the start of the second + body.Insert(0, 0); // This value stays 0 for ever + body.Insert(1, 0); // This is the end of the first partition and will be the start of the second } public: @@ -101,19 +101,19 @@ public: // Deleted so Partitioning objects can not be copied. Partitioning(const Partitioning &) = delete; Partitioning(Partitioning &&) = delete; - void operator=(const Partitioning &) = delete; - void operator=(Partitioning &&) = delete; + Partitioning &operator=(const Partitioning &) = delete; + Partitioning &operator=(Partitioning &&) = default; ~Partitioning() { } T Partitions() const noexcept { - return static_cast<T>(body->Length())-1; + return static_cast<T>(body.Length())-1; } void ReAllocate(ptrdiff_t newSize) { // + 1 accounts for initial element that is always 0. - body->ReAllocate(newSize + 1); + body.ReAllocate(newSize + 1); } T Length() const noexcept { @@ -124,7 +124,7 @@ public: if (stepPartition < partition) { ApplyStep(partition); } - body->Insert(partition, pos); + body.Insert(partition, pos); stepPartition++; } @@ -132,7 +132,7 @@ public: if (stepPartition < partition) { ApplyStep(partition); } - body->InsertFromArray(partition, positions, 0, length); + body.InsertFromArray(partition, positions, 0, length); stepPartition += static_cast<T>(length); } @@ -141,7 +141,7 @@ public: if (stepPartition < partition) { ApplyStep(partition); } - T *pInsertion = body->InsertEmpty(partition, length); + T *pInsertion = body.InsertEmpty(partition, length); for (size_t i = 0; i < length; i++) { pInsertion[i] = static_cast<T>(positions[i]); } @@ -150,10 +150,10 @@ public: void SetPartitionStartPosition(T partition, T pos) noexcept { ApplyStep(partition+1); - if ((partition < 0) || (partition >= body->Length())) { + if ((partition < 0) || (partition >= body.Length())) { return; } - body->SetValueAt(partition, pos); + body.SetValueAt(partition, pos); } void InsertText(T partitionInsert, T delta) noexcept { @@ -163,7 +163,7 @@ public: // Fill in up to the new insertion point ApplyStep(partitionInsert); stepLength += delta; - } else if (partitionInsert >= (stepPartition - body->Length() / 10)) { + } else if (partitionInsert >= (stepPartition - body.Length() / 10)) { // Close to step but before so move step back BackStep(partitionInsert); stepLength += delta; @@ -185,17 +185,17 @@ public: } else { stepPartition--; } - body->Delete(partition); + body.Delete(partition); } T PositionFromPartition(T partition) const noexcept { PLATFORM_ASSERT(partition >= 0); - PLATFORM_ASSERT(partition < body->Length()); - const ptrdiff_t lengthBody = body->Length(); + PLATFORM_ASSERT(partition < body.Length()); + const ptrdiff_t lengthBody = body.Length(); if ((partition < 0) || (partition >= lengthBody)) { return 0; } - T pos = body->ValueAt(partition); + T pos = body.ValueAt(partition); if (partition > stepPartition) pos += stepLength; return pos; @@ -203,7 +203,7 @@ public: /// Return value in range [0 .. Partitions() - 1] even for arguments outside interval T PartitionFromPosition(T pos) const noexcept { - if (body->Length() <= 1) + if (body.Length() <= 1) return 0; if (pos >= (PositionFromPartition(Partitions()))) return Partitions() - 1; @@ -211,7 +211,7 @@ public: T upper = Partitions(); do { const T middle = (upper + lower + 1) / 2; // Round high - T posMiddle = body->ValueAt(middle); + T posMiddle = body.ValueAt(middle); if (middle > stepPartition) posMiddle += stepLength; if (pos < posMiddle) { @@ -224,7 +224,7 @@ public: } void DeleteAll() { - Allocate(body->GetGrowSize()); + Allocate(body.GetGrowSize()); } void Check() const { diff --git a/src/RunStyles.cxx b/src/RunStyles.cxx index 6d43e6085..37c76f921 100644 --- a/src/RunStyles.cxx +++ b/src/RunStyles.cxx @@ -31,9 +31,9 @@ using namespace Scintilla::Internal; // Find the first run at a position template <typename DISTANCE, typename STYLE> DISTANCE RunStyles<DISTANCE, STYLE>::RunFromPosition(DISTANCE position) const noexcept { - DISTANCE run = starts->PartitionFromPosition(position); + DISTANCE run = starts.PartitionFromPosition(position); // Go to first element with this position - while ((run > 0) && (position == starts->PositionFromPartition(run-1))) { + while ((run > 0) && (position == starts.PositionFromPartition(run-1))) { run--; } return run; @@ -43,26 +43,26 @@ DISTANCE RunStyles<DISTANCE, STYLE>::RunFromPosition(DISTANCE position) const no template <typename DISTANCE, typename STYLE> DISTANCE RunStyles<DISTANCE, STYLE>::SplitRun(DISTANCE position) { DISTANCE run = RunFromPosition(position); - const DISTANCE posRun = starts->PositionFromPartition(run); + const DISTANCE posRun = starts.PositionFromPartition(run); if (posRun < position) { STYLE runStyle = ValueAt(position); run++; - starts->InsertPartition(run, position); - styles->InsertValue(run, 1, runStyle); + starts.InsertPartition(run, position); + styles.InsertValue(run, 1, runStyle); } return run; } template <typename DISTANCE, typename STYLE> void RunStyles<DISTANCE, STYLE>::RemoveRun(DISTANCE run) { - starts->RemovePartition(run); - styles->DeleteRange(run, 1); + starts.RemovePartition(run); + styles.DeleteRange(run, 1); } template <typename DISTANCE, typename STYLE> void RunStyles<DISTANCE, STYLE>::RemoveRunIfEmpty(DISTANCE run) { - if ((run < starts->Partitions()) && (starts->Partitions() > 1)) { - if (starts->PositionFromPartition(run) == starts->PositionFromPartition(run+1)) { + if ((run < starts.Partitions()) && (starts.Partitions() > 1)) { + if (starts.PositionFromPartition(run) == starts.PositionFromPartition(run+1)) { RemoveRun(run); } } @@ -70,9 +70,9 @@ void RunStyles<DISTANCE, STYLE>::RemoveRunIfEmpty(DISTANCE run) { template <typename DISTANCE, typename STYLE> void RunStyles<DISTANCE, STYLE>::RemoveRunIfSameAsPrevious(DISTANCE run) { - if ((run > 0) && (run < starts->Partitions())) { + if ((run > 0) && (run < starts.Partitions())) { const DISTANCE runBefore = run - 1; - if (styles->ValueAt(runBefore) == styles->ValueAt(run)) { + if (styles.ValueAt(runBefore) == styles.ValueAt(run)) { RemoveRun(run); } } @@ -80,9 +80,9 @@ void RunStyles<DISTANCE, STYLE>::RemoveRunIfSameAsPrevious(DISTANCE run) { template <typename DISTANCE, typename STYLE> RunStyles<DISTANCE, STYLE>::RunStyles() { - starts = std::make_unique<Partitioning<DISTANCE>>(8); - styles = std::make_unique<SplitVector<STYLE>>(); - styles->InsertValue(0, 2, 0); + starts = Partitioning<DISTANCE>(8); + styles = SplitVector<STYLE>(); + styles.InsertValue(0, 2, 0); } template <typename DISTANCE, typename STYLE> @@ -91,22 +91,22 @@ RunStyles<DISTANCE, STYLE>::~RunStyles() { template <typename DISTANCE, typename STYLE> DISTANCE RunStyles<DISTANCE, STYLE>::Length() const noexcept { - return starts->PositionFromPartition(starts->Partitions()); + return starts.PositionFromPartition(starts.Partitions()); } template <typename DISTANCE, typename STYLE> STYLE RunStyles<DISTANCE, STYLE>::ValueAt(DISTANCE position) const noexcept { - return styles->ValueAt(starts->PartitionFromPosition(position)); + return styles.ValueAt(starts.PartitionFromPosition(position)); } template <typename DISTANCE, typename STYLE> DISTANCE RunStyles<DISTANCE, STYLE>::FindNextChange(DISTANCE position, DISTANCE end) const noexcept { - const DISTANCE run = starts->PartitionFromPosition(position); - if (run < starts->Partitions()) { - const DISTANCE runChange = starts->PositionFromPartition(run); + const DISTANCE run = starts.PartitionFromPosition(position); + if (run < starts.Partitions()) { + const DISTANCE runChange = starts.PositionFromPartition(run); if (runChange > position) return runChange; - const DISTANCE nextChange = starts->PositionFromPartition(run + 1); + const DISTANCE nextChange = starts.PositionFromPartition(run + 1); if (nextChange > position) { return nextChange; } else if (position < end) { @@ -121,12 +121,12 @@ DISTANCE RunStyles<DISTANCE, STYLE>::FindNextChange(DISTANCE position, DISTANCE template <typename DISTANCE, typename STYLE> DISTANCE RunStyles<DISTANCE, STYLE>::StartRun(DISTANCE position) const noexcept { - return starts->PositionFromPartition(starts->PartitionFromPosition(position)); + return starts.PositionFromPartition(starts.PartitionFromPosition(position)); } template <typename DISTANCE, typename STYLE> DISTANCE RunStyles<DISTANCE, STYLE>::EndRun(DISTANCE position) const noexcept { - return starts->PositionFromPartition(starts->PartitionFromPosition(position) + 1); + return starts.PositionFromPartition(starts.PartitionFromPosition(position) + 1); } template <typename DISTANCE, typename STYLE> @@ -140,9 +140,9 @@ FillResult<DISTANCE> RunStyles<DISTANCE, STYLE>::FillRange(DISTANCE position, ST return resultNoChange; } DISTANCE runEnd = RunFromPosition(end); - if (styles->ValueAt(runEnd) == value) { + if (styles.ValueAt(runEnd) == value) { // End already has value so trim range. - end = starts->PositionFromPartition(runEnd); + end = starts.PositionFromPartition(runEnd); if (position >= end) { // Whole range is already same as value so no action return resultNoChange; @@ -152,20 +152,20 @@ FillResult<DISTANCE> RunStyles<DISTANCE, STYLE>::FillRange(DISTANCE position, ST runEnd = SplitRun(end); } DISTANCE runStart = RunFromPosition(position); - if (styles->ValueAt(runStart) == value) { + if (styles.ValueAt(runStart) == value) { // Start is in expected value so trim range. runStart++; - position = starts->PositionFromPartition(runStart); + position = starts.PositionFromPartition(runStart); fillLength = end - position; } else { - if (starts->PositionFromPartition(runStart) < position) { + if (starts.PositionFromPartition(runStart) < position) { runStart = SplitRun(position); runEnd++; } } if (runStart < runEnd) { const FillResult<DISTANCE> result{ true, position, fillLength }; - styles->SetValueAt(runStart, value); + styles.SetValueAt(runStart, value); // Remove each old run over the range for (DISTANCE run=runStart+1; run<runEnd; run++) { RemoveRun(runStart+1); @@ -189,37 +189,37 @@ void RunStyles<DISTANCE, STYLE>::SetValueAt(DISTANCE position, STYLE value) { template <typename DISTANCE, typename STYLE> void RunStyles<DISTANCE, STYLE>::InsertSpace(DISTANCE position, DISTANCE insertLength) { DISTANCE runStart = RunFromPosition(position); - if (starts->PositionFromPartition(runStart) == position) { + if (starts.PositionFromPartition(runStart) == position) { STYLE runStyle = ValueAt(position); // Inserting at start of run so make previous longer if (runStart == 0) { // Inserting at start of document so ensure 0 if (runStyle) { - styles->SetValueAt(0, STYLE()); - starts->InsertPartition(1, 0); - styles->InsertValue(1, 1, runStyle); - starts->InsertText(0, insertLength); + styles.SetValueAt(0, STYLE()); + starts.InsertPartition(1, 0); + styles.InsertValue(1, 1, runStyle); + starts.InsertText(0, insertLength); } else { - starts->InsertText(runStart, insertLength); + starts.InsertText(runStart, insertLength); } } else { if (runStyle) { - starts->InsertText(runStart-1, insertLength); + starts.InsertText(runStart-1, insertLength); } else { // Insert at end of run so do not extend style - starts->InsertText(runStart, insertLength); + starts.InsertText(runStart, insertLength); } } } else { - starts->InsertText(runStart, insertLength); + starts.InsertText(runStart, insertLength); } } template <typename DISTANCE, typename STYLE> void RunStyles<DISTANCE, STYLE>::DeleteAll() { - starts = std::make_unique<Partitioning<DISTANCE>>(8); - styles = std::make_unique<SplitVector<STYLE>>(); - styles->InsertValue(0, 2, 0); + starts = Partitioning<DISTANCE>(8); + styles = SplitVector<STYLE>(); + styles.InsertValue(0, 2, 0); } template <typename DISTANCE, typename STYLE> @@ -229,12 +229,12 @@ void RunStyles<DISTANCE, STYLE>::DeleteRange(DISTANCE position, DISTANCE deleteL DISTANCE runEnd = RunFromPosition(end); if (runStart == runEnd) { // Deleting from inside one run - starts->InsertText(runStart, -deleteLength); + starts.InsertText(runStart, -deleteLength); RemoveRunIfEmpty(runStart); } else { runStart = SplitRun(position); runEnd = SplitRun(end); - starts->InsertText(runStart, -deleteLength); + starts.InsertText(runStart, -deleteLength); // Remove each old run over the range for (DISTANCE run=runStart; run<runEnd; run++) { RemoveRun(runStart); @@ -246,14 +246,14 @@ void RunStyles<DISTANCE, STYLE>::DeleteRange(DISTANCE position, DISTANCE deleteL template <typename DISTANCE, typename STYLE> DISTANCE RunStyles<DISTANCE, STYLE>::Runs() const noexcept { - return starts->Partitions(); + return starts.Partitions(); } template <typename DISTANCE, typename STYLE> bool RunStyles<DISTANCE, STYLE>::AllSame() const noexcept { - for (DISTANCE run = 1; run < starts->Partitions(); run++) { + for (DISTANCE run = 1; run < starts.Partitions(); run++) { const DISTANCE runBefore = run - 1; - if (styles->ValueAt(run) != styles->ValueAt(runBefore)) + if (styles.ValueAt(run) != styles.ValueAt(runBefore)) return false; } return true; @@ -261,19 +261,19 @@ bool RunStyles<DISTANCE, STYLE>::AllSame() const noexcept { template <typename DISTANCE, typename STYLE> bool RunStyles<DISTANCE, STYLE>::AllSameAs(STYLE value) const noexcept { - return AllSame() && (styles->ValueAt(0) == value); + return AllSame() && (styles.ValueAt(0) == value); } template <typename DISTANCE, typename STYLE> DISTANCE RunStyles<DISTANCE, STYLE>::Find(STYLE value, DISTANCE start) const noexcept { if (start < Length()) { DISTANCE run = start ? RunFromPosition(start) : 0; - if (styles->ValueAt(run) == value) + if (styles.ValueAt(run) == value) return start; run++; - while (run < starts->Partitions()) { - if (styles->ValueAt(run) == value) - return starts->PositionFromPartition(run); + while (run < starts.Partitions()) { + if (styles.ValueAt(run) == value) + return starts.PositionFromPartition(run); run++; } } @@ -285,10 +285,10 @@ void RunStyles<DISTANCE, STYLE>::Check() const { if (Length() < 0) { throw std::runtime_error("RunStyles: Length can not be negative."); } - if (starts->Partitions() < 1) { + if (starts.Partitions() < 1) { throw std::runtime_error("RunStyles: Must always have 1 or more partitions."); } - if (starts->Partitions() != styles->Length()-1) { + if (starts.Partitions() != styles.Length()-1) { throw std::runtime_error("RunStyles: Partitions and styles different lengths."); } DISTANCE start=0; @@ -299,11 +299,11 @@ void RunStyles<DISTANCE, STYLE>::Check() const { } start = end; } - if (styles->ValueAt(styles->Length()-1) != 0) { + if (styles.ValueAt(styles.Length()-1) != 0) { throw std::runtime_error("RunStyles: Unused style at end changed."); } - for (ptrdiff_t j=1; j<styles->Length()-1; j++) { - if (styles->ValueAt(j) == styles->ValueAt(j-1)) { + for (ptrdiff_t j=1; j<styles.Length()-1; j++) { + if (styles.ValueAt(j) == styles.ValueAt(j-1)) { throw std::runtime_error("RunStyles: Style of a partition same as previous."); } } diff --git a/src/RunStyles.h b/src/RunStyles.h index 328fa1d4d..76d75d75a 100644 --- a/src/RunStyles.h +++ b/src/RunStyles.h @@ -25,8 +25,8 @@ struct FillResult { template <typename DISTANCE, typename STYLE> class RunStyles { private: - std::unique_ptr<Partitioning<DISTANCE>> starts; - std::unique_ptr<SplitVector<STYLE>> styles; + Partitioning<DISTANCE> starts; + SplitVector<STYLE> styles; DISTANCE RunFromPosition(DISTANCE position) const noexcept; DISTANCE SplitRun(DISTANCE position); void RemoveRun(DISTANCE run); diff --git a/src/SplitVector.h b/src/SplitVector.h index d0823b0b6..6c5514a22 100644 --- a/src/SplitVector.h +++ b/src/SplitVector.h @@ -77,8 +77,8 @@ public: // Deleted so SplitVector objects can not be copied. SplitVector(const SplitVector &) = delete; SplitVector(SplitVector &&) = delete; - void operator=(const SplitVector &) = delete; - void operator=(SplitVector &&) = delete; + SplitVector &operator=(const SplitVector &) = delete; + SplitVector &operator=(SplitVector &&) = default; ~SplitVector() { } |