On 12/2/10 3:08 AM, Dag Sverre Seljebotn wrote:

> Just in case anybody is wondering what something like this could look
> like, here's a rough scetch complete with bugs. The idea would be to a)
> add some rudimentary support for using the yield keyword in Cython to
> make a generator function, b) inline the generator function if the
> generator is used directly in a for-loop. This should result in very
> efficient code, and would also be much easier to implement than a
> general purpose generator.

I expect that being able to inline a generator would be quite a trick. I would 
go so far as to say that it is a completely different feature from adding 
generators to Cython in general. It's tantamount to adding (hygienic?) macros 
to 
Cython.

I like it.

> @cython.inline
> cdef array_iter_double(np.ndarray a, int axis=-1):
>       cdef np.flatiter it
>       ita = np.PyArray_IterAllButAxis(a,&axis)
>       cdef Py_ssize_t stride = a.strides[axis], length = a.shape[axis], i
>       while np.PyArray_ITER_NOTDONE(ita):
>           for i in range(length):
>               yield<double*>(np.PyArray_ITER_DATA(it) + )[i * stride])[0]
>               # TODO: Probably yield indices as well
>           np.PyArray_ITER_NEXT(it)
>       # TODO: add faster special-cases for stride == sizeof(double)
>
>
> # Use NumPy iterator API to sum all values of array with
> # arbitrary number of dimensions:
> cdef double s = 0, value
> for value in array_iter_double(myarray):
>       s += value
>       # at this point, the contents of the array_iter_double function is
> copied,
>       # and "s += value" simply inserted everywhere "yield" occurs in the
> function
>
> Dag Sverre
>


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to