@@ -12,11 +12,8 @@ from libcpp.map cimport map as cpp_map
12
12
13
13
import numpy as np
14
14
15
- # DTYPE = np.float64
16
- # ctypedef cnp.float64_t DTYPE_t
15
+ from ._typedefs cimport float64_t, intp_t
17
16
18
- # ITYPE = np.intp
19
- # ctypedef cnp.intp_t ITYPE_t
20
17
21
18
# ##############################################################################
22
19
# An object to be used in Python
@@ -30,8 +27,8 @@ cdef class IntFloatDict:
30
27
31
28
def __init__ (
32
29
self ,
33
- ITYPE_t [:] keys ,
34
- DTYPE_t [:] values ,
30
+ intp_t [:] keys ,
31
+ float64_t [:] values ,
35
32
):
36
33
cdef int i
37
34
cdef int size = values.size
@@ -44,7 +41,7 @@ cdef class IntFloatDict:
44
41
return self .my_map.size()
45
42
46
43
def __getitem__ (self , int key ):
47
- cdef cpp_map[ITYPE_t, DTYPE_t ].iterator it = self .my_map.find(key)
44
+ cdef cpp_map[intp_t, float64_t ].iterator it = self .my_map.find(key)
48
45
if it == self .my_map.end():
49
46
# The key is not in the dict
50
47
raise KeyError (' %i ' % key)
@@ -56,20 +53,20 @@ cdef class IntFloatDict:
56
53
# Cython 0.20 generates buggy code below. Commenting this out for now
57
54
# and relying on the to_arrays method
58
55
# def __iter__(self):
59
- # cdef cpp_map[ITYPE_t, DTYPE_t ].iterator it = self.my_map.begin()
60
- # cdef cpp_map[ITYPE_t, DTYPE_t ].iterator end = self.my_map.end()
56
+ # cdef cpp_map[intp_t, float64_t ].iterator it = self.my_map.begin()
57
+ # cdef cpp_map[intp_t, float64_t ].iterator end = self.my_map.end()
61
58
# while it != end:
62
59
# yield deref(it).first, deref(it).second
63
60
# inc(it)
64
61
65
62
def __iter__ (self ):
66
63
cdef int size = self .my_map.size()
67
- cdef ITYPE_t [:] keys = np.empty(size, dtype = np.intp)
68
- cdef DTYPE_t [:] values = np.empty(size, dtype = np.float64)
64
+ cdef intp_t [:] keys = np.empty(size, dtype = np.intp)
65
+ cdef float64_t [:] values = np.empty(size, dtype = np.float64)
69
66
self ._to_arrays(keys, values)
70
67
cdef int idx
71
- cdef ITYPE_t key
72
- cdef DTYPE_t value
68
+ cdef intp_t key
69
+ cdef float64_t value
73
70
for idx in range (size):
74
71
key = keys[idx]
75
72
value = values[idx]
@@ -92,10 +89,10 @@ cdef class IntFloatDict:
92
89
self ._to_arrays(keys, values)
93
90
return keys, values
94
91
95
- cdef _to_arrays(self , ITYPE_t [:] keys, DTYPE_t [:] values):
92
+ cdef _to_arrays(self , intp_t [:] keys, float64_t [:] values):
96
93
# Internal version of to_arrays that takes already-initialized arrays
97
- cdef cpp_map[ITYPE_t, DTYPE_t ].iterator it = self .my_map.begin()
98
- cdef cpp_map[ITYPE_t, DTYPE_t ].iterator end = self .my_map.end()
94
+ cdef cpp_map[intp_t, float64_t ].iterator it = self .my_map.begin()
95
+ cdef cpp_map[intp_t, float64_t ].iterator end = self .my_map.end()
99
96
cdef int index = 0
100
97
while it != end:
101
98
keys[index] = deref(it).first
@@ -104,8 +101,8 @@ cdef class IntFloatDict:
104
101
index += 1
105
102
106
103
def update (self , IntFloatDict other ):
107
- cdef cpp_map[ITYPE_t, DTYPE_t ].iterator it = other.my_map.begin()
108
- cdef cpp_map[ITYPE_t, DTYPE_t ].iterator end = other.my_map.end()
104
+ cdef cpp_map[intp_t, float64_t ].iterator it = other.my_map.begin()
105
+ cdef cpp_map[intp_t, float64_t ].iterator end = other.my_map.end()
109
106
while it != end:
110
107
self .my_map[deref(it).first] = deref(it).second
111
108
inc(it)
@@ -116,9 +113,9 @@ cdef class IntFloatDict:
116
113
out_obj.my_map = self .my_map
117
114
return out_obj
118
115
119
- def append (self , ITYPE_t key , DTYPE_t value ):
116
+ def append (self , intp_t key , float64_t value ):
120
117
# Construct our arguments
121
- cdef pair[ITYPE_t, DTYPE_t ] args
118
+ cdef pair[intp_t, float64_t ] args
122
119
args.first = key
123
120
args.second = value
124
121
self .my_map.insert(args)
@@ -128,10 +125,10 @@ cdef class IntFloatDict:
128
125
# operation on dict
129
126
130
127
def argmin (IntFloatDict d ):
131
- cdef cpp_map[ITYPE_t, DTYPE_t ].iterator it = d.my_map.begin()
132
- cdef cpp_map[ITYPE_t, DTYPE_t ].iterator end = d.my_map.end()
133
- cdef ITYPE_t min_key = - 1
134
- cdef DTYPE_t min_value = np.inf
128
+ cdef cpp_map[intp_t, float64_t ].iterator it = d.my_map.begin()
129
+ cdef cpp_map[intp_t, float64_t ].iterator end = d.my_map.end()
130
+ cdef intp_t min_key = - 1
131
+ cdef float64_t min_value = np.inf
135
132
while it != end:
136
133
if deref(it).second < min_value:
137
134
min_value = deref(it).second
0 commit comments