-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdqyr.cs
715 lines (663 loc) · 21.5 KB
/
dqyr.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
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.Diagnostics;
/// <summary>
/// The public interface methods call the private methods. The private methods (except for indexer) are independent on the reverseView,
/// while the public methods are dependent on it (and decide which private method to call), but do not accept it as argument (obviously).
/// </summary>
interface IDeque<T>
{
public void Prepend(T item);
public void Add(T item);
public T PopBack();
public T PopFront();
}
public class Deque<T> : IDeque<T>, IList<T>
{
private const int blockSize = 32;
protected int firstIndex = blockSize/2;
protected int lastIndex = blockSize/2-1;
protected int firstBlock = 0;
protected int lastBlock = 0;
private int blockCount { get => data.Length; }
private float resizeFactor = 2;
private bool beingEnumerated = false;
private bool reverseView = false;
private T[][] data;
private void CheckForEnumeration()
{
if (this.beingEnumerated)
throw new InvalidOperationException("Cannot modify enumerated collection!");
}
internal void InvertView()
{
this.reverseView ^= true;
}
public Deque()
{
data = new T[1][];
data[0] = new T[blockSize];
}
private void ExpandFront()
{
T[][] newArr = new T[(int)((blockCount - firstBlock) * resizeFactor) + firstBlock][]; //Only the part on "right" gets resized
for (int i = firstBlock; i <= lastBlock; ++i)
{
newArr[i] = data[i];
}
this.data = newArr;
return;
}
private void ExpandBack()
{
T[][] newArr = new T[(int)((lastBlock + 1) * resizeFactor) + blockCount - lastBlock][]; //Only the part on "left" gets resized
int plusSize = newArr.Length - data.Length;
for (int i = lastBlock; i >= firstBlock; --i)
{
newArr[i + plusSize] = data[i];
}
firstBlock += plusSize;
lastBlock += plusSize;
this.data = newArr;
return;
}
private void ShrinkFront()
{
T[][] newArr = new T[(int)((blockCount - firstBlock) / resizeFactor) + firstBlock][]; //Only the part on "right" gets resized
for (int i = firstBlock; i <= lastBlock; ++i)
{
newArr[i] = data[i];
}
this.data = newArr;
return;
}
private void ShrinkBack()
{
T[][] newArr = new T[blockCount - (int)(firstBlock / resizeFactor)][];
int minusSize = data.Length - newArr.Length;
for (int i = firstBlock; i <= lastBlock; ++i)
{
newArr[i - minusSize] = data[i];
}
firstBlock -= minusSize;
lastBlock -= minusSize;
this.data = newArr;
return;
}
private Tuple<int, int> RecountIndex(int index)
{
if (index < 0 || index >= this.Count)
{
throw new ArgumentOutOfRangeException();
}
if (reverseView)
{
int addBlocks = index / blockSize;
int pushNext = index % blockSize;
int next = lastIndex < pushNext ? 1 : 0;
int finalIndex = next == 1 ? blockSize + (lastIndex - pushNext) : lastIndex - pushNext;
int finalBlock = lastBlock - addBlocks - next;
return Tuple.Create(finalBlock, finalIndex);
}
else
{
int addBlocks = index / blockSize;
int pushNext = index % blockSize;
int next = (pushNext + firstIndex) / blockSize;
int finalIndex = (pushNext + firstIndex) % blockSize;
int finalBlock = firstBlock + addBlocks + next;
return Tuple.Create(finalBlock, finalIndex);
}
}
public T this[int index]
{
get
{
if (index < 0 || index >= this.Count)
{
throw new ArgumentOutOfRangeException();
}
if (reverseView)
{
int addBlocks = index / blockSize;
int pushNext = index % blockSize;
int next = lastIndex < pushNext ? 1 : 0;
int finalIndex = next == 1 ? blockSize + (lastIndex - pushNext) : lastIndex - pushNext;
int finalBlock = lastBlock - addBlocks - next;
return data[finalBlock][finalIndex];
}
else
{
/*
int addBlocks = index / blockSize;
int pushNext = index % blockSize;
int next = (pushNext + firstIndex) / blockSize;
int finalIndex = (pushNext + firstIndex) % blockSize;
int finalBlock = firstBlock + addBlocks + next;
*/
return data[firstBlock + index / blockSize + (index % blockSize + firstIndex) / blockSize][(index % blockSize + firstIndex) % blockSize];
}
}
set
{
if (index < 0 || index >= this.Count)
{
throw new ArgumentOutOfRangeException();
}
/*
int addBlocks = index / blockSize;
int pushNext = index % blockSize;
int next = (pushNext + firstIndex) / blockSize;
int finalIndex = (pushNext + firstIndex) % blockSize;
int finalBlock = firstBlock + addBlocks + next;
data[finalBlock][finalIndex]=value;
*/
if (reverseView)
{
int addBlocks = index / blockSize;
int pushNext = index % blockSize;
int next = lastIndex < pushNext ? 1 : 0;
int finalIndex = next == 1 ? blockSize + (lastIndex - pushNext) : lastIndex - pushNext;
int finalBlock = lastBlock - addBlocks - next;
data[finalBlock][finalIndex] = value;
return;
}
else
{
/*
int addBlocks = index / blockSize;
int pushNext = index % blockSize;
int next = (pushNext + firstIndex) / blockSize;
int finalIndex = (pushNext + firstIndex) % blockSize;
int finalBlock = firstBlock + addBlocks + next;
*/
data[firstBlock + index / blockSize + (index % blockSize + firstIndex) / blockSize][(index % blockSize + firstIndex) % blockSize] = value;
return;
}
}
}
public int Count =>
(lastBlock - firstBlock - 1) * blockSize //full blocks
+ blockSize - firstIndex //first block
+ lastIndex + 1; //last block
public bool IsReadOnly => false;
private void _Add(T item)
{
CheckForEnumeration();
if (lastIndex < blockSize - 1) //Add to last block
{
data[lastBlock][++lastIndex] = item;
}
else //Create a new block
{
if (lastBlock + 1 == blockCount)
{
ExpandFront();
}
data[++lastBlock] = new T[blockSize];
data[lastBlock][0] = item;
lastIndex = 0;
}
return;
}
public void Clear()
{
CheckForEnumeration();
int i = firstBlock;
for (int j = firstIndex; j < blockSize; ++j)
{
this.data[i][j] = default;
}
for (i = firstBlock + 1; i < lastBlock; ++i)
{
for (int j = 0; j < blockSize; ++j)
{
this.data[i][j] = default;
}
}
i = lastBlock;
for (int j = 0; j < lastIndex; ++j)
{
this.data[i][j] = default;
}
firstBlock = data.Length / 2;
firstIndex = blockSize/2;
data[firstBlock] = new T[blockSize];
lastBlock = firstBlock;
lastIndex = firstIndex-1;
return;
}
private bool _Contains(T item)
{
for (int i = Count - 1; i >= 0; --i)
{
if (item.Equals(this[i]))
return true;
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
if (array is null)
{
throw new ArgumentNullException();
}
else if (arrayIndex < 0)
{
throw new ArgumentOutOfRangeException();
}
else if (arrayIndex + this.Count >= array.Length)
{
throw new ArgumentException();
}
for (int i = 0; i < Count; ++i)
{
array[arrayIndex + i] = this[i];
}
return;
}
private int _IndexFromBack(T item)
{
int idx = 0;
if (firstBlock == lastBlock)
{
for (int j = firstIndex; j <= lastIndex; ++j)
{
if (Equals(item, data[firstBlock][j]))
return idx;
idx++;
}
return -1;
}
int i = firstBlock;
for (int j = firstIndex; j < blockSize; ++j)
{
if (Equals(item, data[i][j]))
return idx;
idx++;
}
for (i = firstBlock + 1; i < lastBlock; ++i)
{
for (int j = 0; j < blockSize; ++j)
{
if (Equals(item, data[i][j]))
return idx;
idx++;
}
}
i = lastBlock;
for (int j = 0; j <= lastIndex; ++j)
{
if (Equals(item, data[i][j]))
return idx;
idx++;
}
return -1;
}
private int _IndexFromFront(T item)
{
int idx = 0;
if (lastBlock == firstBlock)
{
for (int j = lastIndex; j >= firstIndex; --j)
{
if (Equals(item, data[lastBlock][j]))
return idx;
idx++;
}
return -1;
}
int i = lastBlock;
for (int j = lastIndex; j >= 0; --j)
{
if (Equals(item, data[i][j]))
return idx;
idx++;
}
for (i = lastBlock - 1; i > firstBlock; --i)
{
for (int j = blockSize - 1; j >= 0; --j)
{
if (Equals(item, data[i][j]))
return idx;
idx++;
}
}
i = firstBlock;
for (int j = blockSize - 1; j >= firstIndex; --j)
{
if (Equals(item, data[i][j]))
return idx;
idx++;
}
return -1;
}
private void _Insert(int index, T item)
{
CheckForEnumeration();
//TODO: If index is lower than half of count, push the values to the left (reduce complexity)
var (startBlock, startInd) = RecountIndex(index); //This throws exception if index not valid
this._Add(data[lastBlock][lastIndex]);//Increase the size by 1 and push the last item in there
if(reverseView) //Hotfix for offset in reverseView (adding one element increased the count of elements, so the starting block and index get moved
{
if (startInd == blockSize-1)
{
startBlock += 1;
startInd = 0;
}
else
{
startInd += 1;
}
}
//If moving only one block
if (startBlock == lastBlock)
{
for (int j = lastIndex - 1; j >= startInd; --j)
{
data[startBlock][j + 1] = data[startBlock][j];
}
data[startBlock][startInd] = item;
return;
}
//If moving more
for (int j = lastIndex - 2; j >= 0; --j)
{
data[lastBlock][j + 1] = data[lastBlock][j];
}
data[lastBlock][0] = data[lastBlock - 1][blockSize - 1];
for (int i = lastBlock - 1; i > startBlock; --i)
{
for (int j = blockSize - 2; j >= 0; --j)
{
data[i][j + 1] = data[i][j];
}
data[i][0] = data[i - 1][blockSize - 1];
}
for (int j = blockSize - 2; j >= startInd; --j)
{
data[startBlock][j + 1] = data[startBlock][j];
}
//Finally put the item on its position
data[startBlock][startInd] = item;
return;
}
private T _PopBack()
{
CheckForEnumeration();
if (Count <= 0)
{
throw new InvalidOperationException();
}
if (firstIndex < blockSize - 1) //Still some items in this block remaining
{
T item = data[firstBlock][firstIndex];
data[firstBlock][firstIndex] = default;
firstIndex++;
return item;
}
else
{
T item = data[firstBlock][firstIndex];
data[firstBlock][firstIndex] = default;
firstBlock += 1;
firstIndex = 0;
if (firstBlock > ((lastBlock / resizeFactor) / resizeFactor))
{
ShrinkBack();
}
return item;
}
}
private T _PopFront()
{
CheckForEnumeration();
if (Count <= 0)
{
throw new InvalidOperationException();
}
if (lastIndex > 0) //Still some items in this block remaining
{
T item = data[lastBlock][lastIndex];
data[lastBlock][lastIndex] = default;
lastIndex--;
return item;
}
else
{
T item = data[lastBlock][lastIndex];
data[lastBlock][lastIndex] = default;
lastBlock -= 1;
lastIndex = blockSize - 1;
if (lastBlock < (blockCount / resizeFactor / resizeFactor))
{
ShrinkFront();
}
return item;
}
}
private void _Prepend(T item)
{
CheckForEnumeration();
if (firstIndex > 0) //Add to first block
{
data[firstBlock][--firstIndex] = item;
}
else //Create a new block
{
if (firstBlock - 1 < 0)
{
ExpandBack();
}
data[--firstBlock] = new T[blockSize];
data[firstBlock][blockSize - 1] = item;
firstIndex = blockSize - 1;
}
return;
}
private bool _Remove(T item)
{
CheckForEnumeration();
int idx = this._IndexFromBack(item);
if (idx == -1)
return false;
this._RemoveAt(idx);
return true;
}
private void _RemoveAt(int index)
{
CheckForEnumeration();
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException();
}
else
if (index > Count / 2) //Collapse items from front
{
for (int i = index; i < Count - 1; ++i)
{
this[i] = this[i + 1];
}
this.PopFront();
}
else //Collapse items from back
{
for (int i = index - 1; i >= 0; --i)
{
this[i + 1] = this[i];
}
this.PopBack();
}
return;
}
public IEnumerator<T> GetEnumerator()
{
//Left enumerator goes from right to left (higher -> lower index), right enumerator is "normal" (lower -> higher index)
if (reverseView)
return GetLeftEnumerator();
else
return GetRightEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
if (reverseView)
return GetLeftEnumerator();
else
return GetRightEnumerator();
}
public int IndexOf(T item)
{
if (reverseView)
return _IndexFromFront(item);
else
return _IndexFromBack(item);
}
public void Insert(int index, T item)
{
this._Insert(index, item);
return;
}
public void RemoveAt(int index)
{
this._RemoveAt(index);
return;
}
public void Add(T item)
{
if (reverseView)
{
_Prepend(item);
}
else
{
_Add(item);
}
}
public bool Contains(T item)
{
return (this.IndexOf(item) != -1);
}
public bool Remove(T item)
{
CheckForEnumeration();
int idx = this.IndexOf(item);
if (idx == -1)
return false;
this._RemoveAt(idx);
return true;
}
public void Prepend(T item)
{
if (reverseView)
{
_Add(item);
}
else
{
_Prepend(item);
}
}
public T PopBack()
{
if (reverseView)
{
return _PopFront();
}
else
{
return _PopBack();
}
}
public T PopFront()
{
if (reverseView)
{
return _PopBack();
}
else
{
return _PopFront();
}
}
public IEnumerator<T> GetRightEnumerator()
{
int i = this.firstBlock;
int j = this.firstIndex;
this.beingEnumerated = true;
if (this.firstBlock == this.lastBlock)
{
for (j = firstIndex; j <= lastIndex; ++j)
{
yield return data[i][j];
}
this.beingEnumerated = false;
yield break;
}
else
{
for (j = firstIndex; j < blockSize; ++j)
{
yield return data[i][j];
}
for (i = firstBlock + 1; i < lastBlock; ++i)
{
for (j = 0; j < blockSize; ++j)
{
yield return data[i][j];
}
}
i = this.lastBlock;
for (j = 0; j <= lastIndex; ++j)
{
yield return data[i][j];
}
this.beingEnumerated = false;
yield break;
}
}
public IEnumerator<T> GetLeftEnumerator()
{
int i = this.lastBlock;
int j = this.lastIndex;
this.beingEnumerated = true;
if (this.firstBlock == this.lastBlock)
{
for (j = lastIndex; j >= firstIndex; --j)
{
yield return data[i][j];
}
this.beingEnumerated = false;
yield break;
}
else
{
for (j = lastIndex; j >= 0; --j)
{
yield return data[i][j];
}
for (i = lastBlock - 1; i > firstBlock; --i)
{
for (j = blockSize - 1; j >= 0; --j)
{
yield return data[i][j];
}
}
i = this.firstBlock;
for (j = blockSize - 1; j >= firstIndex; --j)
{
yield return data[i][j];
}
this.beingEnumerated = false;
yield break;
}
}
}
public static class DequeTest
{
public static IList<T> GetReverseView<T>(Deque<T> d)
{
d.InvertView();
return d;
}
}