diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ContractionState.cxx | 6 | ||||
-rw-r--r-- | src/ContractionState.h | 6 | ||||
-rw-r--r-- | src/Decoration.h | 2 | ||||
-rw-r--r-- | src/RunStyles.cxx | 120 | ||||
-rw-r--r-- | src/RunStyles.h | 39 |
5 files changed, 99 insertions, 74 deletions
diff --git a/src/ContractionState.cxx b/src/ContractionState.cxx index a9db134bc..8777e4af9 100644 --- a/src/ContractionState.cxx +++ b/src/ContractionState.cxx @@ -35,9 +35,9 @@ ContractionState::~ContractionState() { void ContractionState::EnsureData() { if (OneToOne()) { - visible.reset(new RunStyles()); - expanded.reset(new RunStyles()); - heights.reset(new RunStyles()); + visible.reset(new RunStyles<int, int>()); + expanded.reset(new RunStyles<int, int>()); + heights.reset(new RunStyles<int, int>()); foldDisplayTexts.reset(new SparseVector<UniqueString>()); displayLines.reset(new Partitioning<int>(4)); InsertLines(0, linesInDocument); diff --git a/src/ContractionState.h b/src/ContractionState.h index 44a531ca5..7f11196ea 100644 --- a/src/ContractionState.h +++ b/src/ContractionState.h @@ -17,9 +17,9 @@ class SparseVector; */ class ContractionState { // These contain 1 element for every document line. - std::unique_ptr<RunStyles> visible; - std::unique_ptr<RunStyles> expanded; - std::unique_ptr<RunStyles> heights; + std::unique_ptr<RunStyles<int, int>> visible; + std::unique_ptr<RunStyles<int, int>> expanded; + std::unique_ptr<RunStyles<int, int>> heights; std::unique_ptr<SparseVector<UniqueString>> foldDisplayTexts; std::unique_ptr<Partitioning<int>> displayLines; Sci::Line linesInDocument; diff --git a/src/Decoration.h b/src/Decoration.h index ab9912935..79ee9ef73 100644 --- a/src/Decoration.h +++ b/src/Decoration.h @@ -12,7 +12,7 @@ namespace Scintilla { class Decoration { int indicator; public: - RunStyles rs; + RunStyles<int, int> rs; explicit Decoration(int indicator_); ~Decoration(); diff --git a/src/RunStyles.cxx b/src/RunStyles.cxx index a365df6e9..f3939bbd2 100644 --- a/src/RunStyles.cxx +++ b/src/RunStyles.cxx @@ -26,8 +26,9 @@ using namespace Scintilla; // Find the first run at a position -int RunStyles::RunFromPosition(int position) const { - int run = starts->PartitionFromPosition(position); +template <typename DISTANCE, typename STYLE> +DISTANCE RunStyles<DISTANCE, STYLE>::RunFromPosition(DISTANCE position) const { + DISTANCE run = starts->PartitionFromPosition(position); // Go to first element with this position while ((run > 0) && (position == starts->PositionFromPartition(run-1))) { run--; @@ -36,11 +37,12 @@ int RunStyles::RunFromPosition(int position) const { } // If there is no run boundary at position, insert one continuing style. -int RunStyles::SplitRun(int position) { - int run = RunFromPosition(position); - const int posRun = starts->PositionFromPartition(run); +template <typename DISTANCE, typename STYLE> +DISTANCE RunStyles<DISTANCE, STYLE>::SplitRun(DISTANCE position) { + DISTANCE run = RunFromPosition(position); + const DISTANCE posRun = starts->PositionFromPartition(run); if (posRun < position) { - int runStyle = ValueAt(position); + STYLE runStyle = ValueAt(position); run++; starts->InsertPartition(run, position); styles->InsertValue(run, 1, runStyle); @@ -48,12 +50,14 @@ int RunStyles::SplitRun(int position) { return run; } -void RunStyles::RemoveRun(int run) { +template <typename DISTANCE, typename STYLE> +void RunStyles<DISTANCE, STYLE>::RemoveRun(DISTANCE run) { starts->RemovePartition(run); styles->DeleteRange(run, 1); } -void RunStyles::RemoveRunIfEmpty(int run) { +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)) { RemoveRun(run); @@ -61,7 +65,8 @@ void RunStyles::RemoveRunIfEmpty(int run) { } } -void RunStyles::RemoveRunIfSameAsPrevious(int run) { +template <typename DISTANCE, typename STYLE> +void RunStyles<DISTANCE, STYLE>::RemoveRunIfSameAsPrevious(DISTANCE run) { if ((run > 0) && (run < starts->Partitions())) { if (styles->ValueAt(run-1) == styles->ValueAt(run)) { RemoveRun(run); @@ -69,30 +74,35 @@ void RunStyles::RemoveRunIfSameAsPrevious(int run) { } } -RunStyles::RunStyles() { - starts.reset(new Partitioning<int>(8)); - styles.reset(new SplitVector<int>()); +template <typename DISTANCE, typename STYLE> +RunStyles<DISTANCE, STYLE>::RunStyles() { + starts.reset(new Partitioning<DISTANCE>(8)); + styles.reset(new SplitVector<STYLE>()); styles->InsertValue(0, 2, 0); } -RunStyles::~RunStyles() { +template <typename DISTANCE, typename STYLE> +RunStyles<DISTANCE, STYLE>::~RunStyles() { } -int RunStyles::Length() const { +template <typename DISTANCE, typename STYLE> +DISTANCE RunStyles<DISTANCE, STYLE>::Length() const { return starts->PositionFromPartition(starts->Partitions()); } -int RunStyles::ValueAt(int position) const { +template <typename DISTANCE, typename STYLE> +STYLE RunStyles<DISTANCE, STYLE>::ValueAt(DISTANCE position) const { return styles->ValueAt(starts->PartitionFromPosition(position)); } -int RunStyles::FindNextChange(int position, int end) const { - const int run = starts->PartitionFromPosition(position); +template <typename DISTANCE, typename STYLE> +DISTANCE RunStyles<DISTANCE, STYLE>::FindNextChange(DISTANCE position, DISTANCE end) const { + const DISTANCE run = starts->PartitionFromPosition(position); if (run < starts->Partitions()) { - const int runChange = starts->PositionFromPartition(run); + const DISTANCE runChange = starts->PositionFromPartition(run); if (runChange > position) return runChange; - const int nextChange = starts->PositionFromPartition(run + 1); + const DISTANCE nextChange = starts->PositionFromPartition(run + 1); if (nextChange > position) { return nextChange; } else if (position < end) { @@ -105,23 +115,26 @@ int RunStyles::FindNextChange(int position, int end) const { } } -int RunStyles::StartRun(int position) const { +template <typename DISTANCE, typename STYLE> +DISTANCE RunStyles<DISTANCE, STYLE>::StartRun(DISTANCE position) const { return starts->PositionFromPartition(starts->PartitionFromPosition(position)); } -int RunStyles::EndRun(int position) const { +template <typename DISTANCE, typename STYLE> +DISTANCE RunStyles<DISTANCE, STYLE>::EndRun(DISTANCE position) const { return starts->PositionFromPartition(starts->PartitionFromPosition(position) + 1); } -bool RunStyles::FillRange(int &position, int value, int &fillLength) { +template <typename DISTANCE, typename STYLE> +bool RunStyles<DISTANCE, STYLE>::FillRange(DISTANCE &position, STYLE value, DISTANCE &fillLength) { if (fillLength <= 0) { return false; } - int end = position + fillLength; + DISTANCE end = position + fillLength; if (end > Length()) { return false; } - int runEnd = RunFromPosition(end); + DISTANCE runEnd = RunFromPosition(end); if (styles->ValueAt(runEnd) == value) { // End already has value so trim range. end = starts->PositionFromPartition(runEnd); @@ -133,7 +146,7 @@ bool RunStyles::FillRange(int &position, int value, int &fillLength) { } else { runEnd = SplitRun(end); } - int runStart = RunFromPosition(position); + DISTANCE runStart = RunFromPosition(position); if (styles->ValueAt(runStart) == value) { // Start is in expected value so trim range. runStart++; @@ -148,7 +161,7 @@ bool RunStyles::FillRange(int &position, int value, int &fillLength) { if (runStart < runEnd) { styles->SetValueAt(runStart, value); // Remove each old run over the range - for (int run=runStart+1; run<runEnd; run++) { + for (DISTANCE run=runStart+1; run<runEnd; run++) { RemoveRun(runStart+1); } runEnd = RunFromPosition(end); @@ -162,20 +175,22 @@ bool RunStyles::FillRange(int &position, int value, int &fillLength) { } } -void RunStyles::SetValueAt(int position, int value) { - int len = 1; +template <typename DISTANCE, typename STYLE> +void RunStyles<DISTANCE, STYLE>::SetValueAt(DISTANCE position, STYLE value) { + DISTANCE len = 1; FillRange(position, value, len); } -void RunStyles::InsertSpace(int position, int insertLength) { - int runStart = RunFromPosition(position); +template <typename DISTANCE, typename STYLE> +void RunStyles<DISTANCE, STYLE>::InsertSpace(DISTANCE position, DISTANCE insertLength) { + DISTANCE runStart = RunFromPosition(position); if (starts->PositionFromPartition(runStart) == position) { - int runStyle = ValueAt(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, 0); + styles->SetValueAt(0, STYLE()); starts->InsertPartition(1, 0); styles->InsertValue(1, 1, runStyle); starts->InsertText(0, insertLength); @@ -195,16 +210,18 @@ void RunStyles::InsertSpace(int position, int insertLength) { } } -void RunStyles::DeleteAll() { - starts.reset(new Partitioning<int>(8)); - styles.reset(new SplitVector<int>()); +template <typename DISTANCE, typename STYLE> +void RunStyles<DISTANCE, STYLE>::DeleteAll() { + starts.reset(new Partitioning<DISTANCE>(8)); + styles.reset(new SplitVector<STYLE>()); styles->InsertValue(0, 2, 0); } -void RunStyles::DeleteRange(int position, int deleteLength) { - int end = position + deleteLength; - int runStart = RunFromPosition(position); - int runEnd = RunFromPosition(end); +template <typename DISTANCE, typename STYLE> +void RunStyles<DISTANCE, STYLE>::DeleteRange(DISTANCE position, DISTANCE deleteLength) { + DISTANCE end = position + deleteLength; + DISTANCE runStart = RunFromPosition(position); + DISTANCE runEnd = RunFromPosition(end); if (runStart == runEnd) { // Deleting from inside one run starts->InsertText(runStart, -deleteLength); @@ -214,7 +231,7 @@ void RunStyles::DeleteRange(int position, int deleteLength) { runEnd = SplitRun(end); starts->InsertText(runStart, -deleteLength); // Remove each old run over the range - for (int run=runStart; run<runEnd; run++) { + for (DISTANCE run=runStart; run<runEnd; run++) { RemoveRun(runStart); } RemoveRunIfEmpty(runStart); @@ -222,11 +239,13 @@ void RunStyles::DeleteRange(int position, int deleteLength) { } } -int RunStyles::Runs() const { +template <typename DISTANCE, typename STYLE> +DISTANCE RunStyles<DISTANCE, STYLE>::Runs() const { return starts->Partitions(); } -bool RunStyles::AllSame() const { +template <typename DISTANCE, typename STYLE> +bool RunStyles<DISTANCE, STYLE>::AllSame() const { for (int run = 1; run < starts->Partitions(); run++) { if (styles->ValueAt(run) != styles->ValueAt(run - 1)) return false; @@ -234,13 +253,15 @@ bool RunStyles::AllSame() const { return true; } -bool RunStyles::AllSameAs(int value) const { +template <typename DISTANCE, typename STYLE> +bool RunStyles<DISTANCE, STYLE>::AllSameAs(STYLE value) const { return AllSame() && (styles->ValueAt(0) == value); } -int RunStyles::Find(int value, int start) const { +template <typename DISTANCE, typename STYLE> +DISTANCE RunStyles<DISTANCE, STYLE>::Find(STYLE value, DISTANCE start) const { if (start < Length()) { - int run = start ? RunFromPosition(start) : 0; + DISTANCE run = start ? RunFromPosition(start) : 0; if (styles->ValueAt(run) == value) return start; run++; @@ -253,7 +274,8 @@ int RunStyles::Find(int value, int start) const { return -1; } -void RunStyles::Check() const { +template <typename DISTANCE, typename STYLE> +void RunStyles<DISTANCE, STYLE>::Check() const { if (Length() < 0) { throw std::runtime_error("RunStyles: Length can not be negative."); } @@ -263,9 +285,9 @@ void RunStyles::Check() const { if (starts->Partitions() != styles->Length()-1) { throw std::runtime_error("RunStyles: Partitions and styles different lengths."); } - int start=0; + DISTANCE start=0; while (start < Length()) { - const int end = EndRun(start); + const DISTANCE end = EndRun(start); if (start >= end) { throw std::runtime_error("RunStyles: Partition is 0 length."); } @@ -280,3 +302,5 @@ void RunStyles::Check() const { } } } + +template class Scintilla::RunStyles<int, int>; diff --git a/src/RunStyles.h b/src/RunStyles.h index 84221d512..c28621334 100644 --- a/src/RunStyles.h +++ b/src/RunStyles.h @@ -12,36 +12,37 @@ namespace Scintilla { +template <typename DISTANCE, typename STYLE> class RunStyles { private: - std::unique_ptr<Partitioning<int>> starts; - std::unique_ptr<SplitVector<int>> styles; - int RunFromPosition(int position) const; - int SplitRun(int position); - void RemoveRun(int run); - void RemoveRunIfEmpty(int run); - void RemoveRunIfSameAsPrevious(int run); + std::unique_ptr<Partitioning<DISTANCE>> starts; + std::unique_ptr<SplitVector<STYLE>> styles; + DISTANCE RunFromPosition(DISTANCE position) const; + DISTANCE SplitRun(DISTANCE position); + void RemoveRun(DISTANCE run); + void RemoveRunIfEmpty(DISTANCE run); + void RemoveRunIfSameAsPrevious(DISTANCE run); public: RunStyles(); // Deleted so RunStyles objects can not be copied. RunStyles(const RunStyles &) = delete; void operator=(const RunStyles &) = delete; ~RunStyles(); - int Length() const; - int ValueAt(int position) const; - int FindNextChange(int position, int end) const; - int StartRun(int position) const; - int EndRun(int position) const; + DISTANCE Length() const; + STYLE ValueAt(DISTANCE position) const; + DISTANCE FindNextChange(DISTANCE position, DISTANCE end) const; + DISTANCE StartRun(DISTANCE position) const; + DISTANCE EndRun(DISTANCE position) const; // Returns true if some values may have changed - bool FillRange(int &position, int value, int &fillLength); - void SetValueAt(int position, int value); - void InsertSpace(int position, int insertLength); + bool FillRange(DISTANCE &position, STYLE value, DISTANCE &fillLength); + void SetValueAt(DISTANCE position, STYLE value); + void InsertSpace(DISTANCE position, DISTANCE insertLength); void DeleteAll(); - void DeleteRange(int position, int deleteLength); - int Runs() const; + void DeleteRange(DISTANCE position, DISTANCE deleteLength); + DISTANCE Runs() const; bool AllSame() const; - bool AllSameAs(int value) const; - int Find(int value, int start) const; + bool AllSameAs(STYLE value) const; + DISTANCE Find(STYLE value, DISTANCE start) const; void Check() const; }; |