1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# HG changeset patch
# User Neil <nyamatongwe@gmail.com>
# 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<int> 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<const char *>();
- displayLines = new Partitioning(4);
+ displayLines = new Partitioning<int>(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<const char *> *foldDisplayTexts;
- Partitioning *displayLines;
+ Partitioning<int> *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<int> {
+template <typename T>
+class SplitVectorWithRangeAdd : public SplitVector<T> {
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 <typename T>
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<T> *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<T>(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<int>(body->Length()-1);
+ T Partitions() const {
+ return static_cast<T>(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<int>(8);
styles = new SplitVector<int>();
styles->InsertValue(0, 2, 0);
}
@@ -204,7 +204,7 @@
starts = NULL;
delete styles;
styles = NULL;
- starts = new Partitioning(8);
+ starts = new Partitioning<int>(8);
styles = new SplitVector<int>();
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<int> *starts;
SplitVector<int> *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 <typename T>
class SparseVector {
private:
- Partitioning *starts;
+ Partitioning<int> *starts;
SplitVector<T> *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<int>(8);
values = new SplitVector<T>();
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<int> svwra(growSize);
SECTION("IsEmptyInitially") {
REQUIRE(0 == svwra.Length());
@@ -49,7 +49,7 @@
TEST_CASE("Partitioning") {
- Partitioning part(growSize);
+ Partitioning<int> part(growSize);
SECTION("IsEmptyInitially") {
REQUIRE(1 == part.Partitions());
|