cython

Форк
0
/
cpp_stl_algo_minmax_ops.pyx 
143 строки · 3.5 Кб
1
# mode: run
2
# tag: cpp, werror, cpp17, cppexecpolicies
3

4
from cython.operator cimport dereference as deref
5

6
from libcpp cimport bool
7
from libcpp.algorithm cimport (min_element, max_element, minmax, minmax_element, 
8
                               clamp)
9
from libcpp.vector cimport vector
10
from libcpp.pair cimport pair
11
from libcpp.execution cimport seq
12

13

14
cdef bool less(int a, int b):
15
    return a < b
16

17
def test_min_element(vector[int] v):
18
    """
19
    Test min_element.
20

21
    >>> test_min_element([0, 1, 2, 3, 4, 5])
22
    0
23
    """
24
    cdef vector[int].iterator it = min_element(v.begin(), v.end())
25
    return deref(it)
26

27
def test_min_element_with_pred(vector[int] v):
28
    """
29
    Test min_element with binary predicate.
30

31
    >>> test_min_element_with_pred([0, 1, 2, 3, 4, 5])
32
    0
33
    """
34
    cdef vector[int].iterator it = min_element(v.begin(), v.end(), less)
35
    return deref(it)
36

37
def test_min_element_with_exec(vector[int] v):
38
    """
39
    Test min_element with execution policy.
40

41
    >>> test_min_element_with_exec([0, 1, 2, 3, 4, 5])
42
    0
43
    """
44
    cdef vector[int].iterator it = min_element(seq, v.begin(), v.end())
45
    return deref(it)
46

47
def test_max_element(vector[int] v):
48
    """
49
    Test max_element.
50

51
    >>> test_max_element([0, 1, 2, 3, 4, 5])
52
    5
53
    """
54
    cdef vector[int].iterator it = max_element(v.begin(), v.end())
55
    return deref(it)
56

57
def test_max_element_with_pred(vector[int] v):
58
    """
59
    Test max_element with binary predicate.
60

61
    >>> test_max_element_with_pred([0, 1, 2, 3, 4, 5])
62
    5
63
    """
64
    cdef vector[int].iterator it = max_element(v.begin(), v.end(), less)
65
    return deref(it)
66

67
def test_max_element_with_exec(vector[int] v):
68
    """
69
    Test max_element with execution policy.
70

71
    >>> test_max_element_with_exec([0, 1, 2, 3, 4, 5])
72
    5
73
    """
74
    cdef vector[int].iterator it = max_element(seq, v.begin(), v.end())
75
    return deref(it)
76

77
def test_minmax(int a, int b):
78
    """
79
    Test minmax.
80

81
    >>> test_minmax(10, 20)
82
    [10, 20]
83
    """
84
    cdef pair[int, int] p = minmax(a, b)
85
    return [p.first, p.second]
86

87
def test_minmax_with_pred(int a, int b):
88
    """
89
    Test minmax with binary predicate.
90

91
    >>> test_minmax_with_pred(10, 20)
92
    [10, 20]
93
    """
94
    cdef pair[int, int] p = minmax(a, b, less)
95
    return [p.first, p.second]
96

97
def test_minmax_element(vector[int] v):
98
    """
99
    Test minmax_element.
100

101
    >>> test_minmax_element([0, 1, 2, 3, 4, 5])
102
    [0, 5]
103
    """
104
    cdef pair[vector[int].iterator, vector[int].iterator] p = minmax_element(v.begin(), v.end())
105
    return [deref(p.first), deref(p.second)]
106

107
def test_minmax_element_with_pred(vector[int] v):
108
    """
109
    Test minmax_element with binary predicate.
110

111
    >>> test_minmax_element_with_pred([0, 1, 2, 3, 4, 5])
112
    [0, 5]
113
    """
114
    cdef pair[vector[int].iterator, vector[int].iterator] p = minmax_element(v.begin(), v.end(), less)
115
    return [deref(p.first), deref(p.second)]
116

117
def test_minmax_element_with_exec(vector[int] v):
118
    """
119
    Test minmax_element with execution policy.
120

121
    >>> test_minmax_element_with_exec([0, 1, 2, 3, 4, 5])
122
    [0, 5]
123
    """
124
    cdef pair[vector[int].iterator, vector[int].iterator] p = minmax_element(seq, v.begin(), v.end())
125
    return [deref(p.first), deref(p.second)]
126

127
def test_clamp(int v, int lo, int hi):
128
    """
129
    Test clamp.
130

131
    >>> test_clamp(-129, -128, 255)
132
    -128
133
    """
134
    return clamp(v, lo, hi)
135

136
def test_clamp_with_pred(int v, int lo, int hi):
137
    """
138
    Test clamp with binary predicate
139

140
    >>> test_clamp_with_pred(-129, -128, 255)
141
    -128
142
    """
143
    return clamp(v, lo, hi, less)

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

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

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

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