I've implemented something like a CircularQueue, but not exactly.
Essentially, it has an 'append' method, which takes x,y and z
arguments, all of which are Python floats. These are added to an
internal array of type 'Point'.
When I call get(num), it pulls the 'num' most recent of these points,
puts them into a list, and returns them.
What I want to know, is there any way to do this more efficiently? It
seems wasteful to have to convert the Python floats to C floats, store
them, and then convert them back to Python floats later again. Would
my life just be easier if I kept them as Python values?
I've just been having a hard time finding the best way to move Python
lists back and forth with C, without doing full copies.
I've included a stripped down version of the code below. Thanks for
any pointers!
cdef struct Point:
float x
float y
float z
cdef class cCircularQueue:
cdef int _size, _start, _end, _iterator_length, _iterator_count, _len
cdef int _getting_pos
cdef Point *_data
def __init__(self, unsigned size):
cdef int i
self._size = size + 1
self._data = <Point *>malloc(self._size * sizeof(Point))
self._end = 0
self._iterator_count = 0
self._getting_pos = 0
for i from 0 <= i < size:
self._data[i].x = 0.0
self._data[i].y = 0.0
self._data[i].z = 0.0
def append(self, float x, float y, float z):
self.cappend(x,y,z)
cdef void cappend(self, float x, float y, float z):
self._end = (self._end + 1) % self._size
self._data[self._end].x = x
self._data[self._end].y = y
self._data[self._end].z = z
def get(self, int num):
cdef list values = []
cdef int end = self._end
cdef int check_getting_pos
......
while (self._iterator_count != num) and \
(self._getting_pos != check_getting_pos):
n = self._getting_pos
self._getting_pos = (self._getting_pos+1) % self._size
self._iterator_count += 1
values.append((self._data[n].x, self._data[n].y, self._data[n].z))
return values
Jay P.
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev