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
|
# mode: run
# tag: cpp, werror
cdef extern from "vector" namespace "std":
cdef cppclass vector[T]:
T at(int)
void push_back(T t)
void assign(int, T)
void clear()
int size()
cppclass iterator:
T operator*()
iterator operator++()
bint operator==(iterator)
bint operator!=(iterator)
iterator end()
iterator begin()
from cython.operator cimport dereference as deref, preincrement as inc
def test_vector(L):
"""
>>> test_vector([1,10,100])
1
10
100
"""
v = new vector[int]()
for a in L:
v.push_back(a)
cdef int i
for i in range(len(L)):
print v.at(i)
del v
ctypedef int my_int
def test_vector_typedef(L):
"""
>>> test_vector_typedef([1, 2, 3])
[1, 2, 3]
"""
cdef vector[my_int] v = L
cdef vector[int] vv = v
return vv
def test_vector_iterator(L):
"""
>>> test_vector([11, 37, 389, 5077])
11
37
389
5077
"""
v = new vector[int]()
for a in L:
v.push_back(a)
cdef vector[int].iterator iter = v.begin()
while iter != v.end():
print deref(iter)
inc(iter)
del v
cdef class VectorWrapper:
"""
>>> VectorWrapper(1, .5, .25, .125)
[1.0, 0.5, 0.25, 0.125]
"""
cdef vector[double] vector
def __init__(self, *args):
self.vector = args
def __repr__(self):
return repr(self.vector)
|