Dag Sverre Seljebotn wrote:
> Dag Sverre Seljebotn wrote:
>> Robert wrote:
>>> I've put a array.pxd here:
>>> http://trac.cython.org/cython_trac/ticket/314
>>>
>>> --
>>> Cython direct interface to Python's array.array type (builtin module).
>>>
>>>      * 1D contiguous data view
>>>      * 2D views - contigious or [x,y] transposed/strided
>>>      * tools for fast array creation, maximum C-speed and handiness
>>>      * suitable as allround light weight auto-array within Cython 
>>> code too
>>>
>>>      See also: array_example.pyx and doc strings
>>> --
>>>
>>> Tests needed. just used it in 2.6 so far.
>> Wow, great! That's a lot more sophistication than I imagined in there :-)
> 
> Looking a bit more, I wonder if the view2D-functionality fits within a 
> Cython version of the pxd (it is, and needs to be by its nature, a bit 
> hackish).

yes, its just a hack to get the row info into the __getbuffer__ 
for the view.
Its just some 20 lines without speed penalty on the 1D. I added it 
more out of curiosity to check out the strided/shape thing of this 
buffer interface ;) - but the 'view' issue seemed to be somewhat 
reasonable principally.
Besides the niceness/readability of [x,y], a 2D view is brings the 
option of 2D boundscheck during debug, negative indexex, ...


> 
> Perhaps it could be reintroduced later as a wrapper class or subclass of 
> array.array.

Yes a subclass with extra storage for the 2D info would be good 
new thing beyond a local view - passing the object around with 
self-knowledge. But accepted everywhere throughout Python, where 
an array.array does.
In array.pxd its just about a local in-place view (without need 
for copy/instantiation of new objects)

I just tried this kind of scheme ...

cdef class array2(array.array):
     cdef Py_ssize_t columns
     # 0:1dim; >0:[row,column]; <0 [column,row]
     def __new__(self, char* typec, initdata=None,
                 Py_ssize_t columns = 0):
         print "hello __new__", typec
     def __init__(self, ctypec,  initdata=None,
                  Py_ssize_t columns = 0):
         print "hello __init__", typec, type(typec), columns
         self.columns=columns
     def reshape(self, Py_ssize_t columns):
         self.columns=columns
     def __getbuffer__(array2 self, Py_buffer* info, int flags):
        ...

.. and it seems already to work principally as expected with all 
1D, 2D, python compatibility... (Just the 3rd init parameter will 
never be accepted upon usage - a reshape call always necessary)

Maybe a array2.pxi - kind of fast light-weight numpy within Cython?

In 90%+ cases I just need such light-weight things as these 2 
classes; actual computations along the axes is rush anyway in 
Cython; and using the little Python memory management (through C 
calls) enables to mostly remove the heavy-handed temporary 
stack/malloc/free array stuff from Cython code.


> Another possibility long-term would be somehow adding a "view with a 
> different shape"-functionality to Cython itself. Perhaps incorporated in
> 
> http://wiki.cython.org/enhancements/buffersyntax
> 
> somehow -- perhaps
> 
> cdef int[:] arr1d = contigarray(10)
> cdef int[:,:] arr2d = <int[0:5,0:2]>arr1d # ok since 5*2==10
> 

that would make a clear 'cast' syntax out the view issue.
Yet would a dynamic column information go (readable) into a cast 
operator like this?:

cdef int[:,:] arr2d = <int[:,0:columns]>arr1d

the info would have to be passed somehow to the __getbuffer__()


-

note: the cdef extern "memset" etc stuff in array.pxd should 
perhaps move over to stdlib.pxd - as far as within ANSI C


Robert

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

Reply via email to