1# tag: cpp 2 3import cython 4 5cimport libcpp 6 7cimport libcpp.deque 8cimport libcpp.list 9cimport libcpp.map 10cimport libcpp.pair 11cimport libcpp.queue 12cimport libcpp.set 13cimport libcpp.stack 14cimport libcpp.vector 15cimport libcpp.complex 16cimport libcpp.limits 17 18from libcpp.deque cimport * 19from libcpp.list cimport * 20from libcpp.map cimport * 21from libcpp.pair cimport * 22from libcpp.queue cimport * 23from libcpp.set cimport * 24from libcpp.stack cimport * 25from libcpp.vector cimport * 26from libcpp.complex cimport * 27from libcpp.limits cimport * 28 29cdef libcpp.deque.deque[int] d1 = deque[int]() 30cdef libcpp.list.list[int] l1 = list[int]() 31cdef libcpp.map.map[int,int] m1 = map[int,int]() 32cdef libcpp.pair.pair[int,int] p1 = pair[int,int](1,2) 33cdef libcpp.queue.queue[int] q1 = queue[int]() 34cdef libcpp.set.set[int] s1 = set[int]() 35cdef libcpp.stack.stack[int] t1 = stack[int]() 36cdef libcpp.vector.vector[int] v1 = vector[int]() 37 38cdef deque[int].iterator id1 = d1.begin() 39cdef deque[int].iterator id2 = d1.end() 40cdef deque[int].reverse_iterator rid1 = d1.rbegin() 41cdef deque[int].reverse_iterator rid2 = d1.rend() 42 43cdef list[int].iterator il1 = l1.begin() 44cdef list[int].iterator il2 = l1.end() 45cdef list[int].reverse_iterator ril1 = l1.rbegin() 46cdef list[int].reverse_iterator ril2 = l1.rend() 47 48cdef map[int,int].iterator im1 = m1.begin() 49cdef map[int,int].iterator im2 = m1.end() 50cdef map[int,int].reverse_iterator rim1 = m1.rbegin() 51cdef map[int,int].reverse_iterator rim2 = m1.rend() 52cdef pair[map[int,int].iterator, bint] pimb = m1.insert(p1) 53 54cdef set[int].iterator is1 = s1.begin() 55cdef set[int].iterator is2 = s1.end() 56cdef set[int].reverse_iterator ris1 = s1.rbegin() 57cdef set[int].reverse_iterator ris2 = s1.rend() 58cdef pair[set[int].iterator, bint] pisb = s1.insert(4) 59 60cdef vector[int].iterator iv1 = v1.begin() 61cdef vector[int].iterator iv2 = v1.end() 62cdef vector[int].reverse_iterator riv1 = v1.rbegin() 63cdef vector[int].reverse_iterator riv2 = v1.rend() 64 65def test_vector_coercion(*args): 66 """ 67 >>> test_vector_coercion(1.75) 68 [1.75] 69 >>> test_vector_coercion(1, 10, 100) 70 [1.0, 10.0, 100.0] 71 """ 72 v = new vector[double]() 73 for a in args: 74 v.push_back(a) 75 return [v[0][i] for i in range(v.size())] 76 77def test_const_vector(*args): 78 """ 79 >>> test_const_vector(1.75) 80 [1.75] 81 >>> test_const_vector(1, 10, 100) 82 [1.0, 10.0, 100.0] 83 """ 84 cdef vector[double] v 85 for a in args: 86 v.push_back(a) 87 return const_vector_to_list(v) 88 89cdef const_vector_to_list(const vector[double]& cv): 90 cdef vector[double].const_iterator iter = cv.const_begin() 91 cdef lst = [] 92 while iter != cv.const_end(): 93 lst.append(cython.operator.dereference(iter)) 94 cython.operator.preincrement(iter) 95 return lst 96 97 98cdef double dmax = numeric_limits[double].max() 99cdef double dmin = numeric_limits[double].min() 100cdef double deps = numeric_limits[double].epsilon() 101cdef double dqnan = numeric_limits[double].quiet_NaN() 102cdef double dsnan = numeric_limits[double].signaling_NaN() 103cdef double dinf = numeric_limits[double].infinity() 104 105cdef int imax = numeric_limits[int].max() 106cdef int imin = numeric_limits[int].min() 107cdef int ieps = numeric_limits[int].epsilon() 108cdef int iqnan = numeric_limits[int].quiet_NaN() 109cdef int isnan = numeric_limits[int].signaling_NaN() 110cdef int iinf = numeric_limits[int].infinity() 111 112#API checks for containers with std::allocator declared 113from libcpp.memory cimport allocator 114 115cdef libcpp.vector.vector[int,allocator[int]] vec_alloc_int = libcpp.vector.vector[int,allocator[int]](10,1) 116assert vec_alloc_int.size() == 10 117 118cdef libcpp.list.list[int,allocator[int]] list_alloc_int = libcpp.list.list[int,allocator[int]](10,1) 119assert list_alloc_int.size() == 10 120 121##Something about the default params breaks the auto-conversion... 122def convert_to_vector(I): 123 """ 124 >>> convert_to_vector([1,2,3,4]) 125 """ 126 cdef vector[int] x = I 127 128 129def complex_operators(): 130 """ 131 >>> complex_operators() 132 [-1.0, 0.0, 0.0, 2.0, 0.0, 2.0] 133 """ 134 cdef libcpp.complex.complex[double] a = libcpp.complex.complex[double](0.0,1.0) 135 cdef libcpp.complex.complex[double] r1=a*a 136 cdef libcpp.complex.complex[double] r2=a*2.0 137 cdef libcpp.complex.complex[double] r3=2.0*a 138 return [r1.real(), r1.imag(), r2.real(), r2.imag(), r3.real(), r3.imag()] 139 140def pair_comparison(): 141 """ 142 >>> pair_comparison() 143 [False, True, False, True, False] 144 """ 145 cdef pair[double, double] p1 = pair[double, double](1.0,2.0) 146 cdef pair[double, double] p2 = pair[double, double](2.0,2.0) 147 return [p1==p2,p1==p1,p1>p2,p1<p2,p2>p2] 148