cython

Форк
0
/
libcpp_all.pyx 
150 строк · 4.6 Кб
1
# tag: cpp, no-cpp-locals
2

3
import cython
4

5
cimport libcpp
6

7
# cimport libcpp.atomic
8
cimport libcpp.deque
9
cimport libcpp.list
10
cimport libcpp.map
11
cimport libcpp.pair
12
cimport libcpp.queue
13
cimport libcpp.set
14
cimport libcpp.stack
15
cimport libcpp.vector
16
cimport libcpp.complex
17
cimport libcpp.limits
18

19
# from libcpp.atomic cimport *
20
from libcpp.deque  cimport *
21
from libcpp.list   cimport *
22
from libcpp.map    cimport *
23
from libcpp.pair   cimport *
24
from libcpp.queue  cimport *
25
from libcpp.set    cimport *
26
from libcpp.stack  cimport *
27
from libcpp.vector cimport *
28
from libcpp.complex cimport *
29
from libcpp.limits cimport *
30

31
# cdef libcpp.atomic.atomc[int]  a1 = atomic[int]()
32
cdef libcpp.deque.deque[int]   d1 = deque[int]()
33
cdef libcpp.list.list[int]     l1 = list[int]()
34
cdef libcpp.map.map[int,int]   m1 = map[int,int]()
35
cdef libcpp.pair.pair[int,int] p1 = pair[int,int](1,2)
36
cdef libcpp.queue.queue[int]   q1 = queue[int]()
37
cdef libcpp.set.set[int]       s1 = set[int]()
38
cdef libcpp.stack.stack[int]   t1 = stack[int]()
39
cdef libcpp.vector.vector[int] v1 = vector[int]()
40

41
cdef deque[int].iterator id1 = d1.begin()
42
cdef deque[int].iterator id2 = d1.end()
43
cdef deque[int].reverse_iterator rid1 = d1.rbegin()
44
cdef deque[int].reverse_iterator rid2 = d1.rend()
45

46
cdef list[int].iterator il1 = l1.begin()
47
cdef list[int].iterator il2 = l1.end()
48
cdef list[int].reverse_iterator ril1 = l1.rbegin()
49
cdef list[int].reverse_iterator ril2 = l1.rend()
50

51
cdef map[int,int].iterator im1 = m1.begin()
52
cdef map[int,int].iterator im2 = m1.end()
53
cdef map[int,int].reverse_iterator rim1 = m1.rbegin()
54
cdef map[int,int].reverse_iterator rim2 = m1.rend()
55
cdef pair[map[int,int].iterator, bint] pimb = m1.insert(p1)
56

57
cdef set[int].iterator is1 = s1.begin()
58
cdef set[int].iterator is2 = s1.end()
59
cdef set[int].reverse_iterator ris1 = s1.rbegin()
60
cdef set[int].reverse_iterator ris2 = s1.rend()
61
cdef pair[set[int].iterator, bint] pisb = s1.insert(4)
62

63
cdef vector[int].iterator iv1 = v1.begin()
64
cdef vector[int].iterator iv2 = v1.end()
65
cdef vector[int].reverse_iterator riv1 = v1.rbegin()
66
cdef vector[int].reverse_iterator riv2 = v1.rend()
67

68
def test_vector_coercion(*args):
69
    """
70
    >>> test_vector_coercion(1.75)
71
    [1.75]
72
    >>> test_vector_coercion(1, 10, 100)
73
    [1.0, 10.0, 100.0]
74
    """
75
    v = new vector[double]()
76
    for a in args:
77
        v.push_back(a)
78
    return [v[0][i] for i in range(v.size())]
79

80
def test_const_vector(*args):
81
    """
82
    >>> test_const_vector(1.75)
83
    [1.75]
84
    >>> test_const_vector(1, 10, 100)
85
    [1.0, 10.0, 100.0]
86
    """
87
    cdef vector[double] v
88
    for a in args:
89
        v.push_back(a)
90
    return const_vector_to_list(v)
91

92
cdef const_vector_to_list(const vector[double]& cv):
93
    cdef vector[double].const_iterator iter = cv.const_begin()
94
    cdef lst = []
95
    while iter != cv.const_end():
96
        lst.append(cython.operator.dereference(iter))
97
        cython.operator.preincrement(iter)
98
    return lst
99

100

101
cdef double dmax = numeric_limits[double].max()
102
cdef double dmin = numeric_limits[double].min()
103
cdef double deps = numeric_limits[double].epsilon()
104
cdef double dqnan = numeric_limits[double].quiet_NaN()
105
cdef double dsnan = numeric_limits[double].signaling_NaN()
106
cdef double dinf = numeric_limits[double].infinity()
107

108
cdef int imax = numeric_limits[int].max()
109
cdef int imin = numeric_limits[int].min()
110
cdef int ieps = numeric_limits[int].epsilon()
111
cdef int iqnan = numeric_limits[int].quiet_NaN()
112
cdef int isnan = numeric_limits[int].signaling_NaN()
113
cdef int iinf = numeric_limits[int].infinity()
114

115
#API checks for containers with std::allocator declared
116
from libcpp.memory cimport allocator
117

118
cdef libcpp.vector.vector[int,allocator[int]] vec_alloc_int = libcpp.vector.vector[int,allocator[int]](10,1)
119
assert vec_alloc_int.size() == 10
120

121
cdef libcpp.list.list[int,allocator[int]] list_alloc_int = libcpp.list.list[int,allocator[int]](10,1)
122
assert list_alloc_int.size() == 10
123

124
##Something about the default params breaks the auto-conversion...
125
def convert_to_vector(I):
126
    """
127
    >>> convert_to_vector([1,2,3,4])
128
    """
129
    cdef vector[int] x = I
130

131

132
def complex_operators():
133
    """
134
    >>> complex_operators()
135
    [-1.0, 0.0, 0.0, 2.0, 0.0, 2.0]
136
    """
137
    cdef libcpp.complex.complex[double] a = libcpp.complex.complex[double](0.0,1.0)
138
    cdef libcpp.complex.complex[double] r1=a*a
139
    cdef libcpp.complex.complex[double] r2=a*2.0
140
    cdef libcpp.complex.complex[double] r3=2.0*a
141
    return [r1.real(), r1.imag(), r2.real(), r2.imag(), r3.real(), r3.imag()]
142

143
def pair_comparison():
144
    """
145
    >>> pair_comparison()
146
    [False, True, False, True, False]
147
    """
148
    cdef pair[double, double] p1 = pair[double, double](1.0,2.0)
149
    cdef pair[double, double] p2 = pair[double, double](2.0,2.0)
150
    return [p1==p2,p1==p1,p1>p2,p1<p2,p2>p2]
151

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

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

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

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