-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermute.py
223 lines (190 loc) · 5.7 KB
/
permute.py
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
from dataclasses import dataclass
import evenodd
def __builtin_ctz(v: int) -> int:
"""Return the number of number of trailing zeros."""
return (v & -v).bit_length() - 1
@dataclass
class Opaque:
"""An uninspectable filler object."""
value: int
def even_odd(x: list[Opaque]) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
return x[::2] + x[1::2]
def naive_even_odd(x: list[Opaque]) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
n = len(x)
assert n == 1 << __builtin_ctz(n), f"{n} is not a power of 2."
m = n - 1
p = list(x)
for i in range(n >> 1):
p[i] = x[i << 1]
for i in range(n >> 1, n):
p[i] = x[((i << 1) | 1) & m]
return p
def naive_bit_even_odd(x: list[Opaque]) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
n = len(x)
assert n == 1 << __builtin_ctz(n), f"{n} is not a power of 2."
m = n - 1
p = list(x)
j = 0
for i in range(n >> 1):
p[i] = x[j]
j += 2
j = min(1, m)
for i in range(n >> 1, n):
p[i] = x[j]
j += 2
return p
def insertion_even_odd(x: list[Opaque]) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
n = len(x)
assert n == 1 << __builtin_ctz(n), f"{n} is not a power of 2."
for i in range(1, n >> 1):
for j in range(i << 1, i, -1):
x[j], x[j - 1] = x[j - 1], x[j]
return x
def quicksort_even_odd(
x: list[Opaque], n: int | None = None, start: int = 0
) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
if n is None:
n = len(x)
assert n == 1 << __builtin_ctz(n), f"{n} is not a power of 2."
# base case
if n <= 2:
return x
# pivot
half = n >> 1
for i in range(start + 1, start + 1 + half, 2):
j = i + half - 1
x[i], x[j] = x[j], x[i]
# recur on both halves
quicksort_even_odd(x, half, start)
quicksort_even_odd(x, half, start + half)
return x
def unravel_mutual(x: list[Opaque], n: int, start: int) -> list[Opaque]:
"""Unravel the result of selection sort."""
assert n == 1 << __builtin_ctz(n), f"{n} is not a power of 2."
# base case
if n <= 2:
return x
unravel_even_odd(x, n, start)
unravel_mutual(x, n >> 1, start)
return x
def unravel_recur(x: list[Opaque], n: int, start: int) -> list[Opaque]:
"""Unravel the result of selection sort."""
assert n == 1 << __builtin_ctz(n), f"{n} is not a power of 2."
# base case
if n <= 2:
return x
# pivot
half = n >> 1
for i in range(start + 1, start + half):
j = (i << 1) - start
x[i], x[j] = x[j], x[i]
# recur on both halves
unravel_recur(x, half, start)
unravel_recur(x, half, start + half)
return x
def unravel_even_odd(
x: list[Opaque], n: int | None = None, start: int = 0
) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
if n is None:
n = len(x)
assert n == 1 << __builtin_ctz(n), f"{n} is not a power of 2."
# base case
if n <= 2:
return x
# pivot
half = n >> 1
for i in range(start + 1, start + half):
k = (i << 1) - start
x[i], x[k] = x[k], x[i]
# recur
return unravel_mutual(x, half, start + half)
def cycle_even_odd(x: list[Opaque]) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
n = len(x)
k = __builtin_ctz(n)
assert n == 1 << k, f"{n} is not a power of 2."
m = n - 1
d = k - 1
for i in range(1, n >> 1, 2):
j = ((i << 1) | (i >> d)) & m
while j > i:
j = ((j << 1) | (j >> d)) & m
if j == i:
# i is minimal
j = ((j << 1) | (j >> d)) & m
while j > i:
t = ((j << 1) | (j >> d)) & m
x[j], x[t] = x[t], x[j]
j = t
return x
def fast_even_odd(x: list[Opaque]) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
n = len(x)
k = __builtin_ctz(n)
assert n == 1 << k, f"{n} is not a power of 2."
m = n - 1
d = k - 1
i = 1
while i < m:
j = ((i << 1) | (i >> d)) & m
while j > i:
t = ((j << 1) | (j >> d)) & m
x[j], x[t] = x[t], x[j]
j = t
i = evenodd.skip(i + 1, k)
return x
def tile(i: int, j: int, k: int) -> int:
"""Tile the top j bits of i into k bits."""
top = i >> (k - j)
z = (top << k) - top
y = z // ((1 << j) - 1)
return y + (y << j != y + z)
def inline_even_odd(x: list[Opaque]) -> list[Opaque]:
"""Apply the even-odd permutation to x."""
n = len(x)
k = __builtin_ctz(n)
assert n == 1 << k, f"{n} is not a power of 2."
m = n - 1
d = k - 1
i = 1
while i < m:
j = ((i << 1) | (i >> d)) & m
while j > i:
t = ((j << 1) | (j >> d)) & m
x[j], x[t] = x[t], x[j]
j = t
i += 1
lsb = k - __builtin_ctz(i)
t = tile(i, lsb, k) if i >= (1 << lsb) else i | 1
while t & 1 == 0:
lsb = k - __builtin_ctz(t)
t = tile(t, lsb, k) if t >= (1 << lsb) else t | 1
i = t
return x
if __name__ == "__main__":
K = 20
methods = [
naive_even_odd,
naive_bit_even_odd,
# insertion_even_odd, # much slower than the rest
quicksort_even_odd,
unravel_even_odd,
cycle_even_odd,
fast_even_odd,
inline_even_odd,
]
for k in range(K):
n = 1 << k
x = [Opaque(i) for i in range(n)]
ans = even_odd(list(x))
for method in methods:
assert method(list(x)) == ans
# for n in range(1 << K):
# x = [Opaque(i) for i in range(n)]
# print(even_odd(x) == naive_even_odd(x))