aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/RunStyles.cxx
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2018-02-01 09:22:14 +1100
committerNeil <nyamatongwe@gmail.com>2018-02-01 09:22:14 +1100
commit36e4a162b950bcee61f0fe27cbacd3e85a7bdb55 (patch)
treea68c95ca6f86006e39eeda67c2d9f32fa21ccb35 /src/RunStyles.cxx
parent171899690407c0c81e0444478cb7ef64a9291c11 (diff)
downloadscintilla-mirror-36e4a162b950bcee61f0fe27cbacd3e85a7bdb55.tar.gz
Templatize RunStyles so it can be over ranges of different types and contain
different style types. Currently only instantiated over <int, int>.
Diffstat (limited to 'src/RunStyles.cxx')
-rw-r--r--src/RunStyles.cxx120
1 files changed, 72 insertions, 48 deletions
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>;