Commit d5e107a9 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Refactoring of the extend function to make it simpler.

parent b9f7a142
...@@ -38,11 +38,9 @@ cdef class Queue: ...@@ -38,11 +38,9 @@ cdef class Queue:
self.append(value) self.append(value)
cdef extend_ints(self, int* values, size_t count): cdef extend_ints(self, int* values, size_t count):
cdef size_t i cdef int value
for i in range(count): for value in values[:count]: # It is possible to slice pointers in Cython.
if not cqueue.queue_push_tail( self.append(value)
self._c_queue, <void*> values[i]):
raise MemoryError()
cpdef int peek(self) except? -1: cpdef int peek(self) except? -1:
cdef int value = <Py_ssize_t> cqueue.queue_peek_head(self._c_queue) cdef int value = <Py_ssize_t> cqueue.queue_peek_head(self._c_queue)
......
...@@ -342,11 +342,9 @@ Adding an ``extend()`` method should now be straight forward:: ...@@ -342,11 +342,9 @@ Adding an ``extend()`` method should now be straight forward::
cdef extend(self, int* values, size_t count): cdef extend(self, int* values, size_t count):
"""Append all ints to the queue. """Append all ints to the queue.
""" """
cdef size_t i cdef int value
for i in range(count): for value in values[:count]: # It is possible to slice pointers in Cython.
if not cqueue.queue_push_tail( self.append(value)
self._c_queue, <void*>values[i]):
raise MemoryError()
This becomes handy when reading values from a C array, for example. This becomes handy when reading values from a C array, for example.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment