Commit 287202cc authored by Alex Huszagh's avatar Alex Huszagh

Updated deque to support random-access iterators and added "difference_type...

Updated deque to support random-access iterators and added "difference_type operator-(iterator)" for all random-access forward and reverse iterators
parent 66d2d95a
cdef extern from "<deque>" namespace "std" nogil: cdef extern from "<deque>" namespace "std" nogil:
cdef cppclass deque[T,ALLOCATOR=*]: cdef cppclass deque[T,ALLOCATOR=*]:
ctypedef T value_type
ctypedef ALLOCATOR allocator_type
# these should really be allocator_type.size_type and
# allocator_type.difference_type to be true to the C++ definition
# but cython doesn't support defered access on template arguments
ctypedef size_t size_type
ctypedef ptrdiff_t difference_type
cppclass iterator: cppclass iterator:
T& operator*() T& operator*()
iterator operator++() iterator operator++()
iterator operator--() iterator operator--()
iterator operator+(size_type)
iterator operator-(size_type)
difference_type operator-(iterator)
bint operator==(iterator) bint operator==(iterator)
bint operator!=(iterator) bint operator!=(iterator)
bint operator<(iterator)
bint operator>(iterator)
bint operator<=(iterator)
bint operator>=(iterator)
cppclass reverse_iterator: cppclass reverse_iterator:
T& operator*() T& operator*()
iterator operator++() reverse_iterator operator++()
iterator operator--() reverse_iterator operator--()
reverse_iterator operator+(size_type)
reverse_iterator operator-(size_type)
difference_type operator-(reverse_iterator)
bint operator==(reverse_iterator) bint operator==(reverse_iterator)
bint operator!=(reverse_iterator) bint operator!=(reverse_iterator)
bint operator<(reverse_iterator)
bint operator>(reverse_iterator)
bint operator<=(reverse_iterator)
bint operator>=(reverse_iterator)
cppclass const_iterator(iterator): cppclass const_iterator(iterator):
pass pass
#cppclass const_reverse_iterator(reverse_iterator): cppclass const_reverse_iterator(reverse_iterator):
# pass pass
deque() except + deque() except +
deque(deque&) except + deque(deque&) except +
deque(size_t) except + deque(size_t) except +
......
...@@ -24,10 +24,11 @@ cdef extern from "<vector>" namespace "std" nogil: ...@@ -24,10 +24,11 @@ cdef extern from "<vector>" namespace "std" nogil:
bint operator>=(iterator) bint operator>=(iterator)
cppclass reverse_iterator: cppclass reverse_iterator:
T& operator*() T& operator*()
iterator operator++() reverse_iterator operator++()
iterator operator--() reverse_iterator operator--()
iterator operator+(size_type) reverse_iterator operator+(size_type)
iterator operator-(size_type) reverse_iterator operator-(size_type)
difference_type operator-(reverse_iterator)
bint operator==(reverse_iterator) bint operator==(reverse_iterator)
bint operator!=(reverse_iterator) bint operator!=(reverse_iterator)
bint operator<(reverse_iterator) bint operator<(reverse_iterator)
......
# mode: run # mode: run
# tag: cpp, werror # tag: cpp, werror
from libcpp.deque cimport deque
from libcpp.vector cimport vector from libcpp.vector cimport vector
from cython.operator cimport dereference as deref from cython.operator cimport dereference as deref
...@@ -15,13 +16,59 @@ def test_vector(py_v): ...@@ -15,13 +16,59 @@ def test_vector(py_v):
>>> test_vector([1, 2, 3]) >>> test_vector([1, 2, 3])
[1, 2, 3] [1, 2, 3]
""" """
cdef vector[int] v = py_v cdef vector[int] vint = py_v
cdef vector[int] result cdef vector[int] result
with nogil: with nogil:
for item in v: for item in vint:
result.push_back(item) result.push_back(item)
return result return result
def test_deque_iterator_subtraction(py_v):
"""
>>> test_deque_iterator_subtraction([1, 2, 3])
3
"""
cdef deque[int] dint
for i in py_v:
dint.push_back(i)
cdef deque[int].iterator first = dint.begin()
cdef deque[int].iterator last = dint.end()
return last - first
def test_vector_iterator_subtraction(py_v):
"""
>>> test_vector_iterator_subtraction([1, 2, 3])
3
"""
cdef vector[int] vint = py_v
cdef vector[int].iterator first = vint.begin()
cdef vector[int].iterator last = vint.end()
return last - first
def test_deque_iterator_addition(py_v):
"""
>>> test_deque_iterator_addition([2, 4, 6])
6
"""
cdef deque[int] dint
for i in py_v:
dint.push_back(i)
cdef deque[int].iterator first = dint.begin()
return deref(first+2)
def test_vector_iterator_addition(py_v):
"""
>>> test_vector_iterator_addition([2, 4, 6])
6
"""
cdef vector[int] vint = py_v
cdef vector[int].iterator first = vint.begin()
return deref(first+2)
def test_ptrs(): def test_ptrs():
""" """
>>> test_ptrs() >>> test_ptrs()
......
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