-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
/
Copy pathhashtable_func_helper.pxi.in
359 lines (298 loc) · 9.96 KB
/
hashtable_func_helper.pxi.in
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
"""
Template for each `dtype` helper function for hashtable
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""
{{py:
# dtype, ttype, c_type
dtypes = [('float64', 'float64', 'float64_t'),
('float32', 'float32', 'float32_t'),
('uint64', 'uint64', 'uint64_t'),
('uint32', 'uint32', 'uint32_t'),
('uint16', 'uint16', 'uint16_t'),
('uint8', 'uint8', 'uint8_t'),
('object', 'pymap', 'object'),
('int64', 'int64', 'int64_t'),
('int32', 'int32', 'int32_t'),
('int16', 'int16', 'int16_t'),
('int8', 'int8', 'int8_t')]
}}
{{for dtype, ttype, c_type in dtypes}}
@cython.wraparound(False)
@cython.boundscheck(False)
{{if dtype == 'object'}}
cdef build_count_table_{{dtype}}(ndarray[{{dtype}}] values,
kh_{{ttype}}_t *table, bint dropna):
{{else}}
cdef build_count_table_{{dtype}}({{dtype}}_t[:] values,
kh_{{ttype}}_t *table, bint dropna):
{{endif}}
cdef:
khiter_t k
Py_ssize_t i, n = len(values)
{{c_type}} val
int ret = 0
{{if dtype == 'object'}}
kh_resize_{{ttype}}(table, n // 10)
for i in range(n):
val = values[i]
if not checknull(val) or not dropna:
k = kh_get_{{ttype}}(table, <PyObject*>val)
if k != table.n_buckets:
table.vals[k] += 1
else:
k = kh_put_{{ttype}}(table, <PyObject*>val, &ret)
table.vals[k] = 1
{{else}}
with nogil:
kh_resize_{{ttype}}(table, n)
for i in range(n):
val = values[i]
{{if dtype == 'float64' or dtype == 'float32'}}
if val == val or not dropna:
{{else}}
if True:
{{endif}}
k = kh_get_{{ttype}}(table, val)
if k != table.n_buckets:
table.vals[k] += 1
else:
k = kh_put_{{ttype}}(table, val, &ret)
table.vals[k] = 1
{{endif}}
@cython.wraparound(False)
@cython.boundscheck(False)
{{if dtype == 'object'}}
cpdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna):
{{else}}
cpdef value_count_{{dtype}}({{c_type}}[:] values, bint dropna):
{{endif}}
cdef:
Py_ssize_t i = 0
kh_{{ttype}}_t *table
{{if dtype != 'object'}}
{{dtype}}_t[:] result_keys
int64_t[:] result_counts
{{endif}}
# Don't use Py_ssize_t, since table.n_buckets is unsigned
khiter_t k
table = kh_init_{{ttype}}()
{{if dtype == 'object'}}
build_count_table_{{dtype}}(values, table, 1)
{{else}}
build_count_table_{{dtype}}(values, table, dropna)
{{endif}}
result_keys = np.empty(table.n_occupied, '{{dtype}}')
result_counts = np.zeros(table.n_occupied, dtype=np.int64)
{{if dtype == 'object'}}
for k in range(table.n_buckets):
if kh_exist_{{ttype}}(table, k):
result_keys[i] = <{{dtype}}>table.keys[k]
result_counts[i] = table.vals[k]
i += 1
{{else}}
with nogil:
for k in range(table.n_buckets):
if kh_exist_{{ttype}}(table, k):
result_keys[i] = table.keys[k]
result_counts[i] = table.vals[k]
i += 1
{{endif}}
kh_destroy_{{ttype}}(table)
{{if dtype == 'object'}}
return result_keys, result_counts
{{else}}
return np.asarray(result_keys), np.asarray(result_counts)
{{endif}}
@cython.wraparound(False)
@cython.boundscheck(False)
{{if dtype == 'object'}}
def duplicated_{{dtype}}(ndarray[{{dtype}}] values, object keep='first'):
{{else}}
def duplicated_{{dtype}}(const {{c_type}}[:] values, object keep='first'):
{{endif}}
cdef:
int ret = 0
{{if dtype != 'object'}}
{{dtype}}_t value
{{endif}}
Py_ssize_t i, n = len(values)
khiter_t k
kh_{{ttype}}_t *table = kh_init_{{ttype}}()
ndarray[uint8_t, ndim=1, cast=True] out = np.empty(n, dtype='bool')
kh_resize_{{ttype}}(table, min(n, SIZE_HINT_LIMIT))
if keep not in ('last', 'first', False):
raise ValueError('keep must be either "first", "last" or False')
if keep == 'last':
{{if dtype == 'object'}}
for i in range(n - 1, -1, -1):
# equivalent: range(n)[::-1], which cython doesn't like in nogil
kh_put_{{ttype}}(table, <PyObject*>values[i], &ret)
out[i] = ret == 0
{{else}}
with nogil:
for i in range(n - 1, -1, -1):
# equivalent: range(n)[::-1], which cython doesn't like in nogil
kh_put_{{ttype}}(table, values[i], &ret)
out[i] = ret == 0
{{endif}}
elif keep == 'first':
{{if dtype == 'object'}}
for i in range(n):
kh_put_{{ttype}}(table, <PyObject*>values[i], &ret)
out[i] = ret == 0
{{else}}
with nogil:
for i in range(n):
kh_put_{{ttype}}(table, values[i], &ret)
out[i] = ret == 0
{{endif}}
else:
{{if dtype == 'object'}}
for i in range(n):
value = values[i]
k = kh_get_{{ttype}}(table, <PyObject*>value)
if k != table.n_buckets:
out[table.vals[k]] = 1
out[i] = 1
else:
k = kh_put_{{ttype}}(table, <PyObject*>value, &ret)
table.keys[k] = <PyObject*>value
table.vals[k] = i
out[i] = 0
{{else}}
with nogil:
for i in range(n):
value = values[i]
k = kh_get_{{ttype}}(table, value)
if k != table.n_buckets:
out[table.vals[k]] = 1
out[i] = 1
else:
k = kh_put_{{ttype}}(table, value, &ret)
table.keys[k] = value
table.vals[k] = i
out[i] = 0
{{endif}}
kh_destroy_{{ttype}}(table)
return out
# ----------------------------------------------------------------------
# Membership
# ----------------------------------------------------------------------
@cython.wraparound(False)
@cython.boundscheck(False)
{{if dtype == 'object'}}
def ismember_{{dtype}}(ndarray[{{c_type}}] arr, ndarray[{{c_type}}] values):
{{else}}
def ismember_{{dtype}}(const {{c_type}}[:] arr, const {{c_type}}[:] values):
{{endif}}
"""
Return boolean of values in arr on an
element by-element basis
Parameters
----------
arr : {{dtype}} ndarray
values : {{dtype}} ndarray
Returns
-------
boolean ndarry len of (arr)
"""
cdef:
Py_ssize_t i, n
khiter_t k
int ret = 0
ndarray[uint8_t] result
{{c_type}} val
kh_{{ttype}}_t *table = kh_init_{{ttype}}()
# construct the table
n = len(values)
kh_resize_{{ttype}}(table, n)
{{if dtype == 'object'}}
for i in range(n):
kh_put_{{ttype}}(table, <PyObject*>values[i], &ret)
{{else}}
with nogil:
for i in range(n):
kh_put_{{ttype}}(table, values[i], &ret)
{{endif}}
# test membership
n = len(arr)
result = np.empty(n, dtype=np.uint8)
{{if dtype == 'object'}}
for i in range(n):
val = arr[i]
k = kh_get_{{ttype}}(table, <PyObject*>val)
result[i] = (k != table.n_buckets)
{{else}}
with nogil:
for i in range(n):
val = arr[i]
k = kh_get_{{ttype}}(table, val)
result[i] = (k != table.n_buckets)
{{endif}}
kh_destroy_{{ttype}}(table)
return result.view(np.bool_)
{{endfor}}
# ----------------------------------------------------------------------
# Mode Computations
# ----------------------------------------------------------------------
{{py:
# dtype, ctype, table_type, npy_dtype
dtypes = [('float64', 'float64_t', 'float64', 'float64'),
('float32', 'float32_t', 'float32', 'float32'),
('int64', 'int64_t', 'int64', 'int64'),
('int32', 'int32_t', 'int32', 'int32'),
('int16', 'int16_t', 'int16', 'int16'),
('int8', 'int8_t', 'int8', 'int8'),
('uint64', 'uint64_t', 'uint64', 'uint64'),
('uint32', 'uint32_t', 'uint32', 'uint32'),
('uint16', 'uint16_t', 'uint16', 'uint16'),
('uint8', 'uint8_t', 'uint8', 'uint8'),
('object', 'object', 'pymap', 'object_')]
}}
{{for dtype, ctype, table_type, npy_dtype in dtypes}}
@cython.wraparound(False)
@cython.boundscheck(False)
{{if dtype == 'object'}}
def mode_{{dtype}}(ndarray[{{ctype}}] values, bint dropna):
{{else}}
def mode_{{dtype}}({{ctype}}[:] values, bint dropna):
{{endif}}
cdef:
int count, max_count = 1
int j = -1 # so you can do +=
# Don't use Py_ssize_t, since table.n_buckets is unsigned
khiter_t k
kh_{{table_type}}_t *table
ndarray[{{ctype}}] modes
table = kh_init_{{table_type}}()
build_count_table_{{dtype}}(values, table, dropna)
modes = np.empty(table.n_buckets, dtype=np.{{npy_dtype}})
{{if dtype != 'object'}}
with nogil:
for k in range(table.n_buckets):
if kh_exist_{{table_type}}(table, k):
count = table.vals[k]
if count == max_count:
j += 1
elif count > max_count:
max_count = count
j = 0
else:
continue
modes[j] = table.keys[k]
{{else}}
for k in range(table.n_buckets):
if kh_exist_{{table_type}}(table, k):
count = table.vals[k]
if count == max_count:
j += 1
elif count > max_count:
max_count = count
j = 0
else:
continue
modes[j] = <object>table.keys[k]
{{endif}}
kh_destroy_{{table_type}}(table)
return modes[:j + 1]
{{endfor}}