@@ -401,6 +401,7 @@ TriMap_finalize(TriMapObject *self, PyObject *Py_UNUSED(unused)) {
401
401
402
402
npy_intp dims [] = {tm -> len };
403
403
404
+ // initialize all to False
404
405
final_src_match = PyArray_ZEROS (1 , dims , NPY_BOOL , 0 );
405
406
if (final_src_match == NULL ) {
406
407
goto error ;
@@ -855,7 +856,7 @@ AK_TM_fill_object(TriMapObject* tm,
855
856
return 0 ;
856
857
}
857
858
858
- #define AK_TM_TRANSFER_FLEXIBLE (c_type ) do { \
859
+ #define AK_TM_TRANSFER_FLEXIBLE (c_type , from_src , array_from , array_to ) do { \
859
860
Py_ssize_t one_count = from_src ? tm->src_one_count : tm->dst_one_count;\
860
861
TriMapOne* one_pairs = from_src ? tm->src_one : tm->dst_one; \
861
862
npy_intp t_element_size = PyArray_ITEMSIZE(array_to); \
@@ -1003,10 +1004,10 @@ AK_TM_map_no_fill(TriMapObject* tm,
1003
1004
}
1004
1005
}
1005
1006
else if (dtype_is_unicode ) {
1006
- AK_TM_TRANSFER_FLEXIBLE (Py_UCS4 );
1007
+ AK_TM_TRANSFER_FLEXIBLE (Py_UCS4 , from_src , array_from , array_to );
1007
1008
}
1008
1009
else if (dtype_is_string ) {
1009
- AK_TM_TRANSFER_FLEXIBLE (char );
1010
+ AK_TM_TRANSFER_FLEXIBLE (char , from_src , array_from , array_to );
1010
1011
}
1011
1012
else {
1012
1013
if (AK_TM_transfer_scalar (tm , from_src , array_from , array_to )) {
@@ -1050,19 +1051,19 @@ TriMap_map_dst_no_fill(TriMapObject *self, PyObject *arg) {
1050
1051
1051
1052
1052
1053
static inline PyObject *
1053
- TriMap_map_merge_no_fill (TriMapObject * self , PyObject * args )
1054
+ TriMap_map_merge (TriMapObject * tm , PyObject * args )
1054
1055
{
1055
1056
PyArrayObject * array_src ;
1056
1057
PyArrayObject * array_dst ;
1057
1058
1058
1059
if (!PyArg_ParseTuple (args ,
1059
- "O!O!:map_merge_no_fill " ,
1060
+ "O!O!:map_merge " ,
1060
1061
& PyArray_Type , & array_src ,
1061
1062
& PyArray_Type , & array_dst
1062
1063
)) {
1063
1064
return NULL ;
1064
1065
}
1065
- if (!self -> finalized ) {
1066
+ if (!tm -> finalized ) {
1066
1067
PyErr_SetString (PyExc_RuntimeError , "Finalization is required" );
1067
1068
return NULL ;
1068
1069
}
@@ -1083,7 +1084,7 @@ TriMap_map_merge_no_fill(TriMapObject *self, PyObject *args)
1083
1084
bool dtype_is_unicode = dtype -> type_num == NPY_UNICODE ;
1084
1085
bool dtype_is_string = dtype -> type_num == NPY_STRING ;
1085
1086
1086
- npy_intp dims [] = {self -> len };
1087
+ npy_intp dims [] = {tm -> len };
1087
1088
1088
1089
// create to array_to
1089
1090
PyArrayObject * array_to ;
@@ -1118,7 +1119,41 @@ TriMap_map_merge_no_fill(TriMapObject *self, PyObject *args)
1118
1119
return NULL ;
1119
1120
}
1120
1121
1121
- Py_RETURN_NONE ;
1122
+ // if we have fill values in src, we need to transfer from dst
1123
+ bool transfer_from_dst = PyArray_SIZE ((PyArrayObject * )tm -> final_src_fill ) != 0 ;
1124
+
1125
+ if (dtype_is_obj ) {
1126
+ if (AK_TM_transfer_object (tm , true, array_src , array_to )) {
1127
+ Py_DECREF ((PyObject * )array_to );
1128
+ return NULL ;
1129
+ }
1130
+ }
1131
+ else if (dtype_is_unicode ) {
1132
+ AK_TM_TRANSFER_FLEXIBLE (Py_UCS4 , true, array_src , array_to );
1133
+ if (transfer_from_dst ) {
1134
+ AK_TM_TRANSFER_FLEXIBLE (Py_UCS4 , false, array_dst , array_to );
1135
+ }
1136
+ }
1137
+ else if (dtype_is_string ) {
1138
+ AK_TM_TRANSFER_FLEXIBLE (char , true, array_src , array_to );
1139
+ if (transfer_from_dst ) {
1140
+ AK_TM_TRANSFER_FLEXIBLE (char , false, array_dst , array_to );
1141
+ }
1142
+ }
1143
+ else {
1144
+ if (AK_TM_transfer_scalar (tm , true, array_src , array_to )) {
1145
+ Py_DECREF ((PyObject * )array_to );
1146
+ return NULL ;
1147
+ }
1148
+ if (transfer_from_dst ) {
1149
+ if (AK_TM_transfer_scalar (tm , false, array_dst , array_to )) {
1150
+ Py_DECREF ((PyObject * )array_to );
1151
+ return NULL ;
1152
+ }
1153
+ }
1154
+ }
1155
+
1156
+ return (PyObject * )array_to ;
1122
1157
1123
1158
// // transfer values
1124
1159
// if (dtype_is_obj) {
@@ -1205,19 +1240,19 @@ AK_TM_map_fill(TriMapObject* tm,
1205
1240
}
1206
1241
}
1207
1242
else if (dtype_is_unicode ) {
1208
- AK_TM_TRANSFER_FLEXIBLE (Py_UCS4 );
1243
+ AK_TM_TRANSFER_FLEXIBLE (Py_UCS4 , from_src , array_from , array_to );
1209
1244
if (AK_TM_fill_unicode (tm , from_src , array_to , fill_value )) {
1210
1245
goto error ;
1211
1246
}
1212
1247
}
1213
1248
else if (dtype_is_string ) {
1214
- AK_TM_TRANSFER_FLEXIBLE (char );
1249
+ AK_TM_TRANSFER_FLEXIBLE (char , from_src , array_from , array_to );
1215
1250
if (AK_TM_fill_string (tm , from_src , array_to , fill_value )) {
1216
1251
goto error ;
1217
1252
}
1218
1253
}
1219
1254
else {
1220
- // Most simple is to fill with scalar, then overwrite values as needed; for object and flexible dtypes this is not efficient; for object dtypes, this obbligates us to decref the filled value when assigning
1255
+ // Most simple is to fill with scalar, then overwrite values as needed; for object and flexible dtypes this is not efficient; for object dtypes, this obligates us to decref the filled value when assigning
1221
1256
if (PyArray_FillWithScalar (array_to , fill_value )) { // -1 on error
1222
1257
goto error ;
1223
1258
}
@@ -1279,29 +1314,6 @@ TriMap_map_dst_fill(TriMapObject *self, PyObject *args) {
1279
1314
1280
1315
1281
1316
1282
- // PyObject *
1283
- // TriMap_map_merge_fill(TriMapObject *self, PyObject *args) {
1284
- // PyArrayObject* array_from;
1285
- // PyObject* fill_value;
1286
- // PyArray_Descr* fill_value_dtype;
1287
- // if (!PyArg_ParseTuple(args,
1288
- // "O!OO!:map_dst_fill",
1289
- // &PyArray_Type, &array_from,
1290
- // &fill_value,
1291
- // &PyArrayDescr_Type, &fill_value_dtype
1292
- // )) {
1293
- // return NULL;
1294
- // }
1295
- // if (!self->finalized) {
1296
- // PyErr_SetString(PyExc_RuntimeError, "Finalization is required");
1297
- // return NULL;
1298
- // }
1299
- // bool from_src = false;
1300
- // return AK_TM_map_fill(self, from_src, array_from, fill_value, fill_value_dtype);
1301
- // }
1302
-
1303
-
1304
-
1305
1317
static PyMethodDef TriMap_methods [] = {
1306
1318
{"register_one" , (PyCFunction )TriMap_register_one , METH_VARARGS , NULL },
1307
1319
{"register_unmatched_dst" , (PyCFunction )TriMap_register_unmatched_dst , METH_NOARGS , NULL },
@@ -1312,9 +1324,9 @@ static PyMethodDef TriMap_methods[] = {
1312
1324
{"dst_no_fill" , (PyCFunction )TriMap_dst_no_fill , METH_NOARGS , NULL },
1313
1325
{"map_src_no_fill" , (PyCFunction )TriMap_map_src_no_fill , METH_O , NULL },
1314
1326
{"map_dst_no_fill" , (PyCFunction )TriMap_map_dst_no_fill , METH_O , NULL },
1315
- {"map_merge_no_fill" , (PyCFunction )TriMap_map_merge_no_fill , METH_VARARGS , NULL },
1316
1327
{"map_src_fill" , (PyCFunction )TriMap_map_src_fill , METH_VARARGS , NULL },
1317
1328
{"map_dst_fill" , (PyCFunction )TriMap_map_dst_fill , METH_VARARGS , NULL },
1329
+ {"map_merge" , (PyCFunction )TriMap_map_merge , METH_VARARGS , NULL },
1318
1330
{NULL },
1319
1331
};
1320
1332
0 commit comments