-
Notifications
You must be signed in to change notification settings - Fork 1
/
permute.cpp
executable file
·160 lines (117 loc) · 3.3 KB
/
permute.cpp
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
/***
permute.cpp
Functions for dealing with permutations, sub-sets, and
n choose k.
Copyright (c) 2014-2022 C. M. Street
***/
#include "libcodehappy.h"
static i32 __call_all_permutations_helper(u32 flags, u32 chosen, int array[], int parray[], u32 n, SubsetCallback callback, void* args) {
u32 ret;
u32 e;
if (chosen == n) {
ret = callback(parray, n, args);
return(ret);
}
for (e = 0; e < n; ++e) {
if ((flags & (1 << e)) == 0) {
parray[chosen] = array[e];
ret = __call_all_permutations_helper(flags | (1 << e), chosen + 1, array, parray, n, callback, args);
if (ret != 0L)
return(ret);
}
}
return(0L);
}
/***
Call the specified callback function for each permutation
of the integers in the array.
The pointer "args" is passed as-is to the callback function.
The return value is 0 if all possibilities were run through. If the callback
function returns a non-zero value to exit early, that becomes the return value of this function.
***/
i32 call_all_permutations(int array[], u32 n, SubsetCallback callback, void* args) {
i32 ret;
int parray[32];
// 32! is pretty darn big anyway
if (n >= 32)
return(-1);
ret = __call_all_permutations_helper(0UL, 0UL, array, parray, n, callback, args);
return(ret);
}
// TODO: call all _unique_ permutations or n choose ks?
static u32 __make_subset_helper(int array[], int parray[], u32 flags) {
int e;
u32 m;
u32 c;
m = 1UL;
c = 0UL;
for (e = 0; e < 32; ++e) {
if ((flags & m) != 0) {
parray[c] = array[e];
++c;
}
m <<= 1;
}
return(c);
}
/***
Call the specified callback function for each subset
of the integers in the array
The pointer "args" is passed as-is to the callback function.
The return value is 0 if all possibilities were run through. If the callback
function returns a non-zero value to exit early, that becomes the return value of this function.
***/
i32 call_all_subsets(int array[], u32 n, SubsetCallback callback, void* args) {
u32 mask;
u32 mask_max;
int parray[32];
u32 nn;
i32 ret;
// 2^32 is pretty big, too
if (n > 31)
return(-1);
mask_max = (1 << n);
mask = 0UL;
while (mask < mask_max) {
nn = __make_subset_helper(array, parray, mask);
ret = callback(parray, nn, args);
if (0 != ret)
return(ret);
++mask;
}
return(0L);
}
/***
Call the specified callback function for each k-element subset
of the n integers in the array.
The pointer "args" is passed as-is to the callback function.
The return value is 0 if all possibilities were run through. If the callback
function returns a non-zero value to exit early, that becomes the return value of this function.
***/
i32 call_all_nchoosek(int array[], u32 n, u32 k, SubsetCallback callback, void* args) {
u32 mask;
u32 mask_max;
int parray[32];
i32 ret;
if (n > 31 || k > 31)
return(-1);
if (k > n)
return(-1);
mask = (1 << k) - 1;
mask_max = 1 << n;
forever {
u32 maskn;
__make_subset_helper(array, parray, mask);
ret = callback(parray, k, args);
if (0L != ret)
return(ret);
maskn = next_same_number_1_bits(mask);
if (maskn <= mask)
break;
if (maskn >= mask_max)
break;
mask = maskn;
}
return(0L);
}
/*** end permute.cpp ***/