-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.h
757 lines (665 loc) · 17.8 KB
/
algorithm.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
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* algorithm.h
*
* This file defines STL-compatible algorithms
*/
#ifndef THOR_ALGORITHM_H
#define THOR_ALGORITHM_H
#pragma once
#include <stdlib.h>
#ifndef THOR_SORT_H
#include "sort.h"
#endif
#ifndef THOR_SWAP_H
#include "swap.h"
#endif
#ifndef THOR_HEAP_H
#include "heap.h"
#endif
#ifndef THOR_PAIR_H
#include "pair.h"
#endif
namespace thor
{
template <class InputIterator1, class InputIterator2>
bool equal(InputIterator1 f1, InputIterator1 l1, InputIterator2 f2)
{
for (; f1 != l1; ++f1, ++f2)
{
if (!(*f1 == *f2))
{
return false;
}
}
return true;
}
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal(InputIterator1 f1, InputIterator1 l1, InputIterator2 f2, BinaryPredicate pred)
{
for (; f1 != l1; ++f1, ++f2)
{
if (!pred(*f1,*f2))
{
return false;
}
}
return true;
}
template <class InputIterator1, class InputIterator2>
bool lexicographical_compare(InputIterator1 f1, InputIterator1 l1,
InputIterator2 f2, InputIterator2 l2)
{
for (; f1 != l1 && f2 != l2; ++f1, ++f2)
{
if (*f1 < *f2)
{
return true;
}
if (*f2 < *f1)
{
return false;
}
}
return f1 == l1 && f2 != l2;
}
template <class InputIterator1, class InputIterator2, class Compare>
bool lexicographical_compare(InputIterator1 f1, InputIterator1 l1,
InputIterator2 f2, InputIterator2 l2,
Compare comp)
{
for (; f1 != l1 && f2 != l2; ++f1, ++f2)
{
if (comp(*f1, *f2))
{
return true;
}
if (comp(*f2, *f1))
{
return false;
}
}
return f1 == l1 && f2 != l2;
}
template <class ForwardIterator> ForwardIterator unique(ForwardIterator first, ForwardIterator last)
{
if (first == last)
{
return last;
}
ForwardIterator new_end = first++;
while (first != last)
{
if (!(*new_end == *first))
{
*++new_end = *first;
}
++first;
}
return ++new_end;
}
template <class ForwardIterator, class BinaryPredicate> ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
{
if (first == last)
{
return last;
}
ForwardIterator new_end = first++;
while (first != last)
{
if (!pred(*new_end, *first))
{
*++new_end = *first;
}
++first;
}
return ++new_end;
}
template <class InputIterator, class OutputIterator> OutputIterator copy(InputIterator first, InputIterator last, OutputIterator output)
{
// TODO: Specialize for POD types
while (first != last)
{
*output++ = *first++;
}
return output;
}
template <class RandomAccessIterator, class OutputIterator> OutputIterator copy_backward(RandomAccessIterator first, RandomAccessIterator last, OutputIterator output)
{
// TODO: Specialize for POD types
for (difference_type d = last - first; d > 0; --d)
{
*--output = *--last;
}
return output;
}
template <class InputIterator, class UnaryPredicate> UnaryPredicate for_each(InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first != last)
{
pred(*first++);
}
return pred;
}
template <class InputIterator> void iter_swap(InputIterator first, InputIterator second)
{
thor::swap(*first, *second);
}
template <class ForwardIterator1, class ForwardIterator2>
inline ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2)
{
for ( ; first1 != last1; ++first1, ++first2)
{
iter_swap(first1, first2);
}
return first2;
}
template <class Distance> inline Distance __random(Distance n)
{
return rand() % n;
}
template <class RandomAccessIterator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
{
if (first == last)
{
return;
}
for (RandomAccessIterator iter = first + 1; iter != last; ++iter)
{
thor::iter_swap(iter, first + __random((iter - first) + 1));
}
}
template <class RandomAccessIterator, class RandomNumberGenerator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator prng)
{
if (first == last)
{
return;
}
for (RandomAccessIterator iter = first + 1; iter != last; ++iter)
{
thor::iter_swap(iter, first + prng((iter - first) + 1));
}
}
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1 != last1 && first2 != last2)
{
if (*first2 < *first1)
{
*result = *first2;
++first2;
}
else
{
*result = *first1;
++first1;
}
++result;
}
return copy(first2, last2, copy(first1, last1, result));
}
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp)
{
while (first1 != last1 && first2 != last2)
{
if (comp(*first2, *first1))
{
*result = *first2;
++first2;
}
else
{
*result = *first1;
++first1;
}
++result;
}
return thor::copy(first2, last2, thor::copy(first1, last1, result));
}
template <class ForwardIterator, class T> ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& t)
{
difference_type size = distance(first, last);
THOR_ASSERT(size >= 0);
difference_type half;
ForwardIterator middle;
while (size > 0)
{
half = size >> 1;
middle = first;
advance(middle, half);
if (*middle < t)
{
first = middle;
++first;
size = size - half - 1;
}
else
{
size = half;
}
}
return first;
}
template <class ForwardIterator, class T, class LessThanComparable> ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& t, LessThanComparable comp)
{
difference_type size = thor::distance(first, last);
THOR_ASSERT(size >= 0);
difference_type half;
ForwardIterator middle;
while (size > 0)
{
half = size >> 1;
middle = first;
thor::advance(middle, half);
if (comp(*middle, t))
{
first = middle;
++first;
size = size - half - 1;
}
else
{
size = half;
}
}
return first;
}
template <class ForwardIterator, class T> ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& t)
{
difference_type size = thor::distance(first, last);
THOR_ASSERT(size >= 0);
difference_type half;
while (size > 0)
{
half = size >> 1;
ForwardIterator middle = first;
thor::advance(middle, half);
if (t < *middle)
{
size = half;
}
else
{
first = middle;
++first;
size = size - half - 1;
}
}
return first;
}
template <class ForwardIterator, class T, class LessThanComparable> ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& t, LessThanComparable comp)
{
difference_type size = thor::distance(first, last);
THOR_ASSERT(size >= 0);
difference_type half;
while (size > 0)
{
half = size >> 1;
ForwardIterator middle = first;
thor::advance(middle, half);
if (comp(t, *middle))
{
size = half;
}
else
{
first = middle;
++first;
size = size - half - 1;
}
}
return first;
}
template <class ForwardIterator, class T> thor::pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& t)
{
difference_type size = thor::distance(first, last);
THOR_ASSERT(size >= 0);
difference_type half;
while (size > 0)
{
half = size >> 1;
ForwardIterator middle = first;
thor::advance(middle, half);
if (*middle < t)
{
first = middle;
++first;
size -= (half - 1);
}
else if (t < *middle)
{
size = half;
}
else
{
thor::pair<ForwardIterator, ForwardIterator> result;
result.first = thor::lower_bound(first, middle, t);
thor::advance(result.first, size);
result.second = thor::upper_bound(++middle, first, t);
return result;
}
}
return thor::pair<ForwardIterator, ForwardIterator>(first, first);
}
template <class ForwardIterator, class T, class LessThanComparable> thor::pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& t, LessThanComparable comp)
{
difference_type size = thor::distance(first, last);
THOR_ASSERT(size >= 0);
difference_type half;
while (size > 0)
{
half = size >> 1;
ForwardIterator middle = first;
thor::advance(middle, half);
if (comp(*middle, t))
{
first = middle;
++first;
size -= (half - 1);
}
else if (comp(t, *middle))
{
size = half;
}
else
{
thor::pair<ForwardIterator, ForwardIterator> result;
result.first = thor::lower_bound(first, middle, t, comp);
thor::advance(result.first, size);
result.second = thor::upper_bound(++middle, first, t, comp);
return result;
}
}
return thor::pair<ForwardIterator, ForwardIterator>(first, first);
}
template <class BidirectionalIterator> void reverse(BidirectionalIterator first, BidirectionalIterator last)
{
if (first == last)
{
return;
}
while (first != --last)
{
thor::iter_swap(first, last);
if (++first == last)
{
break;
}
}
}
template <class ForwardIterator, class OutputIterator, class T> OutputIterator remove_copy(ForwardIterator first, ForwardIterator last, OutputIterator result, const T& value)
{
for (; first != last; ++first)
{
if (!(*first == value))
{
*result = *first;
++result;
}
}
return result;
}
template <class ForwardIterator, class OutputIterator, class Predicate> OutputIterator remove_copy_if(ForwardIterator first, ForwardIterator last, OutputIterator result, Predicate pred)
{
for (; first != last; ++first)
{
if (!pred(*first))
{
*result = *first;
++result;
}
}
return result;
}
template <class ForwardIterator, class T> ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value)
{
first = thor::find(first, last, value);
if (first == last)
{
return first;
}
ForwardIterator next = first;
return thor::remove_copy(++next, last, first, value);
}
template <class ForwardIterator, class Predicate> ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred)
{
first = thor::find_if(first, last, pred);
if (first == last)
{
return first;
}
ForwardIterator next = first;
return thor::remove_copy_if(++next, last, first, pred);
}
template <class RingElement>
inline RingElement __gcd(RingElement m, RingElement n)
{
while (n != 0)
{
RingElement t = m % n;
m = n;
n = t;
}
return m;
}
template <class ForwardIterator>
ForwardIterator __rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last, const forward_iterator_tag &)
{
if (first == middle)
{
return last;
}
if (last == middle)
{
return first;
}
ForwardIterator first2 = middle;
do
{
thor::swap(*first++, *first2++);
if (first == middle)
{
middle = first2;
}
} while (first2 != last);
ForwardIterator new_middle = first;
first2 = middle;
while (first2 != last)
{
thor::swap (*first++, *first2++);
if (first == middle)
{
middle = first2;
}
else if (first2 == last)
{
first2 = middle;
}
}
return new_middle;
}
template <class BidirectionalIterator>
BidirectionalIterator __rotate(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, const bidirectional_iterator_tag &)
{
if (first == middle)
{
return last;
}
if (last == middle)
{
return first;
}
thor::reverse(first, middle);
thor::reverse(middle, last);
while (first != middle && middle != last)
{
thor::swap (*first++, *--last);
}
if (first == middle)
{
thor::reverse(middle, last);
return last;
}
else
{
thor::reverse(first, middle);
return first;
}
}
template <class RandomAccessIterator, class T> RandomAccessIterator __rotate(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, T*)
{
difference_type n = last - first;
difference_type k = middle - first;
difference_type l = n - k;
RandomAccessIterator result = first + (last - middle);
if (k == 0)
{
return last;
}
if (k == l)
{
thor::swap_ranges(first, middle, middle);
return result;
}
difference_type d = __gcd(n, k);
for (difference_type i = 0; i < d; ++i)
{
T tmp = *first;
RandomAccessIterator p = first;
if (k < l)
{
for (difference_type j = 0; j < l/d; ++j)
{
if (p > first + l)
{
*p = *(p - l);
p -= l;
}
*p = *(p + k);
p += k;
}
}
else
{
for (difference_type j = 0; j < k/d - 1; ++j)
{
if (p < last - k)
{
*p = *(p + k);
p += k;
}
*p = * (p - l);
p -= l;
}
}
*p = tmp;
++first;
}
return result;
}
template <class RandomAccessIterator> RandomAccessIterator __rotate(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, const random_access_iterator_tag&)
{
return __rotate(first, middle, last, THOR_GET_VALUE_TYPE(first, RandomAccessIterator));
}
template <class ForwardIterator> ForwardIterator rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last)
{
return __rotate(first, middle, last, THOR_GET_CATEGORY(first, ForwardIterator));
}
// Windows.h #defines min and max, so only include them if not already defined.
#ifndef max
template <class T> const T& max(const T& a, const T& b)
{
return a < b ? b : a;
}
template <class T, class BinaryPredicate> const T& max(const T& a, const T& b, BinaryPredicate pred)
{
return pred(a, b) ? b : a;
}
#endif
template <class T> const T& _max(const T& a, const T& b)
{
return a < b ? b : a;
}
template <class T, class BinaryPredicate> const T& _max(const T& a, const T& b, BinaryPredicate pred)
{
return pred(a, b) ? b : a;
}
#ifndef min
template <class T> const T& min(const T& a, const T& b)
{
return b < a ? b : a;
}
template <class T, class BinaryPredicate> const T& min(const T& a, const T& b, BinaryPredicate pred)
{
return pred(b, a) ? b : a;
}
#endif
template <class T> const T& _min(const T& a, const T& b)
{
return b < a ? b : a;
}
template <class T, class BinaryPredicate> const T& _min(const T& a, const T& b, BinaryPredicate pred)
{
return pred(b, a) ? b : a;
}
template <class ForwardIterator> inline ForwardIterator __internal_max_element(ForwardIterator first, ForwardIterator last)
{ // find largest element, using operator<
ForwardIterator found = first;
if (first != last)
{
for (; first != last; ++first)
{
if (*found < *first)
{
found = first;
}
}
}
return found;
}
template <class ForwardIterator> inline ForwardIterator max_element(ForwardIterator first, ForwardIterator last)
{
first = __internal_max_element(first, last);
return first;
}
template <class ForwardIterator> inline ForwardIterator __internal_min_element(ForwardIterator first, ForwardIterator last)
{ // find smallest element, using operator<
ForwardIterator found = first;
if (first != last)
{
for (; first != last; ++first)
{
if (first < *found)
{
found = first;
}
}
}
return found;
}
template <class ForwardIterator> inline ForwardIterator min_element( ForwardIterator first, ForwardIterator last )
{
first = __internal_min_element( first, last );
return first;
}
template <class InputIterator, class T> inline T __internal_accumulate( InputIterator first, InputIterator last, T val)
{ // return sum of val and all in [first, last)
for (; first != last; ++first)
{
val += *first;
}
return val;
}
template <class InputIterator, class T> inline T accumulate( InputIterator first, InputIterator last, T val)
{
return __internal_accumulate( first, last, val);
}
} // namespace thor
#endif