-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastList.cs
More file actions
715 lines (636 loc) · 21 KB
/
Copy pathFastList.cs
File metadata and controls
715 lines (636 loc) · 21 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace ZExtensions
{
public class FastList<T> : IList<T>, IList
{
private int count;
private Cluster first;
private Cluster last;
private readonly List<Dictionary<int, Cluster>> lookupTables =
new List<Dictionary<int, Cluster>>();
private readonly Dictionary<Cluster, int> startIndexCache = new Dictionary<Cluster, int>();
private readonly List<Rail> rails = new List<Rail>();
private Rail lastUsedRail;
public IEnumerator<T> GetEnumerator()
{
var enumerator = new Enumerator(first);
return enumerator;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T item)
{
if (first == null)
{
first = new Cluster();
last = first;
}
if (!last.IsFull)
{
last.Add(item);
}
else
{
var newLast = new Cluster();
newLast.Previous = last;
newLast.Add(item);
last.Next= newLast;
last = newLast;
}
this.AddToTable(item, last);
Interlocked.Increment(ref count);
this.ClearCache();
}
private void AddToTable(T item, Cluster cluster)
{
int hash = GetItemHash(item);
Dictionary<int, Cluster> table = null;
foreach (var t in this.lookupTables)
{
if (!t.ContainsKey(hash))
{
table = t;
break;
}
}
if (table == null)
{
table = new Dictionary<int, Cluster>();
this.lookupTables.Add(table);
}
table.Add(hash, cluster);
}
public int Add(object value)
{
Add((T) value);
return Count;
}
public bool Contains(object value)
{
return Contains((T) value);
}
public void Clear()
{
this.lookupTables.Clear();
first = null;
last = null;
this.ClearCache();
}
private void ClearCache()
{
rails.Clear();
lastUsedRail = null;
startIndexCache.Clear();
}
public int IndexOf(object value)
{
return IndexOf((T) value);
}
public void Insert(int index, object value)
{
Insert(index, (T)value);
}
public void Remove(object value)
{
Remove((T) value);
}
public bool Contains(T item)
{
var cluster = this.GetItemCluster(item);
return cluster != null;
}
public void CopyTo(T[] array, int arrayIndex)
{
int currentIndex = arrayIndex;
var current = first;
while (current != null)
{
current.CopyTo(array, currentIndex);
currentIndex += current.ItemsCount;
current = current.Next;
}
}
public bool Remove(T item)
{
bool removed = false;
int hash = GetItemHash(item);
var table = this.GetItemTable(hash, item);
if (table != null)
{
var cluster = table[hash];
removed = cluster.Remove(item);
if (removed)
{
if (cluster.ItemsCount == 0)
{
var next = cluster.Next;
var previous = cluster.Previous;
if (next != null)
{
next.Previous = previous;
}
if (previous != null)
{
previous.Next = next;
}
if (cluster == first)
{
first = next;
}
if (cluster == last)
{
if (previous != null)
{
previous.Next = null;
}
last = previous;
}
//cluster.Previous = next;
//cluster.Next = previous;
}
table.Remove(hash);
Interlocked.Decrement(ref count);
}
}
this.ClearCache();
#if DEBUG
this.SanityCheck();
#endif
return removed;
}
public void CopyTo(Array array, int index)
{
this.CopyTo((T[])array, index);
}
public int Count
{
get { return count; }
}
public object SyncRoot
{
get { return this; }
}
public bool IsSynchronized
{
get { return false; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool IsFixedSize
{
get { return false; }
}
public int IndexOf(T item)
{
var itemCluster = this.GetItemCluster(item);
int index = -1;
if (itemCluster != null)
{
if (startIndexCache.ContainsKey(itemCluster))
{
index = startIndexCache[itemCluster] + itemCluster.IndexOf(item);
}
else
{
var startIndex = 0;
var currentCluster = first;
while (currentCluster != itemCluster)
{
if (!startIndexCache.ContainsKey(currentCluster))
{
startIndexCache.Add(currentCluster, startIndex);
}
startIndex += currentCluster.ItemsCount;
currentCluster = currentCluster.Next;
}
}
}
return index;
}
public void Insert(int index, T item)
{
this.RangeCheck(index);
int clusterStartIndex;
var cluster = this.GetClusterOfIndex(index, out clusterStartIndex);
if (index == clusterStartIndex && cluster.Previous != null && !cluster.Previous.IsFull)
{
cluster = cluster.Previous;
clusterStartIndex -= cluster.ItemsCount;
}
int clusterIndex = index - clusterStartIndex;
if (!cluster.IsFull)
{
cluster.Insert(clusterIndex, item);
this.AddToTable(item, cluster);
}
else
{
Cluster left;
Cluster right;
SplitCluster(cluster, clusterIndex, out left, out right);
left.Add(item);
var prev = cluster.Previous;
var next = cluster.Next;
if (prev != null)
{
prev.Next = left;
left.Previous = prev;
}
if (next != null)
{
next.Previous = right;
}
right.Next = next;
if (cluster == first)
{
if (first == last)
{
last = left;
}
first = left;
}
for (int i = 0; i < left.ItemsCount; i++)
{
ReaddToTable(left[i], left);
}
for (int i = 0; i < right.ItemsCount; i++)
{
ReaddToTable(right[i], right);
}
#if DEBUG
this.SanityCheck();
#endif
}
Interlocked.Increment(ref count);
this.ClearCache();
}
private void RangeCheck(int index)
{
if (index >= this.Count)
{
this.ThrowOutOfRangeException(index);
}
}
private void ThrowOutOfRangeException(int index)
{
throw new ArgumentOutOfRangeException(string.Format("size {0}, requested index {1}", this.Count, index));
}
#if DEBUG
private void SanityCheck()
{
if (first != null && (first.Previous != null || last.Next != null || first.ItemsCount == 0) ||
last != null && (last.ItemsCount == 0 || last.Next != null))
{
throw new InvalidOperationException();
}
}
#endif
private void ReaddToTable(T item, Cluster cluster)
{
int hash = GetItemHash(item);
var table = this.GetItemTable(hash, item);
if (table != null)
{
table[hash] = cluster;
}
else
{
this.AddToTable(item, cluster);
}
}
private int GetItemHash(T item)
{
int hash = 0;
if (item != null)
{
hash = item.GetHashCode();
}
return hash;
}
private void SplitCluster(Cluster cluster, int splitIndex, out Cluster left, out Cluster right)
{
left = new Cluster();
right = new Cluster();
for (int i = 0; i < splitIndex; i++)
{
left.Add(cluster[i]);
}
for (int i = splitIndex; i < cluster.ItemsCount; i++)
{
right.Add(cluster[i]);
}
left.Next = right;
right.Previous = left;
}
public void RemoveAt(int index)
{
int clusterStartIndex;
var cluster = this.GetClusterOfIndex(index, out clusterStartIndex);
int clusterIndex = index - clusterStartIndex;
cluster.RemoveAt(clusterIndex);
}
object IList.this[int index]
{
get { return this[index]; }
set { this[index] = (T) value; }
}
public T this[int index]
{
get
{
this.RangeCheck(index);
int clusterStartIndex;
var cluster = this.GetClusterOfIndex(index, out clusterStartIndex);
int clusterIndex = index - clusterStartIndex;
var item = cluster[clusterIndex];
return item;
}
set
{
this.RangeCheck(index);
int clusterStartIndex;
var cluster = this.GetClusterOfIndex(index, out clusterStartIndex);
int clusterIndex = index - clusterStartIndex;
cluster[clusterIndex] = value;
}
}
private Cluster GetClusterOfIndex(int index, out int clusterStartIndex)
{
Cluster cluster;
if (index < first.ItemsCount)
{
clusterStartIndex = 0;
cluster = first;
}
else if (index >= count - last.ItemsCount)
{
clusterStartIndex = count - last.ItemsCount;
cluster = last;
}
else
{
var rail = this.GetFastestRail(index);
rail.MoveToIndex(index);
clusterStartIndex = rail.ClusterStartIndex;
cluster = rail.Cluster;
}
return cluster;
}
private Rail GetFastestRail(int index)
{
var totalRails = (int) Math.Ceiling(((decimal) this.Count / 5000));
if (rails.Count < totalRails)
{
rails.Add(new Rail(first, last, count));
}
if (lastUsedRail == null)
{
lastUsedRail = rails[0];
}
int minDistance = Math.Abs(lastUsedRail.ClusterStartIndex - index);
Rail fastestRail = lastUsedRail;
foreach (var rail in rails)
{
if (minDistance <= Cluster.StorageSize * 2)
{
break;
}
int distance = Math.Abs(rail.ClusterStartIndex - index);
if (distance < minDistance)
{
minDistance = distance;
fastestRail = rail;
}
}
lastUsedRail = fastestRail;
return fastestRail;
}
private Cluster GetItemCluster(T item)
{
Cluster cluster = null;
int hash = GetItemHash(item);
var table = this.GetItemTable(hash, item);
if (table != null)
{
cluster = table[hash];
}
return cluster;
}
private Dictionary<int, Cluster> GetItemTable(int hash, T item)
{
Dictionary<int, Cluster> result = null;
foreach (var table in this.lookupTables)
{
if (table.ContainsKey(hash) && table[hash].Contains(item))
{
result = table;
break;
}
}
return result;
}
private class Rail
{
private readonly Cluster first;
private readonly Cluster last;
private readonly int totalCount;
public Rail(Cluster first, Cluster last, int totalCount): this(first, last, totalCount, first, 0)
{
}
public Rail(Cluster first, Cluster last, int totalCount, Cluster cluster, int clusterStartIndex)
{
if (cluster == null)
{
throw new NullReferenceException("cluster");
}
this.first = first;
this.last = last;
this.totalCount = totalCount;
this.Cluster = cluster;
this.ClusterStartIndex = clusterStartIndex;
}
public int ClusterStartIndex { get; private set; }
public Cluster Cluster { get; private set; }
public void MoveToIndex(int index)
{
this.MoveToClosestPosition(index);
var clusterStartIndex = ClusterStartIndex;
var cluster = Cluster;
int lastItemIndex = clusterStartIndex + cluster.ItemsCount - 1;
while (clusterStartIndex > index && lastItemIndex > index)
{
cluster = cluster.Previous;
clusterStartIndex = clusterStartIndex - cluster.ItemsCount;
lastItemIndex = clusterStartIndex + cluster.ItemsCount - 1;
}
while (index > lastItemIndex)
{
clusterStartIndex += cluster.ItemsCount;
cluster = cluster.Next;
lastItemIndex = clusterStartIndex + cluster.ItemsCount - 1;
}
Cluster = cluster;
ClusterStartIndex = clusterStartIndex;
}
private void MoveToClosestPosition(int index)
{
int distance = ClusterStartIndex - index;
if (distance < 0)
{
distance *= -1;
}
int endDistance = totalCount - index;
if (index < distance && index < endDistance)
{
Cluster = first;
ClusterStartIndex = 0;
}
else if (endDistance < distance && endDistance < index)
{
Cluster = last;
ClusterStartIndex = totalCount - last.ItemsCount;
}
}
}
private class Enumerator : IEnumerator<T>
{
private Cluster root;
private Cluster current;
private int currentIndex;
public Enumerator(Cluster root)
{
this.root = root;
}
public void Dispose()
{
root = null;
current = null;
}
public bool MoveNext()
{
if (current == null)
{
current = root;
currentIndex = 0;
}
else
{
currentIndex++;
if (currentIndex > current.ItemsCount - 1)
{
current = current.Next;
currentIndex = 0;
}
}
return current != null;
}
public void Reset()
{
this.current = null;
}
public T Current
{
get { return current[currentIndex]; }
}
object IEnumerator.Current
{
get { return Current; }
}
}
private class Cluster
{
public const int StorageSize = 20;
private readonly T[] storage = new T[StorageSize];
private int current;
internal void Add(T item)
{
if (IsFull)
{
throw new InvalidOperationException("Storage is full");
}
storage[current++] = item;
}
internal bool Remove(T item)
{
bool removed = false;
int index = this.IndexOf(item);
if (index > -1)
{
this.RemoveAt(index);
removed = true;
}
return removed;
}
public bool IsFull
{
get
{
bool result = current >= StorageSize;
return result;
}
}
public int ItemsCount
{
get { return current; }
}
public Cluster Next { get; set; }
public Cluster Previous { get; set; }
public int IndexOf(T item)
{
int index = Array.IndexOf(storage, item);
return index;
}
public T this[int index]
{
get
{
if (index >= current)
{
throw new IndexOutOfRangeException();
}
return storage[index];
}
set
{
if (index >= current)
{
throw new IndexOutOfRangeException();
}
storage[index] = value;
}
}
public bool Contains(T item)
{
return this.IndexOf(item) > -1;
}
public void Insert(int index, T item)
{
for (int i = current - 1; i >= index; i--)
{
storage[i + 1] = storage[i];
}
storage[index] = item;
current++;
}
public void RemoveAt(int index)
{
for (int j = index; j < current - 1; j++)
{
storage[j] = storage[j + 1];
}
current--;
}
public void CopyTo(T[] array, int arrayIndex)
{
Array.Copy(storage, 0, array, arrayIndex, current);
}
}
}
}