cython
1# mode: run
2# tag: cpp, werror, no-cpp-locals, cpp11
3
4from cython.operator cimport dereference as deref
5from cython.operator cimport preincrement as incr
6
7from libcpp.list cimport list as cpp_list
8
9def const_iteration_test(L):
10"""
11>>> const_iteration_test([1,2,4,8])
121
132
144
158
16"""
17l = new cpp_list[int]()
18try:
19for a in L:
20l.push_back(a)
21it = l.cbegin()
22while it != l.cend():
23a = deref(it)
24incr(it)
25print(a)
26finally:
27del l
28
29cdef list const_to_pylist(cpp_list[int]& l):
30cdef list L = []
31it = l.cbegin()
32while it != l.cend():
33L.append(deref(it))
34incr(it)
35return L
36
37def const_item_ptr_test(L, int x):
38"""
39>>> const_item_ptr_test(range(10), 100)
40[100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
41"""
42cdef cpp_list[int] l = L
43cdef int* li_ptr = &l.front()
44li_ptr[0] = x
45return const_to_pylist(l)
46