1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# mode: run
# tag: cpp, werror
from libcpp.deque cimport deque
from libcpp.vector cimport vector
from cython.operator cimport dereference as deref
cdef extern from "cpp_iterators_simple.h":
cdef cppclass DoublePointerIter:
DoublePointerIter(double* start, int len)
double* begin()
double* end()
def test_vector(py_v):
"""
>>> test_vector([1, 2, 3])
[1, 2, 3]
"""
cdef vector[int] vint = py_v
cdef vector[int] result
with nogil:
for item in vint:
result.push_back(item)
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():
"""
>>> test_ptrs()
[1.0, 2.0, 3.0]
"""
cdef double a = 1
cdef double b = 2
cdef double c = 3
cdef vector[double*] v
v.push_back(&a)
v.push_back(&b)
v.push_back(&c)
return [item[0] for item in v]
def test_custom():
"""
>>> test_custom()
[1.0, 2.0, 3.0]
"""
cdef double* values = [1, 2, 3]
cdef DoublePointerIter* iter
try:
iter = new DoublePointerIter(values, 3)
# TODO: It'd be nice to automatically dereference this in a way that
# would not conflict with the pointer slicing iteration.
return [x for x in iter[0]]
finally:
del iter
def test_iteration_over_heap_vector(L):
"""
>>> test_iteration_over_heap_vector([1,2])
[1, 2]
"""
cdef int i
cdef vector[int] *vint = new vector[int]()
try:
for i in L:
vint.push_back(i)
return [ i for i in deref(vint) ]
finally:
del vint
def test_iteration_in_generator(vector[int] vint):
"""
>>> list( test_iteration_in_generator([1,2]) )
[1, 2]
"""
for i in vint:
yield i
def test_iteration_in_generator_reassigned():
"""
>>> list( test_iteration_in_generator_reassigned() )
[1]
"""
cdef vector[int] *vint = new vector[int]()
cdef vector[int] *orig_vint = vint
vint.push_back(1)
reassign = True
try:
for i in deref(vint):
yield i
if reassign:
reassign = False
vint = new vector[int]()
vint.push_back(2)
finally:
if vint is not orig_vint:
del vint
del orig_vint
|