-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcollections.d
More file actions
1793 lines (1497 loc) · 35.7 KB
/
collections.d
File metadata and controls
1793 lines (1497 loc) · 35.7 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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright: (c) 2009 John Chapman
*
* License: See $(LINK2 ..\..\licence.txt, licence.txt) for use and distribution terms.
*/
module juno.base.collections;
import juno.base.core,
juno.locale.core,
std.math;
import std.c.string : memmove, memset;
import std.conv;
/**
* <code>bool delegate(T a, T b)</code>
*/
template EqualityComparison(T) {
alias bool delegate(T a, T b) EqualityComparison;
}
/**
* <code>int delegate(T a, T b)</code>
*/
template Comparison(T) {
alias int delegate(T a, T b) Comparison;
}
/**
* <code>bool delegate(T obj)</code>
*/
template Predicate(T) {
alias bool delegate(T) Predicate;
}
/**
* <code>TOutput delegate(TInput input)</code>
*/
template Converter(TInput, TOutput) {
alias TOutput delegate(TInput) Converter;
}
/**
* <code>void delegate(T obj)</code>
*/
template Action(T) {
alias void delegate(T obj) Action;
}
private bool equalityComparisonImpl(T)(T a, T b) {
static if (is(T == class) || is(T == interface)) {
if (a !is null) {
if (b !is null) {
static if (is(typeof(T.opEquals))) {
return cast(bool)a.opEquals(b);
}
else {
return cast(bool)typeid(T).equals(&a, &b);
}
}
return false;
}
if (b !is null) {
return false;
}
return true;
}
else static if (is(T == struct)) {
static if (is(T.opEquals)) {
return cast(bool)a.opEquals(b);
}
else {
return cast(bool)typeid(T).equals(&a, &b);
}
}
else {
return cast(bool)typeid(T).equals(&a, &b);
}
}
private int comparisonImpl(T)(T a, T b) {
static if (is(T : string)) {
return Culture.current.collator.compare(a, b);
}
else static if (is(T == class) || is(T == interface)) {
if (a !is b) {
if (a !is null) {
if (b !is null) {
static if (is(typeof(T.opCmp))) {
return a.opCmp(b);
}
else {
return typeid(T).compare(&a, &b);
}
}
return 1;
}
return -1;
}
return 0;
}
else static if (is(T == struct)) {
static if (is(typeof(T.opCmp))) {
return a.opCmp(b);
}
else {
return typeid(T).compare(&a, &b);
}
}
else {
return typeid(T).compare(&a, &b);
}
}
/*int indexOf(T)(T[] array, T item, EqualityComparison!(T) comparison = null) {
if (comparison is null) {
comparison = (T a, T b) {
return equalityComparisonImpl(a, b);
};
}
for (auto i = 0; i < array.length; i++) {
if (comparison(array[i], item))
return i;
}
return -1;
}*/
/**
* Defines methods that compare two objects for equality.
*/
interface IEqualityComparer(T) {
/**
* Determines whether the specified objects are equal.
* Params:
* a = The first object to compare.
* b = The second object to compare.
* Returns: true if the specified objects are equal; otherwise, false.
*/
bool equals(T a, T b);
/**
* Retrieves a hash code for the specified object.
* Params: value = The object for which a hash code is to be retrieved.
* Returns: The hash code for the specified object.
*/
uint getHash(T value);
}
/**
* Provides a base class for implementations of the IEqualityComparer(T) interface.
*/
abstract class EqualityComparer(T) : IEqualityComparer!(T) {
/**
* $(I Property.) Returns a default equality comparer for the type specified by the template parameter.
*/
static EqualityComparer instance() {
static EqualityComparer instance_;
if (instance_ is null) {
instance_ = new class EqualityComparer {
bool equals(T a, T b) {
return equalityComparisonImpl(a, b);
}
uint getHash(T value) {
return typeid(T).getHash(&value);
}
};
}
return instance_;
}
/**
* Determines whether the specified objects are equal.
* Params:
* a = The first object to compare.
* b = The second object to compare.
* Returns: true if the specified objects are equal; otherwise, false.
*/
abstract bool equals(T a, T b);
/**
* Retrieves a hash code for the specified object.
* Params: value = The object for which a hash code is to be retrieved.
* Returns: The hash code for the specified object.
*/
abstract uint getHash(T value);
}
/**
* Defines a method that compares two objects.
*/
interface IComparer(T) {
/**
* Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
* Params:
* a = The first object to _compare.
* b = The second object to _compare.
* Returns:
* $(TABLE $(TR $(TH Value) $(TH Condition))
* $(TR $(TD Less than zero) $(TD a is less than b.))
* $(TR $(TD Zero) $(TD a equals b.))
* $(TR $(TD Greater than zero) $(TD a is greater than b.)))
*/
int compare(T a, T b);
}
/**
* Provides a base class for implementations of the IComparer(T) interface.
*/
abstract class Comparer(T) : IComparer!(T) {
/**
* $(I Property.) Retrieves a default comparer for the type specified by the template parameter.
*/
static Comparer instance() {
static Comparer instance_;
if (instance_ is null) {
instance_ = new class Comparer {
int compare(T a, T b) {
return comparisonImpl(a, b);
}
};
}
return instance_;
}
/**
* Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
* Params:
* a = The first object to _compare.
* b = The second object to _compare.
* Returns:
* $(TABLE $(TR $(TH Value) $(TH Condition))
* $(TR $(TD Less than zero) $(TD a is less than b.))
* $(TR $(TD Zero) $(TD a equals b.))
* $(TR $(TD Greater than zero) $(TD a is greater than b.)))
*/
abstract int compare(T a, T b);
}
/**
* Sorts the elements int a range of element in an _array using the specified Comparison(T).
* Params:
* array = The _array to _sort.
* index = The starting _index of the range to _sort.
* length = The number of elements in the range to _sort.
* comparison = The Comparison(T) to use when comparing element.
*/
void sort(T, TIndex = size_t, TLength = TIndex)(T[] array, TIndex index, TLength length, int delegate(T, T) comparison = null) {
void quickSortImpl(size_t left, size_t right) {
if (left >= right)
return;
TLength i = left, j = right;
T pivot = array[i + ((j - i) >> 1)];
do {
while (i < right && comparison(array[i], pivot) < 0)
i++;
while (j > left && comparison(pivot, array[j]) < 0)
j--;
assert(i >= left && j <= right);
if (i <= j) {
T temp = array[j];
array[j] = array[i];
array[i] = temp;
i++;
j--;
}
} while (i <= j);
if (left < j)
quickSortImpl(left, j);
if (i < right)
quickSortImpl(i, right);
}
if (comparison is null) {
comparison = (T a, T b) {
return comparisonImpl(a, b);
};
}
quickSortImpl(index, index + length - 1);
}
/**
*/
void sort(T)(T[] array, int delegate(T, T) comparison = null) {
.sort(array, 0, array.length, comparison);
}
/**
* Searches a range of elements in an _array for a value using the specified Comparison(T).
*
* TODO: deprecated use std.algorithms.binarySearch
* Params:
* array = The _array to search.
* index = The starting _index of the range to search.
* length = The number of elements in the range to search.
* comparison = The Comparison(T) to use when comparing elements.
*/
sizediff_t binarySearch(T, TIndex = size_t, TLength = TIndex)(T[] array, TIndex index, TLength length, T value, int delegate(T, T) comparison = null) {
if (comparison is null) {
comparison = (T a, T b) {
return comparisonImpl(a, b);
};
}
auto lo = to!(sizediff_t)(index);
auto hi = to!(sizediff_t)((index + length - 1));
while (lo <= hi) {
auto i = lo + ((hi - lo) >> 1);
auto order = comparison(array[i], value);
if (order == 0)
return i;
if (order < 0)
lo = i + 1;
else
hi = i - 1;
}
return ~lo;
}
void reverse(T, TIndex = int, TLength = TIndex)(T[] array, TIndex index, TLength length) {
auto i = index;
auto j = index + length - 1;
while (i < j) {
T temp = array[i];
array[i] = array[j];
array[j] = temp;
i++, j--;
}
}
/**
*/
void copy(T, TIndex = int, TLength = TIndex)(T[] source, TIndex sourceIndex, T[] target, TIndex targetIndex, TLength length) {
if (length > 0)
memmove(target.ptr + targetIndex, source.ptr + sourceIndex, length * T.sizeof);
}
void clear(T, TIndex = int, TLength = IIndex)(T[] array, TIndex index, TLength length) {
if (length > 0)
memset(array.ptr + index, 0, length * T.sizeof);
}
TOutput[] convertAll(TInput, TOutput)(TInput[] array, Converter!(TInput, TOutput) converter) {
auto ret = new TOutput[array.length];
for (auto i = 0; i < array.length; i++) {
ret[i] = converter(array[i]);
}
return ret;
}
interface IEnumerable(T) {
version (UseRanges) {
bool empty();
void popFront();
T front();
}
else {
int opApply(int delegate(ref T) action);
}
}
/**
* Defines methods to manipulate collections.
*/
interface ICollection(T) : IEnumerable!(T) {
/**
* Adds an _item to the collection.
* Params: item = The object to _add.
*/
void add(T item);
/**
* Removes the first occurence of the specified object from the collection.
* Params: item = The object to _remove.
* Returns: true if item was successfully removed; otherwise, false.
*/
bool remove(T item);
/**
* Determines whether the collection _contains the specified object.
* Params: item = The object to locate.
* Returns: true if item was found; otherwise, false.
*/
bool contains(T item);
/**
* Removes all items from the collection.
*/
void clear();
/**
* $(I Property.) Gets the number of elements in the collection.
*/
@property size_t count();
}
/**
* Represents a collection of objects that can be accessed by index.
*/
interface IList(T) : ICollection!(T) {
size_t indexOf(T item);
/**
* Inserts an _item at the specified _index.
* Params:
* index = The _index at which item should be inserted.
* item = The object to insert.
*/
void insert(size_t index, T item);
/**
* Removes the item at the specified _index.
* Params: index = The _index of the item to remove.
*/
void removeAt(size_t index);
/**
* Gets or sets the object at the specified _index.
* Params:
* value = The item at the specified _index.
* index = The _index of the item to get or set.
*/
void opIndexAssign(T value, size_t index);
/**
* ditto
*/
T opIndex(size_t index);
}
/**
* Represents a list of elements that can be accessed by index.
*/
class List(T) : IList!(T) {
private const int DEFAULT_CAPACITY = 4;
private T[] items_;
private size_t size_;
private size_t index_;
/**
* Initializes a new instance with the specified _capacity.
* Params: capacity = The number of elements the new list can store.
*/
this(size_t capacity = 0) {
items_.length = capacity;
}
/**
* Initializes a new instance containing elements copied from the specified _range.
* Params: range = The _range whose elements are copied to the new list.
*/
this(T[] range) {
items_.length = size_ = range.length;
items_ = range;
}
/**
* ditto
*/
this(IEnumerable!(T) range) {
items_.length = DEFAULT_CAPACITY;
foreach (item; range)
add(item);
}
/**
* Adds an element to the end of the list.
* Params: item = The element to be added.
*/
final void add(T item) {
if (size_ == items_.length)
ensureCapacity(size_ + 1);
items_[size_++] = item;
}
/**
* Adds the elements in the specified _range to the end of the list.
* Params: The _range whose elements are to be added.
*/
final void addRange(T[] range) {
insertRange(size_, range);
}
/**
* ditto
*/
final void addRange(IEnumerable!(T) range) {
insertRange(size_, range);
}
/**
* Inserts an element into the list at the specified _index.
* Params:
* index = The _index at which item should be inserted.
* item = The element to insert.
*/
final void insert(size_t index, T item) {
if (size_ == items_.length)
ensureCapacity(size_ + 1);
if (index < size_)
.copy(items_, index, items_, index + 1, size_ - index);
items_[index] = item;
size_++;
}
/**
* Inserts the elements of a _range into the list at the specified _index.
* Params:
* index = The _index at which the new elements should be inserted.
* range = The _range whose elements should be inserted into the list.
*/
final void insertRange(size_t index, T[] range) {
foreach (item; range) {
insert(index++, item);
}
}
/**
* ditto
*/
final void insertRange(size_t index, IEnumerable!(T) range) {
foreach (item; range) {
insert(index++, item);
}
}
/**
*/
final bool remove(T item) {
size_t index = indexOf(item);
if (index < 0)
return false;
removeAt(index);
return true;
}
final void removeAt(size_t index) {
size_--;
if (index < size_)
.copy(items_, index + 1, items_, index, size_ - index);
items_[size_] = T.init;
}
/**
*/
final void removeRange(size_t index, size_t count) {
if (count > 0) {
size_ -= count;
if (index < size_)
.copy(items_, index + count, items_, index, size_ - index);
.clear(items_, size_, count);
}
}
/**
*/
final bool contains(T item) {
for (auto i = 0; i < size_; i++) {
if (equalityComparisonImpl(items_[i], item))
return true;
}
return false;
}
/**
*/
final void clear() {
if (size_ > 0) {
.clear(items_, 0, size_);
size_ = 0;
}
}
/**
*/
final size_t indexOf(T item) {
return indexOf(item, null);
}
/**
*/
final size_t indexOf(T item, EqualityComparison!(T) comparison) {
if (comparison is null) {
comparison = (T a, T b) {
return equalityComparisonImpl(a, b);
};
}
for (auto i = 0; i < size_; i++) {
if (comparison(items_[i], item))
return i;
}
return -1;
}
/**
*/
final size_t lastIndexOf(T item, EqualityComparison!(T) comparison = null) {
if (comparison is null) {
comparison = (T a, T b) {
return equalityComparisonImpl(a, b);
};
}
for (auto i = size_ - 1; i >= 0; i--) {
if (comparison(items_[i], item))
return i;
}
return -1;
}
/**
*/
final void sort(Comparison!(T) comparison = null) {
.sort(items_, 0, size_, comparison);
}
/**
*/
final sizediff_t binarySearch(T item, Comparison!(T) comparison = null) {
return .binarySearch(items_, 0, size_, item, comparison);
}
/**
*/
final void copyTo(T[] array) {
.copy(items_, 0, array, 0, size_);
}
/**
*/
final T[] toArray() {
return items_[0 .. size_].dup;
}
/**
*/
final T find(Predicate!(T) match) {
for (auto i = 0; i < size_; i++) {
if (match(items_[i]))
return items_[i];
}
return T.init;
}
/**
*/
final T findLast(Predicate!(T) match) {
for (auto i = size_ - 1; i >= 0; i--) {
if (match(items_[i]))
return items_[i];
}
return T.init;
}
/**
*/
final List findAll(Predicate!(T) match) {
auto list = new List;
for (auto i = 0; i < size_; i++) {
if (match(items_[i]))
list.add(items_[i]);
}
return list;
}
/**
*/
final size_t findIndex(Predicate!(T) match) {
for (auto i = 0; i < size_; i++) {
if (match(items_[i]))
return i;
}
return -1;
}
/**
*/
final size_t findLastIndex(Predicate!(T) match) {
for (auto i = size_ - 1; i >= 0; i--) {
if (match(items_[i]))
return i;
}
return -1;
}
/**
*/
final bool exists(Predicate!(T) match) {
return findIndex(match) != -1;
}
/**
*/
final void forEach(Action!(T) action) {
for (auto i = 0; i < size_; i++) {
action(items_[i]);
}
}
/**
*/
final bool trueForAll(Predicate!(T) match) {
for (auto i = 0; i < size_; i++) {
if (!match(items_[i]))
return false;
}
return true;
}
/**
*/
final List!(T) getRange(size_t index, size_t count) {
auto list = new List!(T)(count);
list.items_[0 .. count] = items_[index .. index + count];
list.size_ = count;
return list;
}
/**
*/
final List!(TOutput) convert(TOutput)(Converter!(T, TOutput) converter) {
auto list = new List!(TOutput)(size_);
for (auto i = 0; i < size_; i++) {
list.items_[i] = converter(items_[i]);
}
list.size_ = size_;
return list;
}
final size_t count() {
return size_;
}
final @property void capacity(size_t value) {
items_.length = value;
}
final @property size_t capacity() {
return items_.length;
}
final void opIndexAssign(T value, size_t index) {
if (index >= size_)
throw new ArgumentOutOfRangeException("index");
items_[index] = value;
}
final T opIndex(size_t index) {
if (index >= size_)
throw new ArgumentOutOfRangeException("index");
return items_[index];
}
version (UseRanges) {
final bool empty() {
bool result = (index_ == size_);
if (result)
index_ = 0;
return result;
}
final void popFront() {
if (index_ < size_)
index_++;
}
final T front() {
return items_[index_];
}
}
else {
final int opApply(int delegate(ref T) action) {
int r;
for (auto i = 0; i < size_; i++) {
if ((r = action(items_[i])) != 0)
break;
}
return r;
}
/**
* Ditto
*/
final int opApply(int delegate(ref int, ref T) action) {
int r;
for (auto i = 0; i < size_; i++) {
if ((r = action(i, items_[i])) != 0)
break;
}
return r;
}
}
final bool opIn_r(T item) {
return contains(item);
}
private void ensureCapacity(size_t min) {
if (items_.length < min) {
size_t n = (items_.length == 0) ? DEFAULT_CAPACITY : items_.length * 2;
if (n < min)
n = min;
this.capacity = n;
}
}
}
/**
*/
class ReadOnlyList(T) : IList!(T) {
private List!(T) list_;
this(List!(T) list) {
list_ = list;
}
final size_t indexOf(T item) {
return list_.indexOf(item);
}
final bool contains(T item) {
return list_.contains(item);
}
final void clear() {
list_.clear();
}
final size_t count() {
return list_.count;
}
final T opIndex(size_t index) {
return list_[index];
}
version (UseRanges) {
final bool empty() {
return list_.empty;
}
final void popFront() {
list_.popFront();
}
final T front() {
return list_.front;
}
}
else {
final int opApply(int delegate(ref T) action) {
return list_.opApply(action);
}
}
protected void add(T item) {
throw new NotSupportedException;
}
protected void insert(size_t index, T item) {
throw new NotSupportedException;
}
protected bool remove(T item) {
throw new NotSupportedException;
}
protected void removeAt(size_t index) {
throw new NotSupportedException;
}
protected void opIndexAssign(T item, size_t index) {
throw new NotSupportedException;
}
protected final IList!(T) list() {
return list_;
}
}
class Collection(T) : IList!(T) {
private IList!(T) items_;
this() {
this(new List!(T));
}
this(IList!(T) list) {
items_ = list;
}
final void add(T item) {
insertItem(items_.count, item);
}
final void insert(size_t index, T item) {
insertItem(index, item);
}
final bool remove(T item) {
size_t index = items_.indexOf(item);
if (index < 0)
return false;
removeItem(index);
return true;
}
final void removeAt(size_t index) {
removeItem(index);
}
final void clear() {
clearItems();
}
final size_t indexOf(T item) {
return items_.indexOf(item);
}
final bool contains(T item) {
return items_.contains(item);
}
@property final size_t count() {
return items_.count;
}
final void opIndexAssign(T value, size_t index) {
setItem(index, value);
}
final T opIndex(size_t index) {
return items_[index];
}
version (UseRanges) {
final bool empty() {
return items_.empty;
}
final void popFront() {
items_.popFront();
}
final T front() {
return items_.front;
}
}
else {
final int opApply(int delegate(ref T) action) {
return items_.opApply(action);
}
}
protected void insertItem(size_t index, T item) {
items_.insert(index, item);
}