-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.h
2429 lines (2237 loc) · 75.4 KB
/
array.h
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
/**
* \file array.h
*
* This file contains the implementation of the `array` data structure, which
* is a resizeable ordered collection of elements with generic type `T`. In
* addition, the file contains global helper functions for working with arrays,
* such as resizing, linear and binary search, insertion sort, and quicksort.
* The `pair` structure is also defined here.
*
* This file also contains functions that perform set operations on sorted
* arrays and native arrays, such as union, intersection, subtraction, and
* subset.
*
* <!-- Created on: Mar 3, 2012
* Author: asaparov -->
*/
#ifndef ARRAY_H_
#define ARRAY_H_
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <core/core.h>
/**
* The multiplicative factor by which array capacity is changed.
*/
#define RESIZE_FACTOR 2
namespace core {
namespace detail {
template<typename C> static auto test_resizeable(int32_t) ->
decltype(void(std::declval<C>().on_resize()), std::true_type{});
template<typename C> static auto test_resizeable(int64_t) -> std::false_type;
}
/**
* This type trait is [true_type](http://en.cppreference.com/w/cpp/types/integral_constant)
* if and only if `T` is class with a public function `void on_resize()`.
*/
template<typename T> struct is_resizeable : decltype(core::detail::test_resizeable<T>(0)){};
/**
* Resizes the given native array `data` with the requested capacity `new_capacity`.
* \tparam SizeType a type that satisfies [is_integral](http://en.cppreference.com/w/cpp/types/is_integral).
* \tparam T the generic type of the elements in `data`.
* \param data the array to resize.
* \param new_capacity the requested size.
* \return `true` on success; `data` may point to a different memory address.
* \return `false` on failure; `data` is unchanged.
*/
template<typename T, typename SizeType,
typename std::enable_if<std::is_integral<SizeType>::value>::type* = nullptr>
inline bool resize(T*& data, const SizeType& new_capacity) {
T* new_data = (T*) realloc(static_cast<void*>(data), new_capacity * sizeof(T));
if (new_data == NULL) {
fprintf(stderr, "resize ERROR: Out of memory.\n");
return false;
}
data = new_data;
return true;
}
/**
* This function multiplies `capacity` by #RESIZE_FACTOR. It then repeats this
* until `capacity >= new_length`.
*/
template<typename SizeType, typename std::enable_if<std::is_integral<SizeType>::value>::type* = nullptr>
inline void expand_capacity(SizeType& capacity, size_t new_length) {
do {
/* increase the size of the underlying array */
capacity *= RESIZE_FACTOR;
} while (new_length > capacity);
}
/**
* For a given requested length `new_length`, this function calls
* expand_capacity() to determine the new `capacity` of the native array
* `data`. The function then attempts to resize the array with this capacity.
* Note this function does not check whether `new_length <= capacity`.
*/
template<typename T, typename SizeType,
typename std::enable_if<std::is_integral<SizeType>::value>::type* = nullptr>
inline bool expand(T*& data, SizeType& capacity, size_t new_length) {
expand_capacity(capacity, new_length);
return resize(data, capacity);
}
/**
* For a given requested length `new_length`, this function expands the native
* array `data` by factors of #RESIZE_FACTOR until `capacity >= new_length`.
* If initially `new_length <= capacity`, this function does nothing.
*/
template<typename T, typename SizeType,
typename std::enable_if<std::is_integral<SizeType>::value>::type* = nullptr>
inline bool ensure_capacity(T*& data, SizeType& capacity, size_t new_length)
{
if (new_length <= capacity)
return true;
SizeType new_capacity = capacity;
if (!expand(data, new_capacity, new_length))
return false;
capacity = new_capacity;
return true;
}
/**
* Performs a linear search through the array `data` to find the smallest index
* `i >= start` such that `element == data[i]`.
* \tparam Key a generic type for which operator `==` is defined for arguments of type `Key` and `T`.
* \tparam T a generic type for which operator `==` is defined for arguments of type `Key` and `T`.
* \return an index in `start, start + 1, ..., length - 1` if the element was found.
* \return `length` if the element was not found.
*/
template<typename Key, typename T, typename SizeType,
typename std::enable_if<std::is_integral<SizeType>::value>::type* = nullptr>
inline SizeType index_of(const Key& element, const T* data,
const SizeType& length, const SizeType& start = 0)
{
for (SizeType i = start; i < length; i++)
if (element == data[i])
return i;
return length;
}
/**
* Performs a linear search through the array `data` to find the largest index
* `i` such that `element == data[i]`.
* \tparam Key a generic type for which operator `==` is defined for arguments of type `Key` and `T`.
* \tparam T a generic type for which operator `==` is defined for arguments of type `Key` and `T`.
* \return an index in `0, 1, ..., length - 1` if the element was found.
* \return `static_cast<unsigned int>(-1)` if the element was not found.
*/
template<typename Key, typename T, typename SizeType,
typename std::enable_if<std::is_integral<SizeType>::value>::type* = nullptr>
inline unsigned int last_index_of(const Key& element, const T* data, const SizeType& length)
{
unsigned int i = length;
while (i != 0) {
i--;
if (element == data[i])
return i;
}
return static_cast<unsigned int>(-1);
}
/**
* A resizeable sequence of objects, stored contiguously, each with generic
* type `T`. This structure does not automatically initialize or free its
* elements, and so the user must appropriately free each element before the
* array is destroyed.
*
* In the following example, we demonstrate a simple use-case of array. Here,
* `a` is automatically freed by the destructor since it was initialized on the
* stack. The expected output is `-1 -1 0 3 `.
*
* ```{.cpp}
* #include <core/array.h>
* #include <stdio.h>
* using namespace core;
*
* int main() {
* array<int> a = array<int>(8);
* a.add(-1); a.add(-4);
* a.add(3); a.add(0);
* a.remove(1);
*
* printf("%d ", a[0]);
* for (int element : a)
* printf("%d ", element);
* }
* ```
*
*
* However, if `a` is not allocated on the stack, the destructor will not be
* automatically called, and so it must be freed manually using `core::free` or
* `array::free`.
*
* ```{.cpp}
* #include <core/array.h>
* #include <stdio.h>
* using namespace core;
*
* int main() {
* array<int>& a = *((array<int>*) alloca(sizeof(array<int>)));
* array_init(a, 8);
* a.add(-1); a.add(-4);
* a.add(3); a.add(0);
* a.remove(1);
*
* printf("%d ", a[0]);
* for (int element : a)
* printf("%d ", element);
* free(a);
* }
* ```
*
*
* Also note that a number of member functions require that `T` be
* [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable).
* In other cases, elements should be added manually to the underlying native
* array array::data. This structure also defines array::begin and array::end,
* similar to Standard Template Library iterators, which enables the use of the
* range-based for loop in the example below. In this example, the expected
* output is `first second `.
*
* ```{.cpp}
* #include <core/array.h>
* #include <stdio.h>
* #include <string.h>
* using namespace core;
*
* struct custom_string {
* char* buffer;
*
* static void free(custom_string& s) {
* core::free(s.buffer);
* }
* };
*
* bool init(custom_string& s, const char* src) {
* s.buffer = (char*) malloc(sizeof(char) * (strlen(src) + 1));
* if (s.buffer == NULL)
* return false;
* memcpy(s.buffer, src, sizeof(char) * (strlen(src) + 1));
* return true;
* }
*
* int main() {
* array<custom_string> a = array<custom_string>(8);
* init(a[0], "first");
* init(a[1], "second");
* a.length = 2;
*
* for (const custom_string& s : a)
* printf("%s ", s.buffer);
* for (custom_string& s : a)
* free(s);
* }
* ```
* Note in the above example that since the array struct does not automatically
* free its elements, they must be freed manually.
*/
template<typename T>
struct array {
/**
* The underlying native array of elements.
*/
T* data;
/**
* The length of the array.
*/
size_t length;
/**
* The capacity of array::data.
*/
size_t capacity;
/**
* Constructs an array with zero size and the given `initial_capacity`.
*/
array(size_t initial_capacity)
{
if (!initialize(initial_capacity))
exit(EXIT_FAILURE);
}
~array() { free(); }
/**
* Returns a reference to the element at the given `index`. No bounds-checking is performed.
*/
inline T& operator[] (size_t index) {
return data[index];
}
/**
* Returns a const reference to the element at the given `index`. No bounds-checking is performed.
*/
inline const T& operator[] (size_t index) const {
return data[index];
}
/**
* Sets the length of the array to `0`. Any elements are not freed.
*/
inline void clear()
{
length = 0;
}
/**
* Moves the last element in the array to the position given by `index` and
* decrements array::length by `1`. The element initially at `index` is not
* freed.
*/
void remove(size_t index)
{
core::move(data[length - 1], data[index]);
length--;
}
/**
* For a given requested length `new_length`, this function expands the
* array by factors of #RESIZE_FACTOR until `array::capacity >= new_length`.
* If initially `new_length <= array::capacity`, this function does
* nothing. If the resize operation moved array::data to a new memory
* address, and `T` satisfies is_resizeable, then `x.on_resize()` is called
* for every element `x` in the array.
*/
template<typename C = T, typename std::enable_if<std::is_same<C, T>::value && is_resizeable<C>::value>::type* = nullptr>
bool ensure_capacity(size_t new_length) {
const T* old_data = data;
if (!core::ensure_capacity(data, capacity, new_length)) return false;
if (data != old_data) {
for (unsigned int i = 0; i < length; i++)
data[i].on_resize();
}
return true;
}
template<typename C = T, typename std::enable_if<std::is_same<C, T>::value && !is_resizeable<C>::value>::type* = nullptr>
bool ensure_capacity(size_t new_length) {
return core::ensure_capacity(data, capacity, new_length);
}
/**
* Adds the given native array of elements to this structure. This function
* uses [memcpy](http://en.cppreference.com/w/cpp/string/byte/memcpy),
* and so it should not be used if the elements are not
* [TriviallyCopyable](https://en.cppreference.com/w/cpp/named_req/TriviallyCopyable).
* In such a case, addition should be performed manually using the public fields.
* \tparam T is [TriviallyCopyable](https://en.cppreference.com/w/cpp/named_req/TriviallyCopyable).
*/
bool append(const T* elements, size_t size)
{
if (!ensure_capacity(length + size))
return false;
memcpy(&data[length], elements, sizeof(T) * size);
length += size;
return true;
}
/**
* Calls array::index_of to determine whether `element` exists in this array.
* \tparam Key a generic type for which operator `==` is defined for arguments of type `Key` and `T`.
*/
template<typename Key>
inline bool contains(const Key& element) const {
return index_of(element) < length;
}
/**
* Performs a linear search of the array to find the smallest index `i`
* such that `element == array::data[i]`.
* \tparam Key a generic type for which operator `==` is defined for arguments of type `Key` and `T`.
* \return an index in `0, 1, ..., array::length - 1` if the element was found.
* \return `array::length` if the element was not found.
*/
template<typename Key>
inline unsigned int index_of(const Key& element) const {
return core::index_of(element, data, (unsigned int) length);
}
/**
* Performs a linear search through the array to find the smallest index
* `i >= start` such that `element == array::data[i]`.
* \tparam Key a generic type for which operator `==` is defined for arguments of type `Key` and `T`.
* \return an index in `start, start + 1, ..., length - 1` if the element was found.
* \return `length` if the element was not found.
*/
template<typename Key, typename SizeType>
inline unsigned int index_of(const Key& element, SizeType start) const {
return core::index_of(element, data, (unsigned int) length, start);
}
/**
* Returns a reference to `array::data[0]`, ignoring any bounds.
*/
T& first()
{
return data[0];
}
/**
* Returns a const reference to `array::data[0]`, ignoring any bounds.
*/
const T& first() const
{
return data[0];
}
/**
* Returns a reference to `array::data[array::length - 1]`, ignoring any bounds.
*/
T& last()
{
return data[length - 1];
}
/**
* Returns a const reference to `array::data[array::length - 1]`, ignoring any bounds.
*/
const T& last() const
{
return data[length - 1];
}
/**
* Adds the given element to this array, increasing its capacity if
* necessary. The assignment operator performs the addition, and so this
* function should not be used if `T` is not
* [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable).
* In such a case, addition should be performed manually using the public fields.
* \tparam T is [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable).
*/
bool add(const T& element)
{
if (!ensure_capacity(length + 1))
return false;
data[length] = element;
length++;
return true;
}
/**
* Returns the element at `array::length - 1` and decrements array::length
* by `1`, ignoring any bounds.
*/
T pop()
{
length--;
return data[length];
}
/**
* Returns an iterator to the beginning of the array.
*/
inline T* begin() {
return data;
}
/**
* Returns an iterator to the end of the array.
*/
inline T* end() {
return data + length;
}
/**
* Returns a const iterator to the beginning of the array.
*/
inline const T* begin() const {
return data;
}
/**
* Returns a const iterator to the end of the array.
*/
inline const T* end() const {
return data + length;
}
/**
* Copies the underlying fields from `src` into `dst`, effectively moving
* the array from `src` into `dst`.
*/
static inline void move(const array<T>& src, array<T>& dst) {
dst.length = src.length;
dst.capacity = src.capacity;
dst.data = src.data;
}
template<typename Metric>
static inline long unsigned int size_of(const array<T>& a, const Metric& metric) {
long unsigned int sum = core::size_of(a.capacity) + core::size_of(a.length);
for (unsigned int i = 0; i < a.length; i++)
sum += core::size_of(a.data[i], metric);
return sum + (a.capacity - a.length) * sizeof(T);
}
/**
* Frees array::data. This should not be used if `a` was constructed on the
* stack, as the destructor will automatically free array::data. The
* elements of `a` are not freed.
*/
static inline void free(array<T>& a) { a.free(); }
private:
inline bool initialize(size_t initial_capacity)
{
#if !defined(NDEBUG)
if (initial_capacity == 0)
fprintf(stderr, "array.initialize WARNING: Initial capacity is zero.\n");
#endif
capacity = initial_capacity;
length = 0;
data = (T*) malloc(sizeof(T) * capacity);
if (data == NULL) {
fprintf(stderr, "array.initialize ERROR: Out of memory.\n");
return false;
}
return true;
}
inline void free() {
if (data != NULL) {
core::free(data);
data = NULL;
}
}
template<typename K>
friend bool array_init(array<K>& m, size_t initial_capacity);
};
/**
* Initializes the array `m` with the given `initial_capacity`.
*/
template<typename T>
bool array_init(array<T>& m, size_t initial_capacity) {
return m.initialize(initial_capacity);
}
/**
* Returns array::length of `m`.
*/
template<typename T>
inline size_t size(const array<T>& m) {
return m.length;
}
/**
* Swaps the underlying buffers of the given arrays.
*/
template<typename T>
inline void swap(array<T>& a, array<T>& b)
{
T* temp = a.data;
a.data = b.data;
b.data = temp;
size_t swap = a.length;
a.length = b.length;
b.length = swap;
swap = a.capacity;
a.capacity = b.capacity;
b.capacity = swap;
}
template<typename T, typename Metric>
inline long unsigned int size(const array<T>& a, const Metric& metric) {
long unsigned int sum = size(a.capacity) + size(a.length);
for (unsigned int i = 0; i < a.length; i++)
sum += size(a.data[i], metric);
return sum + (a.capacity - a.length) * sizeof(T);
}
/**
* Compares the two given arrays and returns true if and only if `a.length == b.length`
* and there is no index `i` such that `a.data[i] != b.data[i]`.
*/
template<typename T>
inline bool operator == (const array<T>& a, const array<T>& b) {
if (a.length != b.length)
return false;
for (unsigned int i = 0; i < a.length; i++)
if (a.data[i] != b.data[i])
return false;
return true;
}
/**
* Compares the two given arrays and returns true if and only if `a.length != b.length`
* or there is some index `i` such that `a.data[i] != b.data[i]`.
*/
template<typename T>
inline bool operator != (const array<T>& a, const array<T>& b) {
if (a.length != b.length)
return true;
for (unsigned int i = 0; i < a.length; i++)
if (a.data[i] != b.data[i])
return true;
return false;
}
/**
* Performs insertion sort on the given native array `keys` with given `length`.
* \tparam T is [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable),
* [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible),
* and [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable).
*/
template<typename T>
void insertion_sort(T* keys, unsigned int length)
{
for (unsigned int i = 1; i < length; i++) {
T item = keys[i];
unsigned int hole = i;
while (hole > 0 && item < keys[hole - 1]) {
keys[hole] = keys[hole - 1];
hole--;
}
keys[hole] = item;
}
}
/**
* Performs insertion sort on the given native array `keys` with given `length`
* and `sorter`.
* \tparam T satisfies is_moveable, and for which a function
* `bool less_than(const T&, const T&, const Sorter&)` is defined.
*/
template<typename T, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
void insertion_sort(T* keys, unsigned int length, const Sorter& sorter)
{
T& item = *((T*) malloc(sizeof(T)));
for (unsigned int i = 1; i < length; i++) {
move(keys[i], item);
unsigned int hole = i;
while (hole > 0 && less_than(item, keys[hole - 1], sorter)) {
move(keys[hole - 1], keys[hole]);
hole--;
}
move(item, keys[hole]);
}
free(&item);
}
/**
* Performs insertion sort on the given native arrays `keys` and `values` with given `length`.
* \tparam K is [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable),
* [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible),
* and [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable).
* \tparam V is [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable),
* [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible).
*/
template<typename K, typename V>
void insertion_sort(K* keys, V* values, unsigned int length)
{
for (unsigned int i = 1; i < length; i++) {
K item = keys[i];
V value = values[i];
unsigned int hole = i;
while (hole > 0 && item < keys[hole - 1]) {
keys[hole] = keys[hole - 1];
values[hole] = values[hole - 1];
hole--;
}
keys[hole] = item;
values[hole] = value;
}
}
/**
* Performs insertion sort on the given native arrays `keys` and `values` with
* given `length` and `sorter`.
* \tparam K satisfies is_moveable, and for which a function
* `bool less_than(const K&, const K&, const Sorter&)` is defined.
* \tparam V satisfies is_moveable.
*/
template<typename K, typename V, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
void insertion_sort(K* keys, V* values, unsigned int length, const Sorter& sorter)
{
K& item = *((K*) malloc(sizeof(K)));
V& value = *((V*) malloc(sizeof(V)));
for (unsigned int i = 1; i < length; i++) {
move(keys[i], item);
move(values[i], value);
unsigned int hole = i;
while (hole > 0 && less_than(item, keys[hole - 1], sorter)) {
move(keys[hole - 1], keys[hole]);
move(values[hole - 1], values[hole]);
hole--;
}
move(item, keys[hole]);
move(value, values[hole]);
}
free(&item); free(&value);
}
/**
* Performs insertion sort on the given array `keys`.
* \tparam T is [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable),
* [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible),
* and [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable).
*/
template<typename T>
inline void insertion_sort(array<T>& keys) {
insertion_sort(keys.data, (unsigned int) keys.length);
}
/**
* Performs insertion sort on the given array `keys` with the given `sorter`.
* \tparam T satisfies is_moveable, and for which a function
* `bool less_than(const T&, const T&, const Sorter&)` is defined.
*/
template<typename T, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
inline void insertion_sort(array<T>& keys, const Sorter& sorter)
{
insertion_sort(keys.data, (unsigned int) keys.length, sorter);
}
/**
* Performs insertion sort on the given arrays `keys` and `values`.
* \tparam K is [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable),
* [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible),
* and [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable).
* \tparam V is [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable),
* [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible).
*/
template<typename K, typename V>
inline void insertion_sort(array<K>& keys, array<V>& values) {
insertion_sort(keys.data, values.data, (unsigned int) keys.length);
}
/**
* Performs insertion sort on the given arrays `keys` and `values` with the
* given `sorter`.
* \tparam K satisfies is_moveable, and for which a function
* `bool less_than(const K&, const K&, const Sorter&)` is defined.
* \tparam V satisfies is_moveable.
*/
template<typename K, typename V, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
inline void insertion_sort(array<K>& keys, array<V>& values, const Sorter& sorter)
{
insertion_sort(keys.data, values.data, (unsigned int) keys.length, sorter);
}
/**
* Reverses the order of the elements in the given native `array` with given `length`.
* \tparam T satisfies is_swappable.
*/
template<typename T>
void reverse(T* array, unsigned int length) {
for (unsigned int i = 0; i < length / 2; i++) {
unsigned int other = length - i - 1;
swap(array[i], array[other]);
}
}
/**
* Reverses the order of the elements in the given `array`.
* \tparam T satisfies is_swappable.
*/
template<typename T>
inline void reverse(array<T>& array) {
reverse(array.data, (unsigned int) array.length);
}
template<typename T>
inline const T& get_pivot(T* array, unsigned int start, unsigned int end) {
return array[(end + start) / 2];
}
template<typename T>
inline void quick_sort_partition(T* array,
unsigned int start, unsigned int end,
unsigned int& l, unsigned int& r)
{
const T p = get_pivot(array, start, end);
while (true) {
while (array[l] < p)
l++;
while (p < array[r])
r--;
if (l == r) {
l++;
if (r > 0) r--;
return;
} else if (l > r) return;
swap(array[l++], array[r--]);
}
}
template<typename T, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
inline void quick_sort_partition(
T* array, unsigned int start, unsigned int end,
unsigned int& l, unsigned int& r, const Sorter& sorter)
{
T& p = *((T*) malloc(sizeof(T)));
move(get_pivot(array, start, end), p);
while (true) {
while (less_than(array[l], p, sorter))
l++;
while (less_than(p, array[r], sorter))
r--;
if (l == r) {
l++;
if (r > 0) r--;
break;
} else if (l > r) break;
swap(array[l++], array[r--]);
}
free(&p);
}
template<typename K, typename V>
inline void quick_sort_partition(K* keys, V* values,
unsigned int start, unsigned int end, unsigned int& l, unsigned int& r)
{
const K p = get_pivot(keys, start, end);
while (true) {
while (keys[l] < p)
l++;
while (p < keys[r])
r--;
if (l == r) {
l++;
if (r > 0) r--;
return;
} else if (l > r) return;
swap(values[l], values[r]);
swap(keys[l++], keys[r--]);
}
}
template<typename K, typename V, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
inline void quick_sort_partition(
K* keys, V* values, unsigned int start, unsigned int end,
unsigned int& l, unsigned int& r, const Sorter& sorter)
{
K& p = *((K*) malloc(sizeof(K)));
move(get_pivot(keys, start, end), p);
while(true) {
while (less_than(keys[l], p, sorter))
l++;
while (less_than(p, keys[r], sorter))
r--;
if (l == r) {
l++;
if (r > 0) r--;
break;
} else if (l > r) break;
swap(values[l], values[r]);
swap(keys[l++], keys[r--]);
}
free(&p);
}
template<typename T>
void quick_sort(T* array, unsigned int start, unsigned int end)
{
if (start >= end)
return;
unsigned int l = start, r = end;
quick_sort_partition(array, start, end, l, r);
quick_sort(array, start, r);
quick_sort(array, l, end);
}
template<typename T, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
void quick_sort(T* array, unsigned int start, unsigned int end, const Sorter& sorter)
{
if (start >= end)
return;
unsigned int l = start, r = end;
quick_sort_partition(array, start, end, l, r, sorter);
quick_sort(array, start, r, sorter);
quick_sort(array, l, end, sorter);
}
template<typename K, typename V>
void quick_sort(K* keys, V* values, unsigned int start, unsigned int end)
{
if (start >= end)
return;
unsigned int l = start, r = end;
quick_sort_partition(keys, values, start, end, l, r);
quick_sort(keys, values, start, r);
quick_sort(keys, values, l, end);
}
template<typename K, typename V, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
void quick_sort(K* keys, V* values,
unsigned int start, unsigned int end, const Sorter& sorter)
{
if (start >= end)
return;
unsigned int l = start, r = end;
quick_sort_partition(keys, values, start, end, l, r, sorter);
quick_sort(keys, values, start, r, sorter);
quick_sort(keys, values, l, end, sorter);
}
/**
* Performs Quicksort on the given native array `keys` with given `length`.
* This function assumes `length > 0`. If the preprocessor `NDEBUG` is not
* defined, this function outputs a warning when `length == 0`.
* \tparam T is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible),
* [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable),
* and is_swappable.
*/
template<typename T>
inline void quick_sort(T* keys, unsigned int length) {
#if !defined(NDEBUG)
if (length == 0) {
fprintf(stderr, "quick_sort WARNING: Length is zero.\n");
return;
}
#endif
quick_sort(keys, 0, length - 1);
}
/**
* Performs Quicksort on the given native array `keys` with given `length` and
* `sorter`. This function assumes `length > 0`. If the preprocessor `NDEBUG`
* is not defined, this function outputs a warning when `length == 0`.
* \tparam T a generic type that satisfies is_swappable, and for which a
* function `bool less_than(const T&, const T&, const Sorter&)` is
* defined.
*/
template<typename T, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
inline void quick_sort(T* keys, unsigned int length, const Sorter& sorter)
{
#if !defined(NDEBUG)
if (length == 0) {
fprintf(stderr, "quick_sort WARNING: Length is zero.\n");
return;
}
#endif
quick_sort(keys, 0, length - 1, sorter);
}
/**
* Performs Quicksort on the given native arrays `keys` and `values` with given
* `length`. This function assumes `length > 0`. If the preprocessor `NDEBUG`
* is not defined, this function outputs a warning when `length == 0`.
* \tparam K is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible),
* [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable),
* and is_swappable.
* \tparam V satisfies is_swappable.
*/
template<typename K, typename V>
inline void quick_sort(K* keys, V* values, unsigned int length) {
#if !defined(NDEBUG)
if (length == 0) {
fprintf(stderr, "quick_sort WARNING: Length is zero.\n");
return;
}
#endif
quick_sort(keys, values, 0, length - 1);
}
/**
* Performs Quicksort on the given native arrays `keys` and `values` with given
* `length` and `sorter`. This function assumes `length > 0`. If the preprocessor
* `NDEBUG` is not defined, this function outputs a warning when `length == 0`.
* \tparam K a generic type that satisfies is_swappable, and for which a
* function `bool less_than(const K&, const K&, const Sorter&)` is
* defined.
* \tparam V satisfies is_swappable.
*/
template<typename K, typename V, typename Sorter,
typename std::enable_if<!std::is_integral<Sorter>::value>::type* = nullptr>
inline void quick_sort(K* keys, V* values, unsigned int length, const Sorter& sorter)
{
#if !defined(NDEBUG)
if (length == 0) {
fprintf(stderr, "quick_sort WARNING: Length is zero.\n");
return;
}
#endif
quick_sort(keys, values, 0, length - 1, sorter);
}
/**
* Performs Quicksort on the given array `keys`. This function assumes
* `length > 0`. If the preprocessor `NDEBUG` is not defined, this function
* outputs a warning when `length == 0`.
* \tparam T is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible),
* [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable),
* and is_swappable.
*/