-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scrollable.cs
468 lines (452 loc) · 14.4 KB
/
Scrollable.cs
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System;
using System.Collections.Generic;
namespace TextReader {
public interface IScrollable<T> {
T Current { get; }
bool IsLast { get; }
bool IsFirst { get; }
void ToNext();
void ToPrev();
Position Position { get; set; }
}
public interface Position {
}
public class CachingScrollable<T> : IScrollable<T> {
private int cacheSize;
private IScrollable<T> underlying;
private LinkedList<CacheItem> cache;
private LinkedListNode<CacheItem> currentNode;
private bool atLeft;
private long generation;
public CachingScrollable(IScrollable<T> underlying, int cacheSize) {
this.cacheSize = cacheSize;
this.underlying = underlying;
this.cache = new LinkedList<CacheItem>();
initCurrent();
}
public T Current {
get {
initCurrent();
return currentNode.Value.value;
}
}
public bool IsLast {
get {
initCurrent();
toRight();
return currentNode.Next == null && underlying.IsLast;
}
}
public bool IsFirst {
get {
initCurrent();
toLeft();
return currentNode.Previous == null && underlying.IsFirst;
}
}
public void ToNext() {
initCurrent();
if (currentNode.Next == null) {
toRight();
if (underlying.IsLast) {
throw new ArgumentException("Already at bottom");
} else {
underlying.ToNext();
cache.AddLast(new CacheItem {
pos = underlying.Position,
value = underlying.Current,
index = currentNode.Value.index + 1
});
if (cache.Count > cacheSize) {
cache.RemoveFirst();
}
}
}
currentNode = currentNode.Next;
}
public void ToPrev() {
initCurrent();
if (currentNode.Previous == null) {
toLeft();
if (underlying.IsFirst) {
throw new ArgumentException("Already at top");
} else {
underlying.ToPrev();
cache.AddFirst(new CacheItem {
pos = underlying.Position,
value = underlying.Current,
index = currentNode.Value.index - 1
});
if (cache.Count > cacheSize) {
cache.RemoveLast();
}
}
}
currentNode = currentNode.Previous;
}
public Position Position {
get {
if (currentNode != null) {
return new Pos() { pos = currentNode.Value.pos, generation = generation, index = currentNode.Value.index };
} else {
return new Pos() { pos = underlying.Position, generation = Environment.TickCount };
}
}
set {
if (value is Pos) {
Pos p = (Pos) value;
if (p.generation == this.generation && cache.Count > 0
&& cache.First.Value.index <= p.index && cache.Last.Value.index >= p.index) {
int diff = p.index - currentNode.Value.index;
while (diff > 0) {
ToNext();
diff--;
}
while (diff < 0) {
ToPrev();
diff++;
}
} else {
cache.Clear();
currentNode = null;
underlying.Position = p.pos;
}
} else {
cache.Clear();
currentNode = null;
underlying.Position = value;
}
}
}
protected void Invalidate(bool contentChanged) {
if (!contentChanged && currentNode != null) {
underlying.Position = currentNode.Value.pos;
}
cache.Clear();
currentNode = null;
}
protected void initCurrent() {
if (currentNode == null) {
if (cache.Count != 0) {
throw new ArgumentException("currenNode is null in non-empty cache"); // a bug, that is
}
generation = Environment.TickCount;
cache.AddLast(new CacheItem { pos = underlying.Position, value = underlying.Current, index = 0 });
currentNode = cache.First;
atLeft = false;
}
}
private void toRight() {
if (atLeft) {
underlying.Position = cache.Last.Value.pos;
atLeft = false;
}
}
private void toLeft() {
if (!atLeft) {
underlying.Position = cache.First.Value.pos;
atLeft = true;
}
}
private class CacheItem {
public Position pos;
public T value;
public int index;
}
private class Pos : Position {
public Position pos;
public int index;
public long generation;
}
}
public class ArrayScrollable<T> : IScrollable<T> {
private T[] array;
private int index;
public ArrayScrollable(T[] array) {
if (array.Length == 0) {
throw new ArgumentException("Empty array");
}
this.array = array;
this.index = 0;
}
public T Current { get { return array[index]; } }
public bool IsLast { get { return index == array.Length - 1; } }
public bool IsFirst { get { return index == 0; } }
public void ToNext() { index++; }
public void ToPrev() { index--; }
public Position Position {
get { return new Pos() { index = index }; }
set {
if (!(value is Pos)) {
throw new ArgumentException("Unsupported position type. Expected: " + typeof(Pos) + ", found: " + value.GetType());
}
index = ((Pos) value).index;
}
}
private class Pos : Position {
public int index;
}
}
public class MappingScrollable<T, U> : IScrollable<T> {
protected IScrollable<U> underlying;
protected Func<U, T> map;
public MappingScrollable(IScrollable<U> underlying, Func<U, T> map) {
this.underlying = underlying;
this.map = map;
}
public virtual T Current { get { return map(underlying.Current); } }
public bool IsLast { get { return underlying.IsLast; } }
public bool IsFirst { get { return underlying.IsFirst; } }
public void ToNext() { underlying.ToNext(); }
public void ToPrev() { underlying.ToPrev(); }
public virtual Position Position {
get { return underlying.Position; }
set { underlying.Position = value; }
}
}
public class CachedMappingScrollable<T, U> : MappingScrollable<T, U> {
private U currentUndelying;
private T current;
private bool dirty;
public CachedMappingScrollable(IScrollable<U> underlying, Func<U, T> map) : base(underlying, map) { }
public override T Current {
get {
if (dirty || !underlying.Current.Equals(currentUndelying)) {
currentUndelying = underlying.Current;
current = map(underlying.Current);
dirty = false;
}
return current;
}
}
public void Invalidate() {
dirty = true;
}
}
public abstract class AggregatingScrollable<T, U> : IScrollable<T> {
protected IScrollable<U> underlying;
protected T current;
protected bool isFirst;
protected bool isLast;
protected bool atLeft;
protected Position beginPos;
protected Position endPos;
public AggregatingScrollable(IScrollable<U> underlying) {
this.underlying = underlying;
}
public virtual T Current { get { return current; } }
public virtual bool IsFirst { get { return isFirst; } }
public virtual bool IsLast { get { return isLast; } }
public virtual void ToNext() {
if (atLeft) {
underlying.Position = endPos;
atLeft = false;
}
skipForward();
beginPos = underlying.Position;
current = fetchForward(ref isLast);
endPos = underlying.Position;
}
public virtual void ToPrev() {
if (!atLeft) {
underlying.Position = beginPos;
atLeft = true;
}
skipBackward();
endPos = underlying.Position;
current = fetchBackward(ref isFirst);
beginPos = underlying.Position;
}
public virtual Position Position {
get {
return beginPos;
}
set {
underlying.Position = value;
beginPos = value;
isFirst = checkFirst();
current = fetchForward(ref isLast);
endPos = underlying.Position;
atLeft = false;
}
}
protected virtual void init() {
this.isFirst = checkFirst();
this.beginPos = underlying.Position;
this.current = fetchForward(ref isLast);
this.endPos = underlying.Position;
this.atLeft = false;
}
protected abstract T fetchForward(ref bool isLast);
protected abstract void skipForward();
protected abstract T fetchBackward(ref bool isFirst);
protected abstract void skipBackward();
protected virtual bool checkFirst() {
return underlying.IsFirst;
}
}
public class SplittingScrollable<T, C> : IScrollable<T> where C : IList<T> {
private IScrollable<C> underlying;
private int index;
private int underlyingIndex;
public SplittingScrollable(IScrollable<C> underlying) {
this.underlying = underlying;
this.index = 0;
this.underlyingIndex = 0;
}
public T Current { get { return underlying.Current[index]; } }
public bool IsFirst { get { return index == 0 && underlying.IsFirst; } }
public bool IsLast { get { return index == underlying.Current.Count - 1 && underlying.IsLast; } }
public void ToNext() {
index++;
if (index == underlying.Current.Count) {
underlying.ToNext();
underlyingIndex++;
index = 0;
}
}
public void ToPrev() {
index--;
if (index < 0) {
underlying.ToPrev();
underlyingIndex--;
index = underlying.Current.Count - 1;
}
}
public Position Position {
get {
return new Pos() { pos = underlying.Position, index = index, underlyingIndex = underlyingIndex };
}
set {
if (value is Pos) {
Pos p = (Pos) value;
if (p.underlyingIndex == underlyingIndex) {
index = p.index;
} else {
underlying.Position = p.pos;
index = p.index;
underlyingIndex = p.underlyingIndex;
}
} else {
underlying.Position = value;
index = 0;
underlyingIndex = 0;
}
}
}
private class Pos : Position {
public Position pos;
public int index;
public int underlyingIndex;
}
}
public class TimedScrollable<T> : IScrollable<T> {
long current;
long last;
long first;
long next;
long prev;
long getpos;
long setpos;
IScrollable<T> underlying;
public TimedScrollable(IScrollable<T> underlying) {
this.underlying = underlying;
}
public T Current {
get {
long t = Environment.TickCount;
T result = underlying.Current;
current += Environment.TickCount - t;
return result;
}
}
public bool IsLast {
get {
long t = Environment.TickCount;
bool result = underlying.IsLast;
last += Environment.TickCount - t;
return result;
}
}
public bool IsFirst {
get {
long t = Environment.TickCount;
bool result = underlying.IsFirst;
first += Environment.TickCount - t;
return result;
}
}
public void ToNext() {
long t = Environment.TickCount;
underlying.ToNext();
next += Environment.TickCount - t;
}
public void ToPrev() {
long t = Environment.TickCount;
underlying.ToPrev();
prev += Environment.TickCount - t;
}
public Position Position {
get {
long t = Environment.TickCount;
Position result = underlying.Position;
getpos += Environment.TickCount - t;
return result;
}
set {
long t = Environment.TickCount;
underlying.Position = value;
setpos += Environment.TickCount - t;
}
}
public override string ToString() {
return "c:" + current + " f:" + first + " l:" + last + " n:" + next + " p:" + prev + " gp:" + getpos + " sp:" + setpos;
}
}
public class DebugTrackingScrollable<T> : IScrollable<T> {
IScrollable<T> underlying;
public DebugTrackingScrollable(IScrollable<T> underlying) {
this.underlying = underlying;
}
public T Current {
get {
T result = underlying.Current;
Console.WriteLine("Current: " + result);
return result;
}
}
public bool IsLast {
get {
bool result = underlying.IsLast;
Console.WriteLine("IsLast: " + result);
return result;
}
}
public bool IsFirst {
get {
bool result = underlying.IsFirst;
Console.WriteLine("IsFirst: " + result);
return result;
}
}
public void ToNext() {
Console.WriteLine("ToNext");
underlying.ToNext();
}
public void ToPrev() {
Console.WriteLine("ToPrev");
underlying.ToPrev();
}
public Position Position {
get {
Position result = underlying.Position;
Console.WriteLine("Position: " + result);
return result;
}
set {
Console.WriteLine("ToPosition: " + value);
underlying.Position = value;
}
}
}
}