cython

Форк
0
/
fused_cpp.pyx 
90 строк · 2.0 Кб
1
# tag: cpp
2

3
cimport cython
4
from libcpp.vector cimport vector
5
from libcpp.map cimport map
6
from libcpp.typeinfo cimport type_info
7
from cython.operator cimport typeid
8

9
def test_cpp_specialization(cython.floating element):
10
    """
11
    >>> import cython
12
    >>> test_cpp_specialization[cython.float](10.0)
13
    vector[float] * float 10.0
14
    >>> test_cpp_specialization[cython.double](10.0)
15
    vector[double] * double 10.0
16
    """
17
    cdef vector[cython.floating] *v = new vector[cython.floating]()
18
    v.push_back(element)
19
    print cython.typeof(v), cython.typeof(element), v.at(0)
20

21
cdef fused C:
22
   int
23
   object
24

25
cdef const type_info* tidint = &typeid(int)
26
def typeid_call(C x):
27
    """
28
    For GH issue 3203
29
    >>> typeid_call(1)
30
    True
31
    """
32
    cdef const type_info* a = &typeid(C)
33
    return a[0] == tidint[0]
34

35
cimport cython
36

37
def typeid_call2(cython.integral x):
38
    """
39
    For GH issue 3203
40
    >>> typeid_call2[int](1)
41
    True
42
    """
43
    cdef const type_info* a = &typeid(cython.integral)
44
    return a[0] == tidint[0]
45

46
cdef fused_ref(cython.integral& x):
47
    return x*2
48

49
def test_fused_ref(int x):
50
    """
51
    >>> test_fused_ref(5)
52
    (10, 10)
53
    """
54
    return fused_ref(x), fused_ref[int](x)
55

56
ctypedef fused nested_fused:
57
    vector[cython.integral]
58

59
cdef vec_of_fused(nested_fused v):
60
    x = v[0]
61
    return cython.typeof(x)
62

63
def test_nested_fused():
64
    """
65
    >>> test_nested_fused()
66
    int
67
    long
68
    """
69
    cdef vector[int] vi = [0,1]
70
    cdef vector[long] vl = [0,1]
71
    print vec_of_fused(vi)
72
    print vec_of_fused(vl)
73

74
ctypedef fused nested_fused2:
75
    map[cython.integral, cython.floating]
76

77
cdef map_of_fused(nested_fused2 m):
78
    for pair in m:
79
        return cython.typeof(pair.first), cython.typeof(pair.second)
80

81
def test_nested_fused2():
82
    """
83
    >>> test_nested_fused2()
84
    ('int', 'float')
85
    ('long', 'double')
86
    """
87
    cdef map[int, float] mif = { 0: 0.0 }
88
    cdef map[long, double] mld = { 0: 0.0 }
89
    print map_of_fused(mif)
90
    print map_of_fused(mld)
91

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

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

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

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