-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNumber Generator
More file actions
231 lines (213 loc) · 7.73 KB
/
Number Generator
File metadata and controls
231 lines (213 loc) · 7.73 KB
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
#cython: language_level=3
from cython cimport view
try:
import numbers
except ImportError:
def isint(value):
return isinstance(value, int) or isinstance(value, long)
else:
def isint(value):
return isinstance(value, numbers.Integral)
cdef extern from "types.h":
ctypedef unsigned int uint32_t
cdef class BitColumnMatrix(object):
cdef uint32_t columns[32]
cdef size_t columns_len
@staticmethod
def unity(uint32_t n):
cdef uint32_t value
cdef BitColumnMatrix result = BitColumnMatrix(int(n))
if n > 32:
raise NotImplementedError
result.columns_len = n
value = 1
for i in range(n):
result.columns[i] = value
value <<= 1
return result
@staticmethod
def mask(n, start, end):
"""Make a BitColumnMatrix that represents a mask.
BitColumnMatrix size of n*n, to manipulate n binary bits.
If start <= end, then bits in half-open range [start, end) are set.
If end < start, then bits in ranges [0, end) and [start, n) are set,
i.e. bits in range [end, start) are clear; others are set.
"""
cdef uint32_t value
cdef BitColumnMatrix result = BitColumnMatrix(int(n))
if n > 32:
raise NotImplementedError
result.columns_len = n
value = 1
for i in range(n):
if start <= end:
result.columns[i] = value if (start <= i < end) else 0
else:
result.columns[i] = value if (start <= i or i < end) else 0
value <<= 1
return result
@staticmethod
def shift(uint32_t n, int shift_value):
cdef uint32_t value
cdef BitColumnMatrix result = BitColumnMatrix(int(n))
if n > 32:
raise NotImplementedError
result.columns_len = n
if shift_value >= 0:
value = 1 << shift_value
else:
value = 0
for i in range(n):
result.columns[i] = value
if shift_value < 0:
shift_value += 1
if shift_value == 0:
value = 1
else:
if n == 0 or value >= (1u << (n-1)):
value = 0
else:
value <<= 1
return result
def __init__(self, columns=None, do_copy=True):
if isinstance(columns, BitColumnMatrix):
self.columns_len = (<BitColumnMatrix>columns).columns_len
for i in range(32):
self.columns[i] = (<BitColumnMatrix>columns).columns[i]
elif isint(columns):
if columns > 32:
raise NotImplementedError
self.columns_len = columns
#self.columns = view.array(shape=(columns,), itemsize=sizeof(unsigned int), format="I")
for i in range(32):
self.columns[i] = 0
else:
self.columns_len = len(columns)
if self.columns_len > 32:
raise NotImplementedError
#self.columns = view.array(shape=(self.columns_len,), itemsize=sizeof(unsigned int), format="I")
for i, column in enumerate(columns):
self.columns[i] = int(column)
def __len__(self):
#return self.columns.shape[0]
return self.columns_len
# def __iter__(self):
# return iter(self.columns)
def __richcmp__(BitColumnMatrix x, BitColumnMatrix y, int op):
cdef BitColumnMatrix result
if not isinstance(x, BitColumnMatrix) or not isinstance(y, BitColumnMatrix):
raise NotImplementedError
if op == 2: # ==
if len(x) != len(y):
return False
for i in range(len(x)):
if x.columns[i] != y.columns[i]:
return False
return True
elif op == 3: # !=
if len(x) != len(y):
return True
for i in range(len(x)):
if x.columns[i] != y.columns[i]:
return True
return False
else:
raise NotImplementedError
def __add__(BitColumnMatrix x, BitColumnMatrix y):
cdef BitColumnMatrix result
if not isinstance(x, BitColumnMatrix) or not isinstance(y, BitColumnMatrix):
return NotImplemented
if len(x) != len(y):
raise IndexError("Matrices are not of same width")
result = BitColumnMatrix(len(x))
for i in range(len(x)):
result.columns[i] = x.columns[i] ^ y.columns[i]
return result
def __sub__(BitColumnMatrix x, BitColumnMatrix y):
cdef BitColumnMatrix result
if not isinstance(x, BitColumnMatrix) or not isinstance(y, BitColumnMatrix):
return NotImplemented
if len(x) != len(y):
raise IndexError("Matrices are not of same width")
result = BitColumnMatrix(len(x))
for i in range(len(x)):
result.columns[i] = (<BitColumnMatrix>x).columns[i] ^ (<BitColumnMatrix>y).columns[i]
return result
def __mul__(x, y):
cdef BitColumnMatrix result
cdef uint32_t yy
cdef uint32_t value
cdef size_t x_len
cdef size_t y_len
if not isinstance(x, BitColumnMatrix):
return NotImplemented
x_len = len(x)
if isint(y):
# Single value
yy = int(y)
value = 0
for i in range(x_len):
if (yy & 1):
value ^= (<BitColumnMatrix>x).columns[i]
yy >>= 1
return value
elif not isinstance(y, BitColumnMatrix):
return NotImplemented
else:
# Matrix multiplication
y_len = len(y)
if x_len != y_len:
raise IndexError("Matrices are not of same width")
result = BitColumnMatrix(y_len)
for j in range(y_len):
yy = (<BitColumnMatrix>y).columns[j]
value = 0
for i in range(x_len):
if (yy & 1):
value ^= (<BitColumnMatrix>x).columns[i]
yy >>= 1
result.columns[j] = value
return result
def __imul__(self, BitColumnMatrix other):
cdef BitColumnMatrix result
cdef uint32_t yy
cdef uint32_t value
cdef size_t self_len
cdef size_t other_len
if not isinstance(other, BitColumnMatrix):
# Single int value, or some other object. Not suitable for __imul__.
return NotImplemented
else:
# Matrix multiplication
self_len = len(self)
other_len = len(other)
if self_len != other_len:
raise IndexError("Matrices are not of same width")
result = BitColumnMatrix(other_len)
for j in range(other_len):
yy = (<BitColumnMatrix>other).columns[j]
value = 0
for i in range(self_len):
if (yy & 1):
value ^= self.columns[i]
yy >>= 1
result.columns[j] = value
for i in range(32):
self.columns[i] = result.columns[i]
return self
def __pow__(x, y, modulo):
cdef BitColumnMatrix result
cdef BitColumnMatrix x_exp
if modulo is not None:
return NotImplemented
if not isinstance(x, BitColumnMatrix):
return NotImplemented
n = int(y)
result = BitColumnMatrix.unity(len(x))
x_exp = BitColumnMatrix(x)
while n != 0:
if n & 1:
result = x_exp * result
x_exp *= x_exp
n >>= 1
return result