cpp_stl_forward_list.pyx 1.33 KB
Newer Older
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
# mode: run
# tag: cpp, werror, cpp11

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

from libcpp.forward_list cimport forward_list
from libcpp cimport bool as cbool


def simple_iteration_test(L):
    """
    >>> iteration_test([1,2,4,8])
    8
    4
    2
    1
    >>> iteration_test([8,4,2,1])
    1
    2
    4
    8
    """
    cdef forward_list[int] l
    for a in L:
        l.push_front(a)
    for a in l:
        print(a)

def iteration_test(L):
    """
    >>> iteration_test([1,2,4,8])
    8
    4
    2
    1
    >>> iteration_test([8,4,2,1])
    1
    2
    4
    8
    """
    l = new forward_list[int]()
    try:
        for a in L:
            l.push_front(a)
        it = l.begin()
        while it != l.end():
            a = deref(it)
            incr(it)
            print(a)
    finally:
        del l

def test_value_type(x):
    """
    >>> test_value_type(2)
    2.0
    >>> test_value_type(2.5)
    2.5
    """
    cdef forward_list[double].value_type val = x
    return val

def test_value_type_complex(x):
    """
    >>> test_value_type_complex(2)
    (2+0j)
    """
    cdef forward_list[double complex].value_type val = x
    return val


#  Tests GitHub issue #1788.
cdef cppclass MyForwardList[T](forward_list):
    pass

cdef cppclass Ints(MyForwardList[int]):
    pass