1 from libc.stdlib cimport malloc, free
2 from cpython.object cimport Py_EQ, Py_NE
4 def double_ptr_slice(x, L, int a, int b):
6 >>> L = list(range(10))
7 >>> double_ptr_slice(5, L, 0, 10)
8 >>> double_ptr_slice(6, L, 0, 10)
9 >>> double_ptr_slice(None, L, 0, 10)
10 >>> double_ptr_slice(0, L, 3, 7)
11 >>> double_ptr_slice(5, L, 3, 7)
12 >>> double_ptr_slice(9, L, 3, 7)
14 >>> double_ptr_slice(EqualsEvens(), L, 0, 10)
15 >>> double_ptr_slice(EqualsEvens(), L, 1, 10)
17 cdef double *L_c = NULL
19 L_c = <double*>malloc(len(L) * sizeof(double))
20 for i, a in enumerate(L):
22 assert (x in L_c[:b]) == (x in L[:b])
23 assert (x in L_c[a:b]) == (x in L[a:b])
24 assert (x in L_c[a:b:2]) == (x in L[a:b:2])
28 def void_ptr_slice(py_x, L, int a, int b):
30 >>> L = list(range(10))
31 >>> void_ptr_slice(5, L, 0, 10)
32 >>> void_ptr_slice(6, L, 0, 10)
33 >>> void_ptr_slice(None, L, 0, 10)
34 >>> void_ptr_slice(0, L, 3, 7)
35 >>> void_ptr_slice(5, L, 3, 7)
36 >>> void_ptr_slice(9, L, 3, 7)
38 # I'm using the fact that small Python ints are cached.
39 cdef void **L_c = NULL
40 cdef void *x = <void*>py_x
42 L_c = <void**>malloc(len(L) * sizeof(void*))
43 for i, a in enumerate(L):
45 assert (x in L_c[:b]) == (py_x in L[:b])
46 assert (x in L_c[a:b]) == (py_x in L[a:b])
47 # assert (x in L_c[a:b:2]) == (py_x in L[a:b:2])
51 cdef class EqualsEvens:
58 >>> [e == k for k in range(4)]
59 [True, False, True, False]
61 def __richcmp__(self, other, int op):