Skip to content

Commit 57ece6e

Browse files
committed
completed preliminary TriMap_map_merge
1 parent 33f3acb commit 57ece6e

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/tri_map.c

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -765,15 +765,16 @@ static inline int
765765
AK_TM_transfer_object(TriMapObject* tm,
766766
bool from_src,
767767
PyArrayObject* array_from,
768-
PyArrayObject* array_to
768+
PyArrayObject* array_to,
769+
bool clear_target
769770
) {
770771
Py_ssize_t one_count = from_src ? tm->src_one_count : tm->dst_one_count;
771772
TriMapOne* one_pairs = from_src ? tm->src_one : tm->dst_one;
772773

773774
// NOTE: could use PyArray_Scalar instead of PyArray_GETITEM if we wanted to store scalars instead of Python objects; however, that is pretty uncommon for object arrays to store PyArray_Scalars
774775
bool f_is_obj = PyArray_TYPE(array_from) == NPY_OBJECT;
775776

776-
// the passed in object array is assumed to be contiguous and have NULL (not None) in each position
777+
// the passed in object array is contiguous and have NULL (not None) in each position
777778
PyObject** array_to_data = (PyObject**)PyArray_DATA(array_to);
778779
PyObject* pyo;
779780
void* f;
@@ -788,6 +789,9 @@ AK_TM_transfer_object(TriMapObject* tm,
788789
else { // will convert any value to an object
789790
pyo = PyArray_GETITEM(array_from, f);
790791
}
792+
if (clear_target) {
793+
Py_XDECREF(array_to_data[o->to]);
794+
}
791795
array_to_data[o->to] = pyo;
792796
}
793797
PyObject** t;
@@ -810,9 +814,12 @@ AK_TM_transfer_object(TriMapObject* tm,
810814
}
811815
while (t < t_end) {
812816
Py_INCREF(pyo); // one more than we need
817+
if (clear_target) {
818+
Py_XDECREF(*t);
819+
}
813820
*t++ = pyo;
814821
}
815-
Py_DECREF(pyo); // remove the extra one
822+
Py_DECREF(pyo); // remove the extra ref
816823
}
817824
else { // from_dst, dst is an array
818825
dst_pos = 0;
@@ -827,6 +834,9 @@ AK_TM_transfer_object(TriMapObject* tm,
827834
else {
828835
pyo = PyArray_GETITEM(array_from, f);
829836
}
837+
if (clear_target) {
838+
Py_XDECREF(*t);
839+
}
830840
*t++ = pyo;
831841
dst_pos++;
832842
}
@@ -998,7 +1008,7 @@ AK_TM_map_no_fill(TriMapObject* tm,
9981008
}
9991009
// transfer values
10001010
if (dtype_is_obj) {
1001-
if (AK_TM_transfer_object(tm, from_src, array_from, array_to)) {
1011+
if (AK_TM_transfer_object(tm, from_src, array_from, array_to, false)) {
10021012
Py_DECREF((PyObject*)array_to);
10031013
return NULL;
10041014
}
@@ -1123,10 +1133,16 @@ TriMap_map_merge(TriMapObject *tm, PyObject *args)
11231133
bool transfer_from_dst = PyArray_SIZE((PyArrayObject*)tm->final_src_fill) != 0;
11241134

11251135
if (dtype_is_obj) {
1126-
if (AK_TM_transfer_object(tm, true, array_src, array_to)) {
1136+
if (AK_TM_transfer_object(tm, true, array_src, array_to, false)) {
11271137
Py_DECREF((PyObject*)array_to);
11281138
return NULL;
11291139
}
1140+
if (transfer_from_dst) {
1141+
if (AK_TM_transfer_object(tm, false, array_dst, array_to, true)) {
1142+
Py_DECREF((PyObject*)array_to);
1143+
return NULL;
1144+
}
1145+
}
11301146
}
11311147
else if (dtype_is_unicode) {
11321148
AK_TM_TRANSFER_FLEXIBLE(Py_UCS4, true, array_src, array_to);
@@ -1152,34 +1168,9 @@ TriMap_map_merge(TriMapObject *tm, PyObject *args)
11521168
}
11531169
}
11541170
}
1155-
11561171
return (PyObject*)array_to;
1157-
1158-
// // transfer values
1159-
// if (dtype_is_obj) {
1160-
// if (AK_TM_transfer_object(tm, from_src, array_from, array_to)) {
1161-
// Py_DECREF((PyObject*)array_to);
1162-
// return NULL;
1163-
// }
1164-
// }
1165-
// else if (dtype_is_unicode) {
1166-
// AK_TM_TRANSFER_FLEXIBLE(Py_UCS4);
1167-
// }
1168-
// else if (dtype_is_string) {
1169-
// AK_TM_TRANSFER_FLEXIBLE(char);
1170-
// }
1171-
// else {
1172-
// if (AK_TM_transfer_scalar(tm, from_src, array_from, array_to)) {
1173-
// Py_DECREF((PyObject*)array_to);
1174-
// return NULL;
1175-
// }
1176-
// }
1177-
// PyArray_CLEARFLAGS(array_to, NPY_ARRAY_WRITEABLE);
1178-
// return (PyObject*)array_to;
11791172
}
11801173

1181-
1182-
11831174
// Returns NULL on error.
11841175
static inline PyObject *
11851176
AK_TM_map_fill(TriMapObject* tm,
@@ -1232,7 +1223,7 @@ AK_TM_map_fill(TriMapObject* tm,
12321223
}
12331224
// array_from, array_to inc refed and dec refed on error
12341225
if (dtype_is_obj) {
1235-
if (AK_TM_transfer_object(tm, from_src, array_from, array_to)) {
1226+
if (AK_TM_transfer_object(tm, from_src, array_from, array_to, false)) {
12361227
goto error;
12371228
}
12381229
if (AK_TM_fill_object(tm, from_src, array_to, fill_value)) {

0 commit comments

Comments
 (0)