cython

Форк
0
/
cpp_stl_list_cpp11.pyx 
45 строк · 955.0 Байт
1
# mode: run
2
# tag: cpp, werror, no-cpp-locals, cpp11
3

4
from cython.operator cimport dereference as deref
5
from cython.operator cimport preincrement as incr
6

7
from libcpp.list cimport list as cpp_list
8

9
def const_iteration_test(L):
10
    """
11
    >>> const_iteration_test([1,2,4,8])
12
    1
13
    2
14
    4
15
    8
16
    """
17
    l = new cpp_list[int]()
18
    try:
19
        for a in L:
20
            l.push_back(a)
21
        it = l.cbegin()
22
        while it != l.cend():
23
            a = deref(it)
24
            incr(it)
25
            print(a)
26
    finally:
27
        del l
28

29
cdef list const_to_pylist(cpp_list[int]& l):
30
    cdef list L = []
31
    it = l.cbegin()
32
    while it != l.cend():
33
        L.append(deref(it))
34
        incr(it)
35
    return L
36

37
def 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
    """
42
    cdef cpp_list[int] l = L
43
    cdef int* li_ptr = &l.front()
44
    li_ptr[0] = x
45
    return const_to_pylist(l)
46

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.