Commit 05059e2a authored by Matthew Edwards's avatar Matthew Edwards Committed by Stefan Behnel

Add most modifying sequence operations to libcpp.algorithm (GH-3188)

Also fix tests in libcpp_algo (how did this ever work?)
parent 5a8c9844
......@@ -9,6 +9,8 @@ cdef extern from "<algorithm>" namespace "std" nogil:
bool any_of[Iter, Pred](Iter first, Iter last, Pred pred) except +
bool none_of[Iter, Pred](Iter first, Iter last, Pred pred) except +
void for_each[Iter, UnaryFunction](Iter first, Iter last, UnaryFunction f) except + # actually returns f
ptrdiff_t count[Iter, T](Iter first, Iter last, const T& value)
ptrdiff_t count_if[Iter, Pred](Iter first, Iter last, Pred pred) except +
......@@ -37,10 +39,54 @@ cdef extern from "<algorithm>" namespace "std" nogil:
Iter first1, Iter last1, Size count, const T& value, BinaryPred pred) except +
# Modifying sequence operations
OutputIter copy[InputIter, OutputIter](InputIter, InputIter, OutputIter)
OutputIt copy[InputIt, OutputIt](InputIt first, InputIt last, OutputIt d_first)
OutputIt copy_if[InputIt, OutputIt, Pred](InputIt first, InputIt last, OutputIt d_first, Pred pred) except +
OutputIt copy_n[InputIt, Size, OutputIt](InputIt first, Size count, OutputIt result)
Iter2 copy_backward[Iter1, Iter2](Iter1 first, Iter1 last, Iter2 d_last)
OutputIt move[InputIt, OutputIt](InputIt first, InputIt last, OutputIt d_first)
Iter2 move_backward[Iter1, Iter2](Iter1 first, Iter1 last, Iter2 d_last)
void fill[Iter, T](Iter first, Iter last, const T& value)
Iter fill_n[Iter, Size, T](Iter first, Size count, const T& value)
OutputIt transform[InputIt, OutputIt, UnaryOp](
InputIt first1, InputIt last1, OutputIt d_first, UnaryOp unary_op) except +
OutputIt transform[InputIt1, InputIt2, OutputIt, BinaryOp](
InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOp binary_op) except +
void generate[Iter, Generator](Iter first, Iter last, Generator g) except +
void generate_n[Iter, Size, Generator](Iter first, Size count, Generator g) except +
Iter remove[Iter, T](Iter first, Iter last, const T& value)
Iter remove_if[Iter, UnaryPred](Iter first, Iter last, UnaryPred pred)
OutputIt remove_copy[InputIt, OutputIt, T](InputIt first, InputIt last, OutputIt d_first, const T& value)
OutputIt remove_copy_if[InputIt, OutputIt, UnaryPred](
InputIt first, InputIt last, OutputIt d_first, UnaryPred pred) except +
void replace[Iter, T](Iter first, Iter last, const T& old_value, const T& new_value)
void replace_if[Iter, UnaryPred, T](Iter first, Iter last, UnaryPred pred, const T& new_value) except +
OutputIt replace_copy[InputIt, OutputIt, T](
InputIt first, InputIt last, OutputIt d_first, const T& old_value, const T& new_value)
OutputIt replace_copy_if[InputIt, OutputIt, UnaryPred, T](
InputIt first, InputIt last, OutputIt d_first, UnaryPred pred, const T& new_value) except +
void swap[T](T& a, T& b) # array overload also works
Iter2 swap_ranges[Iter1, Iter2](Iter1 first1, Iter1 last1, Iter2 first2)
void iter_swap[Iter](Iter a, Iter b)
void reverse[Iter](Iter first, Iter last)
OutputIt reverse_copy[InputIt, OutputIt](InputIt first, InputIt last, OutputIt d_first)
Iter rotate[Iter](Iter first, Iter n_first, Iter last)
OutputIt rotate_copy[InputIt, OutputIt](InputIt first, InputIt n_first, InputIt last, OutputIt d_first)
Iter unique[Iter](Iter first, Iter last)
Iter unique[Iter, BinaryPredicate](Iter first, Iter last, BinaryPredicate p) except +
Iter unique[Iter, BinaryPred](Iter first, Iter last, BinaryPred p) except +
OutputIt unique_copy[InputIt, OutputIt](InputIt first, InputIt last, OutputIt d_first)
OutputIt unique_copy[InputIt, OutputIt, BinaryPred](
InputIt first, InputIt last, OutputIt d_first, BinaryPred pred) except +
# Partitioning operations
......@@ -79,6 +125,7 @@ cdef extern from "<algorithm>" namespace "std" nogil:
void sort_heap[Iter, Compare](Iter first, Iter last, Compare comp) except +
# Minimum/maximum operations
Iter min_element[Iter](Iter first, Iter last)
# Comparison operations
......
......@@ -62,6 +62,10 @@ cdef extern from "<string>" namespace "std" nogil:
void reserve(size_t)
void clear()
bint empty()
iterator erase(iterator position)
iterator erase(const_iterator position)
iterator erase(iterator first, iterator last)
iterator erase(const_iterator first, const_iterator last)
char& at(size_t)
char& operator[](size_t)
......
This diff is collapsed.
......@@ -4,8 +4,8 @@
from cython.operator cimport dereference as deref
from libcpp cimport bool
from libcpp.algorithm cimport all_of, any_of, none_of, count, count_if, mismatch, find, find_if, find_if_not, find_end
from libcpp.algorithm cimport find_first_of, adjacent_find, search, search_n
from libcpp.algorithm cimport all_of, any_of, none_of, for_each, count, count_if, mismatch, find, find_if, find_if_not
from libcpp.algorithm cimport find_end, find_first_of, adjacent_find, search, search_n
from libcpp.iterator cimport distance
from libcpp.string cimport string
from libcpp.vector cimport vector
......@@ -61,7 +61,23 @@ def count_ones(vector[int] values):
return count(values.begin(), values.end(), 1)
def count_odd(vector[int] values):
cdef void add_one(int &i):
# https://github.com/cython/cython/issues/1863
(&i)[0] += 1
def increment_ints(vector[int] values):
"""
Test for_each.
>>> increment_ints([3, 4, 2, 8, 15, 267])
[4, 5, 3, 9, 16, 268]
"""
for_each(values.begin(), values.end(), &add_one)
return values
def count_odd(vector[int] values):
"""
Test count_if with is_odd predicate.
......@@ -174,7 +190,7 @@ def find_last_int_sequence2(vector[int] values, vector[int] target):
4
>>> find_last_int_sequence2([1, 2, 3], [4, 5])
"""
result = find_end(values.begin(), values.end(), target.begin(), target.end(), <bool (*)(int, int)>is_equal)
result = find_end(values.begin(), values.end(), target.begin(), target.end(), &is_equal)
if result != values.end():
return distance(values.begin(), result)
else:
......@@ -264,7 +280,7 @@ def in_quote2(string quote, string word):
>>> in_quote2(b"why waste time learning, when ignorance is instantaneous?", b"lemming")
False
"""
return search(quote.begin(), quote.end(), word.begin(), word.end(), <bool (*)(int, int)>is_equal) != quote.end()
return search(quote.begin(), quote.end(), word.begin(), word.end(), &is_equal) != quote.end()
def consecutive_values(string c, int count, char v):
......@@ -288,4 +304,4 @@ def consecutive_values2(string c, int count, char v):
>>> consecutive_values2(b"1001010100010101001010101", 3, ord("0"))
True
"""
return search_n(c.begin(), c.end(), count, v, <bool (*)(int, int)>is_equal) != c.end()
return search_n(c.begin(), c.end(), count, v, &is_equal) != c.end()
......@@ -21,8 +21,8 @@ def heapsort(l, bool reverse=False):
cdef vector[int] v = l
if reverse:
make_heap(v.begin(), v.end(), greater)
sort_heap(v.begin(), v.end(), greater)
make_heap(v.begin(), v.end(), &greater)
sort_heap(v.begin(), v.end(), &greater)
else:
make_heap(v.begin(), v.end())
sort_heap(v.begin(), v.end())
......@@ -39,7 +39,7 @@ def partialsort(l, int k, reverse=False):
"""
cdef vector[int] v = l
if reverse:
partial_sort(v.begin(), v.begin() + k, v.end(), greater)
partial_sort(v.begin(), v.begin() + k, v.end(), &greater)
else:
partial_sort(v.begin(), v.begin() + k, v.end())
return v
......@@ -54,7 +54,7 @@ def stdsort(l, reverse=False):
"""
cdef vector[int] v = l
if reverse:
sort(v.begin(), v.end(), greater)
sort(v.begin(), v.end(), &greater)
else:
sort(v.begin(), v.end())
return v
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment