@@ -11,14 +11,14 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
1111{{py:
1212
1313# dtype, ttype
14- dtypes = [('float64', 'float64'),
15- ('uint64', 'uint64'),
16- ('object', 'pymap'),
17- ('int64', 'int64')]
14+ dtypes = [('float64', 'float64', 'float64_t' ),
15+ ('uint64', 'uint64', 'uint64_t' ),
16+ ('object', 'pymap', 'object' ),
17+ ('int64', 'int64', 'int64_t' )]
1818
1919}}
2020
21- {{for dtype, ttype in dtypes}}
21+ {{for dtype, ttype, scalar in dtypes}}
2222
2323
2424@cython.wraparound(False)
@@ -34,9 +34,7 @@ cdef build_count_table_{{dtype}}({{dtype}}_t[:] values,
3434 khiter_t k
3535 Py_ssize_t i, n = len(values)
3636
37- {{if dtype != 'object'}}
38- {{dtype}}_t val
39- {{endif}}
37+ {{scalar}} val
4038
4139 int ret = 0
4240
@@ -79,7 +77,7 @@ cdef build_count_table_{{dtype}}({{dtype}}_t[:] values,
7977{{if dtype == 'object'}}
8078cpdef value_count_{{dtype}}(ndarray[{{dtype}}] values, bint dropna):
8179{{else}}
82- cpdef value_count_{{dtype}}({{dtype}}_t [:] values, bint dropna):
80+ cpdef value_count_{{dtype}}({{scalar}} [:] values, bint dropna):
8381{{endif}}
8482 cdef:
8583 Py_ssize_t i=0
@@ -130,12 +128,11 @@ cpdef value_count_{{dtype}}({{dtype}}_t[:] values, bint dropna):
130128@cython.boundscheck(False)
131129{{if dtype == 'object'}}
132130
133-
134131def duplicated_{{dtype}}(ndarray[{{dtype}}] values, object keep='first'):
135132{{else}}
136133
137134
138- def duplicated_{{dtype}}({{dtype}}_t [:] values, object keep='first'):
135+ def duplicated_{{dtype}}({{scalar}} [:] values, object keep='first'):
139136{{endif}}
140137 cdef:
141138 int ret = 0
@@ -203,8 +200,87 @@ def duplicated_{{dtype}}({{dtype}}_t[:] values, object keep='first'):
203200 kh_destroy_{{ttype}}(table)
204201 return out
205202
203+
204+ #----------------------------------------------------------------------
205+ # Membership
206+ #----------------------------------------------------------------------
207+
208+
209+ @cython.wraparound(False)
210+ @cython.boundscheck(False)
211+ {{if dtype == 'object'}}
212+
213+ def ismember_{{dtype}}(ndarray[{{scalar}}] arr, ndarray[{{scalar}}] values, bint hasnans=0):
214+ {{else}}
215+
216+ def ismember_{{dtype}}({{scalar}}[:] arr, {{scalar}}[:] values, bint hasnans=0):
217+ {{endif}}
218+
219+ """
220+ Return boolean of values in arr on an
221+ element by-element basis
222+
223+ Parameters
224+ ----------
225+ arr : {{dtype}} ndarray
226+ values : {{dtype}} ndarray
227+ hasnans : bint, optional
228+
229+ Returns
230+ -------
231+ boolean ndarry len of (arr)
232+ """
233+ cdef:
234+ Py_ssize_t i, n, k
235+ int ret = 0
236+ ndarray[uint8_t] result
237+ {{scalar}} val
238+ kh_{{ttype}}_t * table = kh_init_{{ttype}}()
239+
240+
241+ # construct the table
242+ n = len(values)
243+ kh_resize_{{ttype}}(table, min(n, len(values)))
244+
245+ {{if dtype == 'object'}}
246+ for i in range(n):
247+ kh_put_{{ttype}}(table, <PyObject*> values[i], &ret)
248+ {{else}}
249+ with nogil:
250+ for i in range(n):
251+ kh_put_{{ttype}}(table, values[i], &ret)
252+ {{endif}}
253+
254+ # test membership
255+ n = len(arr)
256+ result = np.empty(n, dtype=np.uint8)
257+
258+ {{if dtype == 'object'}}
259+ for i in range(n):
260+ val = arr[i]
261+ k = kh_get_{{ttype}}(table, <PyObject*> val)
262+ if k != table.n_buckets:
263+ result[i] = 1
264+ else:
265+ result[i] = hasnans and val != val
266+ {{else}}
267+ with nogil:
268+ for i in range(n):
269+ val = arr[i]
270+ k = kh_get_{{ttype}}(table, val)
271+ if k != table.n_buckets:
272+ result[i] = 1
273+ else:
274+ result[i] = hasnans and val != val
275+ {{endif}}
276+
277+ kh_destroy_{{ttype}}(table)
278+ return result.view(np.bool_)
279+
206280{{endfor}}
207281
282+
283+
208284#----------------------------------------------------------------------
209285# Mode Computations
210286#----------------------------------------------------------------------
0 commit comments