@@ -6,26 +6,77 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
66
77{{py:
88
9- # name, dtype, ttype, c_type, to_c_type
9+ # name, dtype, ttype, c_type, to_c_type, set_ttype
1010dtypes = [('Complex128', 'complex128', 'complex128',
11- 'khcomplex128_t', 'to_khcomplex128_t'),
11+ 'khcomplex128_t', 'to_khcomplex128_t', 'set_complex128' ),
1212 ('Complex64', 'complex64', 'complex64',
13- 'khcomplex64_t', 'to_khcomplex64_t'),
14- ('Float64', 'float64', 'float64', 'float64_t', ''),
15- ('Float32', 'float32', 'float32', 'float32_t', ''),
16- ('UInt64', 'uint64', 'uint64', 'uint64_t', ''),
17- ('UInt32', 'uint32', 'uint32', 'uint32_t', ''),
18- ('UInt16', 'uint16', 'uint16', 'uint16_t', ''),
19- ('UInt8', 'uint8', 'uint8', 'uint8_t', ''),
20- ('Object', 'object', 'pymap', 'object', '<PyObject*>'),
21- ('Int64', 'int64', 'int64', 'int64_t', ''),
22- ('Int32', 'int32', 'int32', 'int32_t', ''),
23- ('Int16', 'int16', 'int16', 'int16_t', ''),
24- ('Int8', 'int8', 'int8', 'int8_t', '')]
13+ 'khcomplex64_t', 'to_khcomplex64_t', 'set_complex64'),
14+ ('Float64', 'float64', 'float64', 'float64_t', '', 'set_float64'),
15+ ('Float32', 'float32', 'float32', 'float32_t', '', 'set_float32'),
16+ ('UInt64', 'uint64', 'uint64', 'uint64_t', '', 'set_uint64'),
17+ ('UInt32', 'uint32', 'uint32', 'uint32_t', '', 'set_uint32'),
18+ ('UInt16', 'uint16', 'uint16', 'uint16_t', '', 'set_uint16'),
19+ ('UInt8', 'uint8', 'uint8', 'uint8_t', '', 'set_uint8'),
20+ ('Object', 'object', 'pymap', 'object', '<PyObject*>', 'pyset'),
21+ ('Int64', 'int64', 'int64', 'int64_t', '', 'set_int64'),
22+ ('Int32', 'int32', 'int32', 'int32_t', '', 'set_int32'),
23+ ('Int16', 'int16', 'int16', 'int16_t', '', 'set_int16'),
24+ ('Int8', 'int8', 'int8', 'int8_t', '', 'set_int8')]
25+
26+ # Primitive set types used by ismember. Declared here (not via cimport) so
27+ # that their companion functions (kh_init_*, kh_put_*, etc.) are also in
28+ # scope. kh_pyset_t (object) is handled via khash.pxd / hashtable.pxd.
29+ # name, c_type
30+ primitive_set_types_for_ismember = [
31+ ('set_complex128', 'khcomplex128_t'),
32+ ('set_complex64', 'khcomplex64_t'),
33+ ('set_float64', 'float64_t'),
34+ ('set_float32', 'float32_t'),
35+ ('set_uint64', 'uint64_t'),
36+ ('set_uint32', 'uint32_t'),
37+ ('set_uint16', 'uint16_t'),
38+ ('set_uint8', 'uint8_t'),
39+ ('set_int64', 'int64_t'),
40+ ('set_int32', 'int32_t'),
41+ ('set_int16', 'int16_t'),
42+ ('set_int8', 'int8_t'),
43+ ]
2544
2645}}
2746
28- {{for name, dtype, ttype, c_type, to_c_type in dtypes}}
47+ {{for set_name, set_c_type in primitive_set_types_for_ismember}}
48+ cdef extern from "pandas/vendored/klib/khash_python.h":
49+ ctypedef struct kh_{{set_name}}_t:
50+ uint32_t n_buckets, size, n_occupied, upper_bound
51+ uint32_t *flags
52+ {{set_c_type}} *keys
53+ char *vals
54+
55+ kh_{{set_name}}_t* kh_init_{{set_name}}() nogil
56+ void kh_destroy_{{set_name}}(kh_{{set_name}}_t*) nogil
57+ uint32_t kh_get_{{set_name}}(kh_{{set_name}}_t*, {{set_c_type}}) nogil
58+ void kh_resize_{{set_name}}(kh_{{set_name}}_t*, uint32_t) nogil
59+ uint32_t kh_put_{{set_name}}(kh_{{set_name}}_t*, {{set_c_type}}, int*) nogil
60+
61+ {{endfor}}
62+
63+ # kh_pyset_t is the set counterpart to kh_pymap_t (object dtype).
64+ # Redeclared here so kh_init_pyset / kh_put_pyset / kh_get_pyset are in scope.
65+ from cpython.object cimport PyObject
66+ cdef extern from "pandas/vendored/klib/khash_python.h":
67+ ctypedef struct kh_pyset_t:
68+ uint32_t n_buckets, size, n_occupied, upper_bound
69+ uint32_t *flags
70+ PyObject **keys
71+ char *vals
72+
73+ kh_pyset_t* kh_init_pyset()
74+ void kh_destroy_pyset(kh_pyset_t*)
75+ uint32_t kh_get_pyset(kh_pyset_t*, PyObject*)
76+ void kh_resize_pyset(kh_pyset_t*, uint32_t)
77+ uint32_t kh_put_pyset(kh_pyset_t*, PyObject*, int*)
78+
79+ {{for name, dtype, ttype, c_type, to_c_type, set_ttype in dtypes}}
2980
3081
3182@cython.wraparound(False)
@@ -243,11 +294,11 @@ cdef ismember_{{dtype}}(const {{dtype}}_t[:] arr, const {{dtype}}_t[:] values):
243294 {{c_type}} val
244295 {{endif}}
245296
246- kh_{{ttype }}_t *table = kh_init_{{ttype }}()
297+ kh_{{set_ttype }}_t *table = kh_init_{{set_ttype }}()
247298
248299 # construct the table
249300 n = len(values)
250- kh_resize_{{ttype }}(table, n)
301+ kh_resize_{{set_ttype }}(table, n)
251302
252303 {{if dtype == 'object'}}
253304 if True:
@@ -256,7 +307,7 @@ cdef ismember_{{dtype}}(const {{dtype}}_t[:] arr, const {{dtype}}_t[:] values):
256307 {{endif}}
257308 for i in range(n):
258309 val = {{to_c_type}}(values[i])
259- kh_put_{{ttype }}(table, val, &ret)
310+ kh_put_{{set_ttype }}(table, val, &ret)
260311
261312 # test membership
262313 n = len(arr)
@@ -269,10 +320,10 @@ cdef ismember_{{dtype}}(const {{dtype}}_t[:] arr, const {{dtype}}_t[:] values):
269320 {{endif}}
270321 for i in range(n):
271322 val = {{to_c_type}}(arr[i])
272- k = kh_get_{{ttype }}(table, val)
323+ k = kh_get_{{set_ttype }}(table, val)
273324 result[i] = (k != table.n_buckets)
274325
275- kh_destroy_{{ttype }}(table)
326+ kh_destroy_{{set_ttype }}(table)
276327 return result.view(np.bool_)
277328
278329# ----------------------------------------------------------------------
0 commit comments