cython

Форк
0
/
cpp_stl_algo_modifying_sequence_ops.pyx 
436 строк · 10.1 Кб
1
# mode: run
2
# tag: cpp, werror, cpp11, no-cpp-locals
3

4
from __future__ import print_function
5

6
from cython.operator cimport dereference as deref
7
from cython.operator cimport preincrement, postincrement
8
from libcpp cimport bool
9
from libcpp.algorithm cimport copy, copy_if, copy_n, copy_backward, move, move_backward, fill, fill_n, transform
10
from libcpp.algorithm cimport generate, generate_n, remove, remove_if, remove_copy, remove_copy_if, replace, replace_if
11
from libcpp.algorithm cimport replace_copy, replace_copy_if, swap, swap_ranges, iter_swap, reverse, reverse_copy
12
from libcpp.algorithm cimport rotate, rotate_copy, unique, unique_copy
13
from libcpp.algorithm cimport sort, upper_bound, min_element, max_element
14
from libcpp.iterator cimport back_inserter
15
from libcpp.string cimport string
16
from libcpp.vector cimport vector
17

18

19
def copy_int(vector[int] values):
20
    """
21
    Test copy.
22

23
    >>> copy_int(range(5))
24
    [0, 1, 2, 3, 4]
25
    """
26
    cdef vector[int] out
27
    copy(values.begin(), values.end(), back_inserter(out))
28
    return out
29

30

31
cdef bool is_odd(int i):
32
    return i % 2
33

34

35
def copy_int_if_odd(vector[int] values):
36
    """
37
    Test copy_if.
38

39
    >>> copy_int_if_odd(range(5))
40
    [1, 3]
41
    """
42
    cdef vector[int] out
43
    copy_if(values.begin(), values.end(), back_inserter(out), is_odd)
44
    return out
45

46

47
def copy_int_n(vector[int] values, int count):
48
    """
49
    Test copy_n.
50

51
    >>> copy_int_n(range(5), 2)
52
    [0, 1]
53
    """
54
    cdef vector[int] out
55
    copy_n(values.begin(), count, back_inserter(out))
56
    return out
57

58

59
def copy_int_backward(vector[int] values):
60
    """
61
    Test copy_backward.
62

63
    >>> copy_int_backward(range(5))
64
    [0, 0, 0, 0, 1, 2, 3, 4]
65
    """
66
    out = vector[int](values.size() + 3)
67
    copy_backward(values.begin(), values.end(), out.end())
68
    return out
69

70

71
def move_int(vector[int] values):
72
    """
73
    Test move.
74

75
    >>> move_int(range(5))
76
    [0, 1, 2, 3, 4]
77
    """
78
    cdef vector[int] out
79
    move(values.begin(), values.end(), back_inserter(out))
80
    return out
81

82

83
def move_int_backward(vector[int] values):
84
    """
85
    Test move_backward.
86

87
    >>> move_int_backward(range(5))
88
    [0, 0, 0, 0, 1, 2, 3, 4]
89
    """
90
    out = vector[int](values.size() + 3)
91
    move_backward(values.begin(), values.end(), out.end())
92
    return out
93

94

95
def fill_int(vector[int] array, int value):
96
    """
97
    Test fill.
98

99
    >>> fill_int(range(5), -1)
100
    [-1, -1, -1, -1, -1]
101
    """
102
    fill(array.begin(), array.end(), value)
103
    return array
104

105

106
def fill_int_n(vector[int] array, int count, int value):
107
    """
108
    Test fill_n.
109

110
    >>> fill_int_n(range(5), 3, -1)
111
    [-1, -1, -1, 3, 4]
112
    """
113
    fill_n(array.begin(), count, value)
114
    return array
115

116

117
cdef int to_ord(unsigned char c):
118
    return c
119

120

121
def string_to_ord(string s):
122
    """
123
    Test transform (unary version).
124

125
    >> string_to_ord(b"HELLO")
126
    [72, 69, 76, 76, 79]
127
    """
128
    cdef vector[int] ordinals
129
    transform(s.begin(), s.end(), back_inserter(ordinals), to_ord)
130
    return ordinals
131

132

133
cdef int add_ints(int lhs, int rhs):
134
    return lhs + rhs
135

136

137
def add_int_vectors(vector[int] lhs, vector[int] rhs):
138
    """
139
    Test transform (binary version).
140

141
    >>> add_int_vectors([1, 2, 3], [4, 5, 6])
142
    [5, 7, 9]
143
    """
144
    transform(lhs.begin(), lhs.end(), rhs.begin(), lhs.begin(), add_ints)
145
    return lhs
146

147

148
cdef int i = 0
149
cdef int generator():
150
    return postincrement(i)
151

152

153
def generate_ints(int count):
154
    """
155
    Test generate.
156

157
    >> generate_ints(5)
158
    [0, 1, 2, 3, 4]
159
    """
160
    out = vector[int](count)
161
    generate(out.begin(), out.end(), generator)
162
    return out
163

164

165
cdef int j = 0
166
cdef int generator2():
167
    return postincrement(j)
168

169

170
def generate_n_ints(int count):
171
    """
172
    Test generate_n.
173

174
    >> generate_n_ints(5)
175
    [0, 1, 2, 3, 4, 0, 0, 0]
176
    """
177
    out = vector[int](count + 3)
178
    generate_n(out.begin(), count, generator2)
179
    return out
180

181

182
def remove_spaces(string s):
183
    """
184
    Test remove.
185

186
    >>> print(remove_spaces(b"Text with some   spaces").decode("ascii"))
187
    Textwithsomespaces
188
    """
189
    s.erase(remove(s.begin(), s.end(), ord(" ")), s.end())
190
    return s
191

192

193
cdef bool is_whitespace(unsigned char c) except -1:
194
    # std::isspace from <cctype>
195
    return chr(c) in " \f\n\r\t\v"
196

197

198
def remove_whitespace(string s):
199
    r"""
200
    Test remove_if.
201

202
    >>> print(remove_whitespace(b"Text\n with\tsome \t  whitespaces\n\n").decode("ascii"))
203
    Textwithsomewhitespaces
204
    """
205
    s.erase(remove_if(s.begin(), s.end(), &is_whitespace), s.end())
206
    return s
207

208

209
def remove_spaces2(string s):
210
    """
211
    Test remove_copy.
212

213
    >>> print(remove_spaces2(b"Text with some   spaces").decode("ascii"))
214
    Textwithsomespaces
215
    """
216
    cdef string out
217
    remove_copy(s.begin(), s.end(), back_inserter(out), ord(" "))
218
    return out
219

220

221
def remove_whitespace2(string s):
222
    r"""
223
    Test remove_copy_if.
224

225
    >>> print(remove_whitespace2(b"Text\n with\tsome \t  whitespaces\n\n").decode("ascii"))
226
    Textwithsomewhitespaces
227
    """
228
    cdef string out
229
    remove_copy_if(s.begin(), s.end(), back_inserter(out), &is_whitespace)
230
    return out
231

232

233
def replace_ints(vector[int] values, int old, int new):
234
    """
235
    Test replace.
236

237
    >>> replace_ints([5, 7, 4, 2, 8, 6, 1, 9, 0, 3], 8, 88)
238
    [5, 7, 4, 2, 88, 6, 1, 9, 0, 3]
239
    """
240
    replace(values.begin(), values.end(), old, new)
241
    return values
242

243

244
cdef bool less_than_five(int i):
245
    return i < 5
246

247

248
def replace_ints_less_than_five(vector[int] values, int new):
249
    """
250
    Test replace_if (using cppreference example that doesn't translate well).
251

252
    >>> replace_ints_less_than_five([5, 7, 4, 2, 88, 6, 1, 9, 0, 3], 55)
253
    [5, 7, 55, 55, 88, 6, 55, 9, 55, 55]
254
    """
255
    replace_if(values.begin(), values.end(), less_than_five, new)
256
    return values
257

258

259
def replace_ints2(vector[int] values, int old, int new):
260
    """
261
    Test replace_copy.
262

263
    >>> replace_ints2([5, 7, 4, 2, 8, 6, 1, 9, 0, 3], 8, 88)
264
    [5, 7, 4, 2, 88, 6, 1, 9, 0, 3]
265
    """
266
    cdef vector[int] out
267
    replace_copy(values.begin(), values.end(), back_inserter(out), old, new)
268
    return out
269

270

271
def replace_ints_less_than_five2(vector[int] values, int new):
272
    """
273
    Test replace_copy_if (using cppreference example that doesn't translate well).
274

275
    >>> replace_ints_less_than_five2([5, 7, 4, 2, 88, 6, 1, 9, 0, 3], 55)
276
    [5, 7, 55, 55, 88, 6, 55, 9, 55, 55]
277
    """
278
    cdef vector[int] out
279
    replace_copy_if(values.begin(), values.end(), back_inserter(out), less_than_five, new)
280
    return out
281

282

283
def test_swap_ints():
284
    """
285
    >>> test_swap_ints()
286
    5 3
287
    3 5
288
    """
289
    cdef int a = 5, b = 3
290
    print(a, b)
291
    swap(a, b)
292
    print(a, b)
293

294

295
def test_swap_vectors():
296
    """
297
    >>> test_swap_vectors()
298
    [1, 2, 3] [4, 5, 6]
299
    [4, 5, 6] [1, 2, 3]
300
    """
301
    cdef vector[int] a = [1, 2, 3], b = [4, 5, 6]
302
    print(a, b)
303
    swap(a, b)
304
    print(a, b)
305

306

307
def test_swap_ranges():
308
    """
309
    >>> test_swap_ranges()
310
    [1, 2, 3] [4, 5, 6]
311
    [4, 5, 6] [1, 2, 3]
312
    """
313
    cdef vector[int] a = [1, 2, 3], b = [4, 5, 6]
314
    print(a, b)
315
    swap_ranges(a.begin(), a.end(), b.begin())
316
    print(a, b)
317

318

319
def selection_sort(vector[int] values, reversed=False):
320
    """
321
    Test iter_swap using cppreference example. Extra "reversed argument tests max_element
322

323
    >>> selection_sort([-7, 6, 2, 4, -1, 6, -9, -1, 2, -5, 10, -9, -5, -3, -5, -3, 6, 6, 1, 8])
324
    [-9, -9, -7, -5, -5, -5, -3, -3, -1, -1, 1, 2, 2, 4, 6, 6, 6, 6, 8, 10]
325
    >>> selection_sort([-7, 6, 2, 4, -1, 6, -9, -1, 2, -5, 10, -9, -5, -3, -5, -3, 6, 6, 1, 8], reversed=True)
326
    [10, 8, 6, 6, 6, 6, 4, 2, 2, 1, -1, -1, -3, -3, -5, -5, -5, -7, -9, -9]
327
    """
328
    i = values.begin()
329
    end = values.end()
330
    while i < end:
331
        iter_swap(i, min_element(i, end) if not reversed else max_element(i,end))
332
        preincrement(i)
333
    return values
334

335

336
def reverse_ints(vector[int] values):
337
    """
338
    Test reverse.
339

340
    >>> reverse_ints([1, 2, 3])
341
    [3, 2, 1]
342
    """
343
    reverse(values.begin(), values.end())
344
    return values
345

346

347
def reverse_ints2(vector[int] values):
348
    """
349
    Test reverse_copy.
350

351
    >>> reverse_ints2([1, 2, 3])
352
    [3, 2, 1]
353
    """
354
    cdef vector[int] out
355
    reverse_copy(values.begin(), values.end(), back_inserter(out))
356
    return out
357

358

359
def insertion_sort(vector[int] values):
360
    """
361
    Test rotate using cppreference example.
362

363
    >>> insertion_sort([2, 4, 2, 0, 5, 10, 7, 3, 7, 1])
364
    [0, 1, 2, 2, 3, 4, 5, 7, 7, 10]
365
    """
366
    i = values.begin()
367
    while i < values.end():
368
        rotate(upper_bound(values.begin(), i, deref(i)), i, i + 1)
369
        preincrement(i)
370
    return values
371

372

373
def rotate_ints_about_middle(vector[int] values):
374
    """
375
    Test rotate_copy.
376

377
    >>> rotate_ints_about_middle([1, 2, 3, 4, 5])
378
    [3, 4, 5, 1, 2]
379
    """
380
    cdef vector[int] out
381
    cdef vector[int].iterator pivot = values.begin() + values.size()/2
382
    rotate_copy(values.begin(), pivot, values.end(), back_inserter(out))
383
    return out
384

385

386
def unique_ints(vector[int] values):
387
    """
388
    Test unique.
389

390
    >>> unique_ints([1, 2, 3, 1, 2, 3, 3, 4, 5, 4, 5, 6, 7])
391
    [1, 2, 3, 4, 5, 6, 7]
392
    """
393
    sort(values.begin(), values.end())
394
    values.erase(unique(values.begin(), values.end()), values.end())
395
    return values
396

397

398
cdef bool both_space(unsigned char lhs, unsigned char rhs):
399
    return lhs == rhs == ord(' ')
400

401

402
def collapse_spaces(string text):
403
    """
404
    Test unique (predicate version) using cppreference example for unique_copy.
405

406
    >>> print(collapse_spaces(b"The      string    with many       spaces!").decode("ascii"))
407
    The string with many spaces!
408
    """
409
    last = unique(text.begin(), text.end(), &both_space)
410
    text.erase(last, text.end())
411
    return text
412

413

414
def unique_ints2(vector[int] values):
415
    """
416
    Test unique_copy.
417

418
    >>> unique_ints2([1, 2, 3, 1, 2, 3, 3, 4, 5, 4, 5, 6, 7])
419
    [1, 2, 3, 4, 5, 6, 7]
420
    """
421
    cdef vector[int] out
422
    sort(values.begin(), values.end())
423
    unique_copy(values.begin(), values.end(), back_inserter(out))
424
    return out
425

426

427
def collapse_spaces2(string text):
428
    """
429
    Test unique_copy (predicate version) using cppreference example.
430

431
    >>> print(collapse_spaces2(b"The      string    with many       spaces!").decode("ascii"))
432
    The string with many spaces!
433
    """
434
    cdef string out
435
    unique_copy(text.begin(), text.end(), back_inserter(out), &both_space)
436
    return out
437

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

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

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

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