Skip to content

Commit 6846643

Browse files
committed
using to_c_type, rather than a switch
1 parent 873f2a2 commit 6846643

File tree

2 files changed

+45
-113
lines changed

2 files changed

+45
-113
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -336,24 +336,24 @@ cdef class HashTable:
336336

337337
{{py:
338338

339-
# name, dtype, c_type, float_group, complex_group
340-
dtypes = [('Complex128', 'complex128', 'khcomplex128_t', True, True),
341-
('Float64', 'float64', 'float64_t', True, False),
342-
('UInt64', 'uint64', 'uint64_t', False, False),
343-
('Int64', 'int64', 'int64_t', False, False),
344-
('Complex64', 'complex64', 'khcomplex64_t', True, True),
345-
('Float32', 'float32', 'float32_t', True, False),
346-
('UInt32', 'uint32', 'uint32_t', False, False),
347-
('Int32', 'int32', 'int32_t', False, False),
348-
('UInt16', 'uint16', 'uint16_t', False, False),
349-
('Int16', 'int16', 'int16_t', False, False),
350-
('UInt8', 'uint8', 'uint8_t', False, False),
351-
('Int8', 'int8', 'int8_t', False, False)]
339+
# name, dtype, c_type, float_group, complex_group, to_c_type
340+
dtypes = [('Complex128', 'complex128', 'khcomplex128_t', True, True, "to_khcomplex128_t"),
341+
('Float64', 'float64', 'float64_t', True, False, ""),
342+
('UInt64', 'uint64', 'uint64_t', False, False, ""),
343+
('Int64', 'int64', 'int64_t', False, False, ""),
344+
('Complex64', 'complex64', 'khcomplex64_t', True, True, "to_khcomplex64_t"),
345+
('Float32', 'float32', 'float32_t', True, False, ""),
346+
('UInt32', 'uint32', 'uint32_t', False, False, ""),
347+
('Int32', 'int32', 'int32_t', False, False, ""),
348+
('UInt16', 'uint16', 'uint16_t', False, False, ""),
349+
('Int16', 'int16', 'int16_t', False, False, ""),
350+
('UInt8', 'uint8', 'uint8_t', False, False, ""),
351+
('Int8', 'int8', 'int8_t', False, False, "")]
352352

353353
}}
354354

355355

356-
{{for name, dtype, c_type, float_group, complex_group in dtypes}}
356+
{{for name, dtype, c_type, float_group, complex_group, to_c_type in dtypes}}
357357

358358
cdef class {{name}}HashTable(HashTable):
359359

@@ -375,11 +375,7 @@ cdef class {{name}}HashTable(HashTable):
375375
cdef:
376376
khiter_t k
377377
{{c_type}} ckey
378-
{{if complex_group}}
379-
ckey = to_{{c_type}}(key)
380-
{{else}}
381-
ckey = key
382-
{{endif}}
378+
ckey = {{to_c_type}}(key)
383379
k = kh_get_{{dtype}}(self.table, ckey)
384380
return k != self.table.n_buckets
385381

@@ -395,11 +391,7 @@ cdef class {{name}}HashTable(HashTable):
395391
cdef:
396392
khiter_t k
397393
{{c_type}} cval
398-
{{if complex_group}}
399-
cval = to_{{c_type}}(val)
400-
{{else}}
401-
cval = val
402-
{{endif}}
394+
cval = {{to_c_type}}(val)
403395
k = kh_get_{{dtype}}(self.table, cval)
404396
if k != self.table.n_buckets:
405397
return self.table.vals[k]
@@ -411,11 +403,7 @@ cdef class {{name}}HashTable(HashTable):
411403
khiter_t k
412404
int ret = 0
413405
{{c_type}} ckey
414-
{{if complex_group}}
415-
ckey = to_{{c_type}}(key)
416-
{{else}}
417-
ckey = key
418-
{{endif}}
406+
ckey = {{to_c_type}}(key)
419407
k = kh_put_{{dtype}}(self.table, ckey, &ret)
420408
if kh_exist_{{dtype}}(self.table, k):
421409
self.table.vals[k] = val
@@ -432,11 +420,7 @@ cdef class {{name}}HashTable(HashTable):
432420

433421
with nogil:
434422
for i in range(n):
435-
{{if complex_group}}
436-
key = to_{{c_type}}(keys[i])
437-
{{else}}
438-
key = keys[i]
439-
{{endif}}
423+
key = {{to_c_type}}(keys[i])
440424
k = kh_put_{{dtype}}(self.table, key, &ret)
441425
self.table.vals[k] = <Py_ssize_t>values[i]
442426

@@ -450,11 +434,7 @@ cdef class {{name}}HashTable(HashTable):
450434

451435
with nogil:
452436
for i in range(n):
453-
{{if complex_group}}
454-
val= to_{{c_type}}(values[i])
455-
{{else}}
456-
val = values[i]
457-
{{endif}}
437+
val= {{to_c_type}}(values[i])
458438
k = kh_put_{{dtype}}(self.table, val, &ret)
459439
self.table.vals[k] = i
460440

@@ -469,11 +449,7 @@ cdef class {{name}}HashTable(HashTable):
469449

470450
with nogil:
471451
for i in range(n):
472-
{{if complex_group}}
473-
val = to_{{c_type}}(values[i])
474-
{{else}}
475-
val = values[i]
476-
{{endif}}
452+
val = {{to_c_type}}(values[i])
477453
k = kh_get_{{dtype}}(self.table, val)
478454
if k != self.table.n_buckets:
479455
locs[i] = self.table.vals[k]
@@ -550,25 +526,13 @@ cdef class {{name}}HashTable(HashTable):
550526
# We use None, to make it optional, which requires `object` type
551527
# for the parameter. To please the compiler, we use na_value2,
552528
# which is only used if it's *specified*.
553-
{{if complex_group}}
554-
na_value2 = to_{{c_type}}(na_value)
555-
{{else}}
556-
na_value2 = na_value
557-
{{endif}}
529+
na_value2 = {{to_c_type}}(na_value)
558530
else:
559-
{{if complex_group}}
560-
na_value2 = to_{{c_type}}(0)
561-
{{else}}
562-
na_value2 = 0
563-
{{endif}}
531+
na_value2 = {{to_c_type}}(0)
564532

565533
with nogil:
566534
for i in range(n):
567-
{{if complex_group}}
568-
val = to_{{c_type}}(values[i])
569-
{{else}}
570-
val = values[i]
571-
{{endif}}
535+
val = {{to_c_type}}(values[i])
572536

573537
if ignore_na and use_mask:
574538
if mask_values[i]:
@@ -702,11 +666,7 @@ cdef class {{name}}HashTable(HashTable):
702666

703667
with nogil:
704668
for i in range(n):
705-
{{if complex_group}}
706-
val = to_{{c_type}}(values[i])
707-
{{else}}
708-
val = values[i]
709-
{{endif}}
669+
val = {{to_c_type}}(values[i])
710670

711671
# specific for groupby
712672
{{if dtype == 'complex64' or dtype== 'complex128'}}

pandas/_libs/hashtable_func_helper.pxi.in

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
66

77
{{py:
88

9-
# dtype, ttype, c_type, complex_group
10-
dtypes = [('complex128', 'complex128', 'khcomplex128_t', True),
11-
('complex64', 'complex64', 'khcomplex64_t', True),
12-
('float64', 'float64', 'float64_t', False),
13-
('float32', 'float32', 'float32_t', False),
14-
('uint64', 'uint64', 'uint64_t', False),
15-
('uint32', 'uint32', 'uint32_t', False),
16-
('uint16', 'uint16', 'uint16_t', False),
17-
('uint8', 'uint8', 'uint8_t', False),
18-
('object', 'pymap', 'object', False),
19-
('int64', 'int64', 'int64_t', False),
20-
('int32', 'int32', 'int32_t', False),
21-
('int16', 'int16', 'int16_t', False),
22-
('int8', 'int8', 'int8_t', False)]
9+
# dtype, ttype, c_type, complex_group, to_c_type
10+
dtypes = [('complex128', 'complex128', 'khcomplex128_t', True, "to_khcomplex128_t"),
11+
('complex64', 'complex64', 'khcomplex64_t', True, "to_khcomplex64_t"),
12+
('float64', 'float64', 'float64_t', False, ""),
13+
('float32', 'float32', 'float32_t', False, ""),
14+
('uint64', 'uint64', 'uint64_t', False, ""),
15+
('uint32', 'uint32', 'uint32_t', False, ""),
16+
('uint16', 'uint16', 'uint16_t', False, ""),
17+
('uint8', 'uint8', 'uint8_t', False, ""),
18+
('object', 'pymap', 'object', False, ""),
19+
('int64', 'int64', 'int64_t', False, ""),
20+
('int32', 'int32', 'int32_t', False, ""),
21+
('int16', 'int16', 'int16_t', False, ""),
22+
('int8', 'int8', 'int8_t', False, "")]
2323

2424
}}
2525

26-
{{for dtype, ttype, c_type, complex_group in dtypes}}
26+
{{for dtype, ttype, c_type, complex_group, to_c_type in dtypes}}
2727

2828

2929
@cython.wraparound(False)
@@ -47,11 +47,7 @@ cdef build_count_table_{{dtype}}(const {{dtype}}_t[:] values,
4747
kh_resize_{{ttype}}(table, n // 10)
4848

4949
for i in range(n):
50-
{{if complex_group}}
51-
val = to_{{c_type}}(values[i])
52-
{{else}}
5350
val = values[i]
54-
{{endif}}
5551
if not checknull(val) or not dropna:
5652
k = kh_get_{{ttype}}(table, <PyObject*>val)
5753
if k != table.n_buckets:
@@ -64,11 +60,7 @@ cdef build_count_table_{{dtype}}(const {{dtype}}_t[:] values,
6460
kh_resize_{{ttype}}(table, n)
6561

6662
for i in range(n):
67-
{{if complex_group}}
68-
val = to_{{c_type}}(values[i])
69-
{{else}}
70-
val = values[i]
71-
{{endif}}
63+
val = {{to_c_type}}(values[i])
7264

7365
{{if dtype == 'float64' or dtype == 'float32'}}
7466
if val == val or not dropna:
@@ -175,11 +167,7 @@ def duplicated_{{dtype}}(const {{dtype}}_t[:] values, object keep='first'):
175167
with nogil:
176168
for i in range(n - 1, -1, -1):
177169
# equivalent: range(n)[::-1], which cython doesn't like in nogil
178-
{{if complex_group}}
179-
value = to_{{c_type}}(values[i])
180-
{{else}}
181-
value = values[i]
182-
{{endif}}
170+
value = {{to_c_type}}(values[i])
183171
kh_put_{{ttype}}(table, value, &ret)
184172
out[i] = ret == 0
185173
{{endif}}
@@ -191,11 +179,7 @@ def duplicated_{{dtype}}(const {{dtype}}_t[:] values, object keep='first'):
191179
{{else}}
192180
with nogil:
193181
for i in range(n):
194-
{{if complex_group}}
195-
value = to_{{c_type}}(values[i])
196-
{{else}}
197-
value = values[i]
198-
{{endif}}
182+
value = {{to_c_type}}(values[i])
199183
kh_put_{{ttype}}(table, value, &ret)
200184
out[i] = ret == 0
201185
{{endif}}
@@ -214,11 +198,7 @@ def duplicated_{{dtype}}(const {{dtype}}_t[:] values, object keep='first'):
214198
{{else}}
215199
with nogil:
216200
for i in range(n):
217-
{{if complex_group}}
218-
value = to_{{c_type}}(values[i])
219-
{{else}}
220-
value = values[i]
221-
{{endif}}
201+
value = {{to_c_type}}(values[i])
222202
k = kh_get_{{ttype}}(table, value)
223203
if k != table.n_buckets:
224204
out[table.vals[k]] = 1
@@ -275,11 +255,7 @@ def ismember_{{dtype}}(const {{dtype}}_t[:] arr, const {{dtype}}_t[:] values):
275255
{{else}}
276256
with nogil:
277257
for i in range(n):
278-
{{if complex_group}}
279-
val = to_{{c_type}}(values[i])
280-
{{else}}
281-
val = values[i]
282-
{{endif}}
258+
val = {{to_c_type}}(values[i])
283259
kh_put_{{ttype}}(table, val, &ret)
284260
{{endif}}
285261

@@ -295,11 +271,7 @@ def ismember_{{dtype}}(const {{dtype}}_t[:] arr, const {{dtype}}_t[:] values):
295271
{{else}}
296272
with nogil:
297273
for i in range(n):
298-
{{if complex_group}}
299-
val = to_{{c_type}}(arr[i])
300-
{{else}}
301-
val = arr[i]
302-
{{endif}}
274+
val = {{to_c_type}}(arr[i])
303275
k = kh_get_{{ttype}}(table, val)
304276
result[i] = (k != table.n_buckets)
305277
{{endif}}

0 commit comments

Comments
 (0)