-
Notifications
You must be signed in to change notification settings - Fork 5
/
combinations.h
969 lines (899 loc) · 29.3 KB
/
combinations.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
#ifndef COMBINATIONS_H
#define COMBINATIONS_H
// (C) Copyright Howard Hinnant 2005-2011.
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt).
//
// See http://www.boost.org/libs/type_traits for most recent version including documentation.
// Details are in namespace detail. Every effort has been made to make
// combine_discontinuous and permute as fast as possible. They minimize the number
// of swaps that are performed. Everything else builds on these two primitives.
// The most complicated algorithm is for_each_reversible_permutation. But it
// builds on combine_discontinuous and permute and I believe represents a minimum
// number of swaps. Without care, algorithms such as for_each_reversible_permutation
// will take longer than for_each_permutation instead of the intended half the time.
// Speed is everything. Lest you could just use std::next_permutation and manually
// eliminate duplicate permutations. If the implementation fails in being orders
// of magnitude faster than that, then it has failed miserably.
#include <iterator>
#include <algorithm>
#include <cstdint>
#include <limits>
#include <stdexcept>
namespace detail
{
// Rotates two discontinuous ranges to put *first2 where *first1 is.
// If last1 == first2 this would be equivalent to rotate(first1, first2, last2),
// but instead the rotate "jumps" over the discontinuity [last1, first2) -
// which need not be a valid range.
// In order to make it faster, the length of [first1, last1) is passed in as d1,
// and d2 must be the length of [first2, last2).
// In a perfect world the d1 > d2 case would have used swap_ranges and
// reverse_iterator, but reverse_iterator is too inefficient.
template <class BidirIter>
void
rotate_discontinuous(BidirIter first1, BidirIter last1,
typename std::iterator_traits<BidirIter>::difference_type d1,
BidirIter first2, BidirIter last2,
typename std::iterator_traits<BidirIter>::difference_type d2)
{
using std::swap;
if (d1 <= d2)
std::rotate(first2, std::swap_ranges(first1, last1, first2), last2);
else
{
BidirIter i1 = last1;
while (first2 != last2)
swap(*--i1, *--last2);
std::rotate(first1, i1, last1);
}
}
// Rotates the three discontinuous ranges to put *first2 where *first1 is.
// Just like rotate_discontinuous, except the second range is now represented by
// two discontinuous ranges: [first2, last2) + [first3, last3).
template <class BidirIter>
void
rotate_discontinuous3(BidirIter first1, BidirIter last1,
typename std::iterator_traits<BidirIter>::difference_type d1,
BidirIter first2, BidirIter last2,
typename std::iterator_traits<BidirIter>::difference_type d2,
BidirIter first3, BidirIter last3,
typename std::iterator_traits<BidirIter>::difference_type d3)
{
rotate_discontinuous(first1, last1, d1, first2, last2, d2);
if (d1 <= d2)
rotate_discontinuous(std::next(first2, d2 - d1), last2, d1, first3, last3, d3);
else
{
rotate_discontinuous(std::next(first1, d2), last1, d1 - d2, first3, last3, d3);
rotate_discontinuous(first2, last2, d2, first3, last3, d3);
}
}
// Call f() for each combination of the elements [first1, last1) + [first2, last2)
// swapped/rotated into the range [first1, last1). As long as f() returns
// false, continue for every combination and then return [first1, last1) and
// [first2, last2) to their original state. If f() returns true, return
// immediately.
// Does the absolute mininum amount of swapping to accomplish its task.
// If f() always returns false it will be called (d1+d2)!/(d1!*d2!) times.
template <class BidirIter, class Function>
bool
combine_discontinuous(BidirIter first1, BidirIter last1,
typename std::iterator_traits<BidirIter>::difference_type d1,
BidirIter first2, BidirIter last2,
typename std::iterator_traits<BidirIter>::difference_type d2,
Function& f,
typename std::iterator_traits<BidirIter>::difference_type d = 0)
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
using std::swap;
if (d1 == 0 || d2 == 0)
return f();
if (d1 == 1)
{
for (BidirIter i2 = first2; i2 != last2; ++i2)
{
if (f())
return true;
swap(*first1, *i2);
}
}
else
{
BidirIter f1p = std::next(first1);
BidirIter i2 = first2;
for (D d22 = d2; i2 != last2; ++i2, --d22)
{
if (combine_discontinuous(f1p, last1, d1-1, i2, last2, d22, f, d+1))
return true;
swap(*first1, *i2);
}
}
if (f())
return true;
if (d != 0)
rotate_discontinuous(first1, last1, d1, std::next(first2), last2, d2-1);
else
rotate_discontinuous(first1, last1, d1, first2, last2, d2);
return false;
}
// A binder for binding arguments to call combine_discontinuous
template <class Function, class BidirIter>
class call_combine_discontinuous
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
Function f_;
BidirIter first1_;
BidirIter last1_;
D d1_;
BidirIter first2_;
BidirIter last2_;
D d2_;
public:
call_combine_discontinuous(
BidirIter first1, BidirIter last1,
D d1,
BidirIter first2, BidirIter last2,
D d2,
Function& f)
: f_(f), first1_(first1), last1_(last1), d1_(d1),
first2_(first2), last2_(last2), d2_(d2) {}
bool operator()()
{
return combine_discontinuous(first1_, last1_, d1_, first2_, last2_, d2_, f_);
}
};
// See combine_discontinuous3
template <class BidirIter, class Function>
bool
combine_discontinuous3_(BidirIter first1, BidirIter last1,
typename std::iterator_traits<BidirIter>::difference_type d1,
BidirIter first2, BidirIter last2,
typename std::iterator_traits<BidirIter>::difference_type d2,
BidirIter first3, BidirIter last3,
typename std::iterator_traits<BidirIter>::difference_type d3,
Function& f,
typename std::iterator_traits<BidirIter>::difference_type d = 0)
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
using std::swap;
if (d1 == 1)
{
for (BidirIter i2 = first2; i2 != last2; ++i2)
{
if (f())
return true;
swap(*first1, *i2);
}
if (f())
return true;
swap(*first1, *std::prev(last2));
swap(*first1, *first3);
for (BidirIter i2 = std::next(first3); i2 != last3; ++i2)
{
if (f())
return true;
swap(*first1, *i2);
}
}
else
{
BidirIter f1p = std::next(first1);
BidirIter i2 = first2;
for (D d22 = d2; i2 != last2; ++i2, --d22)
{
if (combine_discontinuous3_(f1p, last1, d1-1, i2, last2, d22, first3,
last3, d3, f, d+1))
return true;
swap(*first1, *i2);
}
i2 = first3;
for (D d22 = d3; i2 != last3; ++i2, --d22)
{
if (combine_discontinuous(f1p, last1, d1-1, i2, last3, d22, f, d+1))
return true;
swap(*first1, *i2);
}
}
if (f())
return true;
if (d1 == 1)
swap(*std::prev(last2), *first3);
if (d != 0)
{
if (d2 > 1)
rotate_discontinuous3(first1, last1, d1, std::next(first2), last2, d2-1, first3, last3, d3);
else
rotate_discontinuous(first1, last1, d1, first3, last3, d3);
}
else
rotate_discontinuous3(first1, last1, d1, first2, last2, d2, first3, last3, d3);
return false;
}
// Like combine_discontinuous, but swaps/rotates each combination out of
// [first1, last1) + [first2, last2) + [first3, last3) into [first1, last1).
// If f() always returns false, it is called (d1+d2+d3)!/(d1!*(d2+d3)!) times.
template <class BidirIter, class Function>
bool
combine_discontinuous3(BidirIter first1, BidirIter last1,
typename std::iterator_traits<BidirIter>::difference_type d1,
BidirIter first2, BidirIter last2,
typename std::iterator_traits<BidirIter>::difference_type d2,
BidirIter first3, BidirIter last3,
typename std::iterator_traits<BidirIter>::difference_type d3,
Function& f)
{
typedef call_combine_discontinuous<Function&, BidirIter> F;
F fbc(first2, last2, d2, first3, last3, d3, f); // BC
return combine_discontinuous3_(first1, last1, d1, first2, last2, d2, first3, last3, d3, fbc);
}
// See permute
template <class BidirIter, class Function>
bool
permute_(BidirIter first1, BidirIter last1,
typename std::iterator_traits<BidirIter>::difference_type d1,
Function& f)
{
using std::swap;
switch (d1)
{
case 0:
case 1:
return f();
case 2:
if (f())
return true;
swap(*first1, *std::next(first1));
return f();
case 3:
{
if (f())
return true;
BidirIter f2 = std::next(first1);
BidirIter f3 = std::next(f2);
swap(*f2, *f3);
if (f())
return true;
swap(*first1, *f3);
swap(*f2, *f3);
if (f())
return true;
swap(*f2, *f3);
if (f())
return true;
swap(*first1, *f2);
swap(*f2, *f3);
if (f())
return true;
swap(*f2, *f3);
return f();
}
}
BidirIter fp1 = std::next(first1);
for (BidirIter p = fp1; p != last1; ++p)
{
if (permute_(fp1, last1, d1-1, f))
return true;
std::reverse(fp1, last1);
swap(*first1, *p);
}
return permute_(fp1, last1, d1-1, f);
}
// Calls f() for each permutation of [first1, last1)
// Divided into permute and permute_ in a (perhaps futile) attempt to
// squeeze a little more performance out of it.
template <class BidirIter, class Function>
bool
permute(BidirIter first1, BidirIter last1,
typename std::iterator_traits<BidirIter>::difference_type d1,
Function& f)
{
using std::swap;
switch (d1)
{
case 0:
case 1:
return f();
case 2:
{
if (f())
return true;
BidirIter i = std::next(first1);
swap(*first1, *i);
if (f())
return true;
swap(*first1, *i);
}
break;
case 3:
{
if (f())
return true;
BidirIter f2 = std::next(first1);
BidirIter f3 = std::next(f2);
swap(*f2, *f3);
if (f())
return true;
swap(*first1, *f3);
swap(*f2, *f3);
if (f())
return true;
swap(*f2, *f3);
if (f())
return true;
swap(*first1, *f2);
swap(*f2, *f3);
if (f())
return true;
swap(*f2, *f3);
if (f())
return true;
swap(*first1, *f3);
}
break;
default:
BidirIter fp1 = std::next(first1);
for (BidirIter p = fp1; p != last1; ++p)
{
if (permute_(fp1, last1, d1-1, f))
return true;
std::reverse(fp1, last1);
swap(*first1, *p);
}
if (permute_(fp1, last1, d1-1, f))
return true;
std::reverse(first1, last1);
break;
}
return false;
}
// Creates a functor with no arguments which calls f_(first_, last_).
// Also has a variant that takes two It and ignores them.
template <class Function, class It>
class bound_range
{
Function f_;
It first_;
It last_;
public:
bound_range(Function f, It first, It last)
: f_(f), first_(first), last_(last) {}
bool
operator()()
{
return f_(first_, last_);
}
bool
operator()(It, It)
{
return f_(first_, last_);
}
};
// A binder for binding arguments to call permute
template <class Function, class It>
class call_permute
{
typedef typename std::iterator_traits<It>::difference_type D;
Function f_;
It first_;
It last_;
D d_;
public:
call_permute(Function f, It first, It last, D d)
: f_(f), first_(first), last_(last), d_(d) {}
bool
operator()()
{
return permute(first_, last_, d_, f_);
}
};
} // detail
template <class BidirIter, class Function>
Function
for_each_combination(BidirIter first, BidirIter mid,
BidirIter last, Function f)
{
detail::bound_range<Function&, BidirIter> wfunc(f, first, mid);
detail::combine_discontinuous(first, mid, std::distance(first, mid),
mid, last, std::distance(mid, last),
wfunc);
return std::move(f);
}
template <class UInt>
UInt
gcd(UInt x, UInt y)
{
while (y != 0)
{
UInt t = x % y;
x = y;
y = t;
}
return x;
}
template <class Int>
inline
typename std::enable_if
<
std::is_unsigned<Int>::value,
void
>::type
check_non_negative(Int, Int)
{
}
template <class Int>
typename std::enable_if
<
!std::is_unsigned<Int>::value,
void
>::type
check_non_negative(Int d1, Int d2)
{
if (d1 < Int(0) || d2 < Int(0))
throw std::invalid_argument("expected non-negative argument");
}
template <class UInt>
UInt
count_each_combination(UInt d1, UInt d2)
{
check_non_negative(d1, d2);
if (d2 < d1)
std::swap(d1, d2);
if (d1 == UInt())
return 1;
if (d1 > std::numeric_limits<UInt>::max() - d2)
throw std::overflow_error("overflow in count_each_combination");
UInt n = d1 + d2;
UInt r = n;
--n;
for (UInt k = UInt(2); k <= d1; ++k, --n)
{
// r = r * n / k, known to not not have truncation error
UInt g = gcd(r, k);
r /= g;
UInt t = n / (k / g);
if (r > std::numeric_limits<UInt>::max() / t)
throw std::overflow_error("overflow in count_each_combination");
r *= t;
}
return r;
}
template <class BidirIter>
std::uintmax_t
count_each_combination(BidirIter first, BidirIter mid, BidirIter last)
{
return count_each_combination<std::uintmax_t>
(std::distance(first, mid), std::distance(mid, last));
}
// For each of the permutation algorithms, use for_each_combination (or
// combine_discontinuous) to handle the "r out of N" part of the algorithm.
// Thus each permutation algorithm has to deal only with an "N out of N"
// problem. I.e. For each combination of r out of N items, permute it thusly.
template <class BidirIter, class Function>
Function
for_each_permutation(BidirIter first, BidirIter mid,
BidirIter last, Function f)
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
typedef detail::bound_range<Function&, BidirIter> Wf;
typedef detail::call_permute<Wf, BidirIter> PF;
Wf wfunc(f, first, mid);
D d1 = std::distance(first, mid);
PF pf(wfunc, first, mid, d1);
detail::combine_discontinuous(first, mid, d1,
mid, last, std::distance(mid, last),
pf);
return std::move(f);
}
template <class UInt>
UInt
count_each_permutation(UInt d1, UInt d2)
{
// return (d1+d2)!/d2!
check_non_negative(d1, d2);
if (d1 > std::numeric_limits<UInt>::max() - d2)
throw std::overflow_error("overflow in count_each_permutation");
UInt n = d1 + d2;
UInt r(1);
for (; n > d2; --n)
{
if (r > std::numeric_limits<UInt>::max() / n)
throw std::overflow_error("overflow in count_each_permutation");
r *= n;
}
return r;
}
template <class BidirIter>
std::uintmax_t
count_each_permutation(BidirIter first, BidirIter mid, BidirIter last)
{
return count_each_permutation(static_cast<std::uintmax_t>(std::distance(first, mid)),
static_cast<std::uintmax_t>(std::distance(mid, last)));
}
namespace detail
{
// Adapt functor to permute over [first+1, last)
// A circular permutation of N items is done by holding the first item and
// permuting [first+1, last).
template <class Function, class BidirIter>
class circular_permutation
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
Function f_;
D s_;
public:
explicit circular_permutation(Function f, D s) : f_(f), s_(s) {}
bool
operator()(BidirIter first, BidirIter last)
{
if (s_ <= 1)
return f_(first, last);
bound_range<Function, BidirIter> f(f_, first, last);
return permute(std::next(first), last, s_ - 1, f);
}
};
} // detail
template <class BidirIter, class Function>
Function
for_each_circular_permutation(BidirIter first,
BidirIter mid,
BidirIter last, Function f)
{
for_each_combination(first, mid, last, detail::circular_permutation<Function&,
BidirIter>(f, std::distance(first, mid)));
return std::move(f);
}
template <class UInt>
UInt
count_each_circular_permutation(UInt d1, UInt d2)
{
// return d1 > 0 ? (d1+d2)!/(d1*d2!) : 1
check_non_negative(d1, d2);
if (d1 == UInt())
return 1;
UInt r;
if (d1 <= d2)
{
try
{
r = count_each_combination(d1, d2);
}
catch (const std::overflow_error&)
{
throw std::overflow_error("overflow in count_each_circular_permutation");
}
for (--d1; d1 > UInt(1); --d1)
{
if (r > std::numeric_limits<UInt>::max()/d1)
throw std::overflow_error("overflow in count_each_circular_permutation");
r *= d1;
}
}
else
{ // functionally equivalent but faster algorithm
if (d1 > std::numeric_limits<UInt>::max() - d2)
throw std::overflow_error("overflow in count_each_circular_permutation");
UInt n = d1 + d2;
r = 1;
for (; n > d1; --n)
{
if (r > std::numeric_limits<UInt>::max()/n)
throw std::overflow_error("overflow in count_each_circular_permutation");
r *= n;
}
for (--n; n > d2; --n)
{
if (r > std::numeric_limits<UInt>::max()/n)
throw std::overflow_error("overflow in count_each_circular_permutation");
r *= n;
}
}
return r;
}
template <class BidirIter>
std::uintmax_t
count_each_circular_permutation(BidirIter first, BidirIter mid, BidirIter last)
{
return count_each_circular_permutation<std::uintmax_t>
(std::distance(first, mid), std::distance(mid, last));
}
namespace detail
{
// Difficult!!! See notes for operator().
template <class Function, class Size>
class reversible_permutation
{
Function f_;
Size s_;
public:
reversible_permutation(Function f, Size s) : f_(f), s_(s) {}
template <class BidirIter>
bool
operator()(BidirIter first, BidirIter last);
};
// rev1 looks like call_permute
template <class Function, class BidirIter>
class rev1
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
Function f_;
BidirIter first1_;
BidirIter last1_;
D d1_;
public:
rev1(Function f, BidirIter first, BidirIter last, D d)
: f_(f), first1_(first), last1_(last), d1_(d) {}
bool operator()()
{
return permute(first1_, last1_, d1_, f_);
}
};
// For each permutation in [first1, last1),
// call f() for each permutation of [first2, last2).
template <class Function, class BidirIter>
class rev2
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
Function f_;
BidirIter first1_;
BidirIter last1_;
D d1_;
BidirIter first2_;
BidirIter last2_;
D d2_;
public:
rev2(Function f, BidirIter first1, BidirIter last1, D d1,
BidirIter first2, BidirIter last2, D d2)
: f_(f), first1_(first1), last1_(last1), d1_(d1),
first2_(first2), last2_(last2), d2_(d2) {}
bool operator()()
{
call_permute<Function, BidirIter> f(f_, first2_, last2_, d2_);
return permute(first1_, last1_, d1_, f);
}
};
// For each permutation in [first1, last1),
// and for each permutation of [first2, last2)
// call f() for each permutation of [first3, last3).
template <class Function, class BidirIter>
class rev3
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
Function f_;
BidirIter first1_;
BidirIter last1_;
D d1_;
BidirIter first2_;
BidirIter last2_;
D d2_;
BidirIter first3_;
BidirIter last3_;
D d3_;
public:
rev3(Function f, BidirIter first1, BidirIter last1, D d1,
BidirIter first2, BidirIter last2, D d2,
BidirIter first3, BidirIter last3, D d3)
: f_(f), first1_(first1), last1_(last1), d1_(d1),
first2_(first2), last2_(last2), d2_(d2),
first3_(first3), last3_(last3), d3_(d3) {}
bool operator()()
{
rev2<Function, BidirIter> f(f_, first2_, last2_, d2_, first3_, last3_, d3_);
return permute(first1_, last1_, d1_, f);
}
};
// There are simpler implementations. I believe the simpler ones are far more
// expensive.
template <class Function, class Size>
template <class BidirIter>
bool
reversible_permutation<Function, Size>::operator()(BidirIter first,
BidirIter last)
{
typedef rev2<bound_range<Function&, BidirIter>, BidirIter> F2;
typedef rev3<bound_range<Function&, BidirIter>, BidirIter> F3;
// When the range is 0 - 2, then this is just a combination of N out of N
// elements.
if (s_ < 3)
return f_(first, last);
using std::swap;
// Hold the first element steady and call f_(first, last) for each
// permutation in [first+1, last).
BidirIter a = std::next(first);
bound_range<Function&, BidirIter> f(f_, first, last);
if (permute(a, last, s_-1, f))
return true;
// Beginning with the first element, swap the previous element with the
// next element. For each swap, call f_(first, last) for each
// permutation of the discontinuous range:
// [prior to the orignal element] + [after the original element].
Size s2 = s_ / 2;
BidirIter am1 = first;
BidirIter ap1 = std::next(a);
for (Size i = 1; i < s2; ++i, ++am1, ++a, ++ap1)
{
swap(*am1, *a);
F2 f2(f, first, a, i, ap1, last, s_ - i - 1);
if (combine_discontinuous(first, a, i, ap1, last, s_ - i - 1, f2))
return true;
}
// If [first, last) has an even number of elements, then fix it up to the
// original permutation.
if (2 * s2 == s_)
{
std::rotate(first, am1, a);
}
// else if the range has length 3, we need one more call and the fix is easy.
else if (s_ == 3)
{
swap(*am1, *a);
if (f_(first, last))
return true;
swap(*am1, *a);
}
// else the range is an odd number greater than 3. We need to permute
// through exactly half of the permuations with the original element in
// the middle.
else
{
// swap the original first element into the middle, and hold the current
// first element steady. This creates a discontinuous range:
// [first+1, middle) + [middle+1, last). Run through all permutations
// of that discontinuous range.
swap(*am1, *a);
BidirIter b = first;
BidirIter bp1 = std::next(b);
F2 f2(f, bp1, a, s2-1, ap1, last, s_ - s2 - 1);
if (combine_discontinuous(bp1, a, s2-1, ap1, last, s_ - s2 - 1, f2))
return true;
// Swap the current first element into every place from first+1 to middle-1.
// For each location, hold it steady to create the following discontinuous
// range (made of 3 ranges): [first, b-1) + [b+1, middle) + [middle+1, last).
// For each b in [first+1, middle-1), run through all permutations of
// the discontinuous ranges.
b = bp1;
++bp1;
BidirIter bm1 = first;
for (Size i = 1; i < s2-1; ++i, ++bm1, ++b, ++bp1)
{
swap(*bm1, *b);
F3 f3(f, first, b, i, bp1, a, s2-i-1, ap1, last, s_ - s2 - 1);
if (combine_discontinuous3(first, b, i, bp1, a, s2-i-1, ap1, last, s_-s2-1, f3))
return true;
}
// swap b into into middle-1, creates a discontinuous range:
// [first, middle-1) + [middle+1, last). Run through all permutations
// of that discontinuous range.
swap(*bm1, *b);
F2 f21(f, first, b, s2-1, ap1, last, s_ - s2 - 1);
if (combine_discontinuous(first, b, s2-1, ap1, last, s_ - s2 - 1, f21))
return true;
// Revert [first, last) to original order
std::reverse(first, b);
std::reverse(first, ap1);
}
return false;
}
} // detail
template <class BidirIter, class Function>
Function
for_each_reversible_permutation(BidirIter first,
BidirIter mid,
BidirIter last, Function f)
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
for_each_combination(first, mid, last,
detail::reversible_permutation<Function&, D>(f,
std::distance(first, mid)));
return std::move(f);
}
template <class UInt>
UInt
count_each_reversible_permutation(UInt d1, UInt d2)
{
// return d1 > 1 ? (d1+d2)!/(2*d2!) : (d1+d2)!/d2!
check_non_negative(d1, d2);
if (d1 > std::numeric_limits<UInt>::max() - d2)
throw std::overflow_error("overflow in count_each_reversible_permutation");
UInt n = d1 + d2;
UInt r(1);
if (d1 > UInt(1))
{
r = n;
if ((n & UInt(1)) == UInt(0))
r /= UInt(2);
--n;
UInt t = n;
if ((t & UInt(1)) == UInt(0))
t /= UInt(2);
if (r > std::numeric_limits<UInt>::max() / t)
throw std::overflow_error("overflow in count_each_reversible_permutation");
r *= t;
--n;
}
for (; n > d2; --n)
{
if (r > std::numeric_limits<UInt>::max() / n)
throw std::overflow_error("overflow in count_each_reversible_permutation");
r *= n;
}
return r;
}
template <class BidirIter>
std::uintmax_t
count_each_reversible_permutation(BidirIter first, BidirIter mid, BidirIter last)
{
return count_each_reversible_permutation<std::uintmax_t>
(std::distance(first, mid), std::distance(mid, last));
}
namespace detail
{
// Adapt functor to permute over [first+1, last)
// A reversible circular permutation of N items is done by holding the first
// item and reverse-permuting [first+1, last).
template <class Function, class BidirIter>
class reverse_circular_permutation
{
typedef typename std::iterator_traits<BidirIter>::difference_type D;
Function f_;
D s_;
public:
explicit reverse_circular_permutation(Function f, D s) : f_(f), s_(s) {}
bool
operator()(BidirIter first, BidirIter last)
{
if (s_ == 1)
return f_(first, last);
typedef bound_range<Function, BidirIter> BoundFunc;
BoundFunc f(f_, first, last);
BidirIter n = std::next(first);
return reversible_permutation<BoundFunc, D>(f, std::distance(n, last))(n, last);
}
};
} // detail
template <class BidirIter, class Function>
Function
for_each_reversible_circular_permutation(BidirIter first,
BidirIter mid,
BidirIter last, Function f)
{
for_each_combination(first, mid, last, detail::reverse_circular_permutation<Function&,
BidirIter>(f, std::distance(first, mid)));
return std::move(f);
}
template <class UInt>
UInt
count_each_reversible_circular_permutation(UInt d1, UInt d2)
{
// return d1 == 0 ? 1 : d1 <= 2 ? (d1+d2)!/(d1*d2!) : (d1+d2)!/(2*d1*d2!)
check_non_negative(d1, d2);
UInt r;
try
{
r = count_each_combination(d1, d2);
}
catch (const std::overflow_error&)
{
throw std::overflow_error("overflow in count_each_reversible_circular_permutation");
}
if (d1 > UInt(3))
{
for (--d1; d1 > UInt(2); --d1)
{
if (r > std::numeric_limits<UInt>::max()/d1)
throw std::overflow_error("overflow in count_each_reversible_circular_permutation");
r *= d1;
}
}
return r;
}
template <class BidirIter>
std::uintmax_t
count_each_reversible_circular_permutation(BidirIter first, BidirIter mid,
BidirIter last)
{
return count_each_reversible_circular_permutation<std::uintmax_t>
(std::distance(first, mid), std::distance(mid, last));
}
#endif // COMBINATIONS_H