Is there a way to make a dereference before assignment in Cython, in order to do e.g. inplace assignment of values in a C++ list?
Cython documentation (here) says that you should use dereference(foo) for producing C++ code like *(foo), but this syntax cannot be used for assignment (e.g. *(foo) = *(foo) + 1.
Below I show an example of assignment (in the inplace_normalize function) that I couldn't implement using the Cython dereference function.
from libcpp.list cimport list as cpplist
from cython.operator import dereference as deref, preincrement as inc
cpdef cpplist[double] normalize(cpplist[double] l):
cdef double norm
cdef cpplist[double] l2
for v in l:
norm += v
for v in l:
l2.push_back(v/norm)
return l2
# inplace version of normalize
cpdef void inplace_normalize(cpplist[double] l):
cdef double norm
for v in l:
norm += v
cdef cpplist[double].iterator it = l.begin()
while it != l.end():
# deref(it) = deref(it)/norm #== *it = *it / norm
print(deref(it)/norm)
inc(it)
If I try to use deref(it) = deref(it)/norm, I get the error Cannot assign to or delete this.
What is the solution for the inplace assigment of values in a libcpp.list using Cython?