2
# tag: cpp, werror, cpp11, no-cpp-locals
4
from __future__ import print_function
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
19
def copy_int(vector[int] values):
23
>>> copy_int(range(5))
27
copy(values.begin(), values.end(), back_inserter(out))
31
cdef bool is_odd(int i):
35
def copy_int_if_odd(vector[int] values):
39
>>> copy_int_if_odd(range(5))
43
copy_if(values.begin(), values.end(), back_inserter(out), is_odd)
47
def copy_int_n(vector[int] values, int count):
51
>>> copy_int_n(range(5), 2)
55
copy_n(values.begin(), count, back_inserter(out))
59
def copy_int_backward(vector[int] values):
63
>>> copy_int_backward(range(5))
64
[0, 0, 0, 0, 1, 2, 3, 4]
66
out = vector[int](values.size() + 3)
67
copy_backward(values.begin(), values.end(), out.end())
71
def move_int(vector[int] values):
75
>>> move_int(range(5))
79
move(values.begin(), values.end(), back_inserter(out))
83
def move_int_backward(vector[int] values):
87
>>> move_int_backward(range(5))
88
[0, 0, 0, 0, 1, 2, 3, 4]
90
out = vector[int](values.size() + 3)
91
move_backward(values.begin(), values.end(), out.end())
95
def fill_int(vector[int] array, int value):
99
>>> fill_int(range(5), -1)
102
fill(array.begin(), array.end(), value)
106
def fill_int_n(vector[int] array, int count, int value):
110
>>> fill_int_n(range(5), 3, -1)
113
fill_n(array.begin(), count, value)
117
cdef int to_ord(unsigned char c):
121
def string_to_ord(string s):
123
Test transform (unary version).
125
>> string_to_ord(b"HELLO")
128
cdef vector[int] ordinals
129
transform(s.begin(), s.end(), back_inserter(ordinals), to_ord)
133
cdef int add_ints(int lhs, int rhs):
137
def add_int_vectors(vector[int] lhs, vector[int] rhs):
139
Test transform (binary version).
141
>>> add_int_vectors([1, 2, 3], [4, 5, 6])
144
transform(lhs.begin(), lhs.end(), rhs.begin(), lhs.begin(), add_ints)
150
return postincrement(i)
153
def generate_ints(int count):
160
out = vector[int](count)
161
generate(out.begin(), out.end(), generator)
166
cdef int generator2():
167
return postincrement(j)
170
def generate_n_ints(int count):
174
>> generate_n_ints(5)
175
[0, 1, 2, 3, 4, 0, 0, 0]
177
out = vector[int](count + 3)
178
generate_n(out.begin(), count, generator2)
182
def remove_spaces(string s):
186
>>> print(remove_spaces(b"Text with some spaces").decode("ascii"))
189
s.erase(remove(s.begin(), s.end(), ord(" ")), s.end())
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"
198
def remove_whitespace(string s):
202
>>> print(remove_whitespace(b"Text\n with\tsome \t whitespaces\n\n").decode("ascii"))
203
Textwithsomewhitespaces
205
s.erase(remove_if(s.begin(), s.end(), &is_whitespace), s.end())
209
def remove_spaces2(string s):
213
>>> print(remove_spaces2(b"Text with some spaces").decode("ascii"))
217
remove_copy(s.begin(), s.end(), back_inserter(out), ord(" "))
221
def remove_whitespace2(string s):
225
>>> print(remove_whitespace2(b"Text\n with\tsome \t whitespaces\n\n").decode("ascii"))
226
Textwithsomewhitespaces
229
remove_copy_if(s.begin(), s.end(), back_inserter(out), &is_whitespace)
233
def replace_ints(vector[int] values, int old, int new):
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]
240
replace(values.begin(), values.end(), old, new)
244
cdef bool less_than_five(int i):
248
def replace_ints_less_than_five(vector[int] values, int new):
250
Test replace_if (using cppreference example that doesn't translate well).
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]
255
replace_if(values.begin(), values.end(), less_than_five, new)
259
def replace_ints2(vector[int] values, int old, int new):
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]
267
replace_copy(values.begin(), values.end(), back_inserter(out), old, new)
271
def replace_ints_less_than_five2(vector[int] values, int new):
273
Test replace_copy_if (using cppreference example that doesn't translate well).
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]
279
replace_copy_if(values.begin(), values.end(), back_inserter(out), less_than_five, new)
289
cdef int a = 5, b = 3
295
def test_swap_vectors():
297
>>> test_swap_vectors()
301
cdef vector[int] a = [1, 2, 3], b = [4, 5, 6]
307
def test_swap_ranges():
309
>>> test_swap_ranges()
313
cdef vector[int] a = [1, 2, 3], b = [4, 5, 6]
315
swap_ranges(a.begin(), a.end(), b.begin())
319
def selection_sort(vector[int] values, reversed=False):
321
Test iter_swap using cppreference example. Extra "reversed argument tests max_element
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]
331
iter_swap(i, min_element(i, end) if not reversed else max_element(i,end))
336
def reverse_ints(vector[int] values):
340
>>> reverse_ints([1, 2, 3])
343
reverse(values.begin(), values.end())
347
def reverse_ints2(vector[int] values):
351
>>> reverse_ints2([1, 2, 3])
355
reverse_copy(values.begin(), values.end(), back_inserter(out))
359
def insertion_sort(vector[int] values):
361
Test rotate using cppreference example.
363
>>> insertion_sort([2, 4, 2, 0, 5, 10, 7, 3, 7, 1])
364
[0, 1, 2, 2, 3, 4, 5, 7, 7, 10]
367
while i < values.end():
368
rotate(upper_bound(values.begin(), i, deref(i)), i, i + 1)
373
def rotate_ints_about_middle(vector[int] values):
377
>>> rotate_ints_about_middle([1, 2, 3, 4, 5])
381
cdef vector[int].iterator pivot = values.begin() + values.size()/2
382
rotate_copy(values.begin(), pivot, values.end(), back_inserter(out))
386
def unique_ints(vector[int] values):
390
>>> unique_ints([1, 2, 3, 1, 2, 3, 3, 4, 5, 4, 5, 6, 7])
391
[1, 2, 3, 4, 5, 6, 7]
393
sort(values.begin(), values.end())
394
values.erase(unique(values.begin(), values.end()), values.end())
398
cdef bool both_space(unsigned char lhs, unsigned char rhs):
399
return lhs == rhs == ord(' ')
402
def collapse_spaces(string text):
404
Test unique (predicate version) using cppreference example for unique_copy.
406
>>> print(collapse_spaces(b"The string with many spaces!").decode("ascii"))
407
The string with many spaces!
409
last = unique(text.begin(), text.end(), &both_space)
410
text.erase(last, text.end())
414
def unique_ints2(vector[int] values):
418
>>> unique_ints2([1, 2, 3, 1, 2, 3, 3, 4, 5, 4, 5, 6, 7])
419
[1, 2, 3, 4, 5, 6, 7]
422
sort(values.begin(), values.end())
423
unique_copy(values.begin(), values.end(), back_inserter(out))
427
def collapse_spaces2(string text):
429
Test unique_copy (predicate version) using cppreference example.
431
>>> print(collapse_spaces2(b"The string with many spaces!").decode("ascii"))
432
The string with many spaces!
435
unique_copy(text.begin(), text.end(), back_inserter(out), &both_space)