forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable_func_helper.pxi.in
196 lines (152 loc) · 5.15 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
"""
Template for each `dtype` helper function for hashtable
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""
#----------------------------------------------------------------------
# VectorData
#----------------------------------------------------------------------
{{py:
# name
dtypes = ['float64', 'int64', 'uint64']
}}
{{for dtype in dtypes}}
@cython.wraparound(False)
@cython.boundscheck(False)
cdef build_count_table_{{dtype}}({{dtype}}_t[:] values,
kh_{{dtype}}_t *table, bint dropna):
cdef:
khiter_t k
Py_ssize_t i, n = len(values)
{{dtype}}_t val
int ret = 0
with nogil:
kh_resize_{{dtype}}(table, n)
for i in range(n):
val = values[i]
if val == val or not dropna:
k = kh_get_{{dtype}}(table, val)
if k != table.n_buckets:
table.vals[k] += 1
else:
k = kh_put_{{dtype}}(table, val, &ret)
table.vals[k] = 1
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef value_count_{{dtype}}({{dtype}}_t[:] values, bint dropna):
cdef:
Py_ssize_t i=0
kh_{{dtype}}_t *table
{{dtype}}_t[:] result_keys
int64_t[:] result_counts
int k
table = kh_init_{{dtype}}()
build_count_table_{{dtype}}(values, table, dropna)
result_keys = np.empty(table.n_occupied, dtype=np.{{dtype}})
result_counts = np.zeros(table.n_occupied, dtype=np.int64)
with nogil:
for k in range(table.n_buckets):
if kh_exist_{{dtype}}(table, k):
result_keys[i] = table.keys[k]
result_counts[i] = table.vals[k]
i += 1
kh_destroy_{{dtype}}(table)
return np.asarray(result_keys), np.asarray(result_counts)
@cython.wraparound(False)
@cython.boundscheck(False)
def duplicated_{{dtype}}({{dtype}}_t[:] values,
object keep='first'):
cdef:
int ret = 0, k
{{dtype}}_t value
Py_ssize_t i, n = len(values)
kh_{{dtype}}_t * table = kh_init_{{dtype}}()
ndarray[uint8_t, ndim=1, cast=True] out = np.empty(n, dtype='bool')
kh_resize_{{dtype}}(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':
with nogil:
for i from n > i >=0:
kh_put_{{dtype}}(table, values[i], &ret)
out[i] = ret == 0
elif keep == 'first':
with nogil:
for i from 0 <= i < n:
kh_put_{{dtype}}(table, values[i], &ret)
out[i] = ret == 0
else:
with nogil:
for i from 0 <= i < n:
value = values[i]
k = kh_get_{{dtype}}(table, value)
if k != table.n_buckets:
out[table.vals[k]] = 1
out[i] = 1
else:
k = kh_put_{{dtype}}(table, value, &ret)
table.keys[k] = value
table.vals[k] = i
out[i] = 0
kh_destroy_{{dtype}}(table)
return out
{{endfor}}
#----------------------------------------------------------------------
# Mode Computations
#----------------------------------------------------------------------
{{py:
# dtype, ctype, table_type, npy_dtype
dtypes = [('int64', 'int64_t', 'int64', 'int64'),
('uint64', 'uint64_t', 'uint64', 'uint64'),
('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,
ndarray[uint8_t, cast=True] mask):
{{else}}
def mode_{{dtype}}({{ctype}}[:] values):
{{endif}}
cdef:
int count, max_count = 2
int j = -1 # so you can do +=
int k
kh_{{table_type}}_t *table
ndarray[{{ctype}}] modes
table = kh_init_{{table_type}}()
{{if dtype == 'object'}}
build_count_table_{{dtype}}(values, mask, table)
{{else}}
build_count_table_{{dtype}}(values, table, 0)
{{endif}}
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}}