File: cpp_stl_list_cpp11.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (45 lines) | stat: -rw-r--r-- 955 bytes parent folder | download
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
# mode: run
# tag: cpp, werror, no-cpp-locals, cpp11

from cython.operator cimport dereference as deref
from cython.operator cimport preincrement as incr

from libcpp.list cimport list as cpp_list

def const_iteration_test(L):
    """
    >>> const_iteration_test([1,2,4,8])
    1
    2
    4
    8
    """
    l = new cpp_list[int]()
    try:
        for a in L:
            l.push_back(a)
        it = l.cbegin()
        while it != l.cend():
            a = deref(it)
            incr(it)
            print(a)
    finally:
        del l

cdef list const_to_pylist(cpp_list[int]& l):
    cdef list L = []
    it = l.cbegin()
    while it != l.cend():
        L.append(deref(it))
        incr(it)
    return L

def const_item_ptr_test(L, int x):
    """
    >>> const_item_ptr_test(range(10), 100)
    [100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    """
    cdef cpp_list[int] l = L
    cdef int* li_ptr = &l.front()
    li_ptr[0] = x
    return const_to_pylist(l)