From 688a7a07d762cd331c6240593eb23d012c61f1b0 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Thu, 26 Jun 2008 17:55:23 +0000 Subject: [PATCH] stl_algo.h (partition_copy): Add in C++0x mode. 2008-06-26 Paolo Carlini * include/bits/stl_algo.h (partition_copy): Add in C++0x mode. * include/bits/algorithmfwd.h: Add. * testsuite/25_algorithms/headers/algorithm/synopsis.cc: Update. * testsuite/25_algorithms/partition_copy/1.cc: New. * testsuite/25_algorithms/partition_copy/check_type.cc: Likewise. * testsuite/25_algorithms/partition_copy/requirements/ explicit_instantiation/2.cc: Likewise. * testsuite/25_algorithms/partition_copy/requirements/ explicit_instantiation/pod.cc: Likewise. From-SVN: r137152 --- libstdc++-v3/ChangeLog | 12 ++ libstdc++-v3/include/bits/algorithmfwd.h | 56 ++++++--- libstdc++-v3/include/bits/stl_algo.h | 46 ++++++++ .../headers/algorithm/synopsis.cc | 5 + .../25_algorithms/partition_copy/1.cc | 107 ++++++++++++++++++ .../partition_copy/check_type.cc | 71 ++++++++++++ .../requirements/explicit_instantiation/2.cc | 48 ++++++++ .../explicit_instantiation/pod.cc | 47 ++++++++ 8 files changed, 377 insertions(+), 15 deletions(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/partition_copy/1.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/partition_copy/check_type.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/partition_copy/requirements/explicit_instantiation/2.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/partition_copy/requirements/explicit_instantiation/pod.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 777170dbc72..87331b9b56e 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2008-06-26 Paolo Carlini + + * include/bits/stl_algo.h (partition_copy): Add in C++0x mode. + * include/bits/algorithmfwd.h: Add. + * testsuite/25_algorithms/headers/algorithm/synopsis.cc: Update. + * testsuite/25_algorithms/partition_copy/1.cc: New. + * testsuite/25_algorithms/partition_copy/check_type.cc: Likewise. + * testsuite/25_algorithms/partition_copy/requirements/ + explicit_instantiation/2.cc: Likewise. + * testsuite/25_algorithms/partition_copy/requirements/ + explicit_instantiation/pod.cc: Likewise. + 2008-06-26 Chris Fairles * testsuite/20_util/unique_ptr/cons/pointer_array.cc: New. diff --git a/libstdc++-v3/include/bits/algorithmfwd.h b/libstdc++-v3/include/bits/algorithmfwd.h index 992dc4f5afe..fa4e72cd412 100644 --- a/libstdc++-v3/include/bits/algorithmfwd.h +++ b/libstdc++-v3/include/bits/algorithmfwd.h @@ -25,9 +25,12 @@ /* adjacent_find + all_of (C++0x) + any_of (C++0x) binary_search copy copy_backward + copy_if (C++0x) count count_if equal @@ -38,6 +41,7 @@ find_end find_first_of find_if + find_if_not (C++0x) for_each generate generate_n @@ -60,10 +64,12 @@ minmax_element (C++0x) mismatch next_permutation + none_of (C++0x) nth_element partial_sort partial_sort_copy partition + partition_copy (C++0x) pop_heap prev_permutation push_heap @@ -111,6 +117,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std) // adjacent_find +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + bool + all_of(_IIter, _IIter, _Predicate); + + template + bool + any_of(_IIter, _IIter, _Predicate); +#endif + template bool binary_search(_FIter, _FIter, const _Tp&); @@ -127,6 +143,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std) _BIter2 copy_backward(_BIter1, _BIter1, _BIter2); +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + _OIter + copy_if(_IIter, _IIter, _OIter, _Predicate); +#endif + // count // count_if @@ -165,28 +187,17 @@ _GLIBCXX_BEGIN_NAMESPACE(std) // find_first_of // find_if - // for_each - // generate - // generate_n #ifdef __GXX_EXPERIMENTAL_CXX0X__ - template - bool - all_of(_IIter, _IIter, _Predicate); - - template - bool - any_of(_IIter, _IIter, _Predicate); - - template - bool - none_of(_IIter, _IIter, _Predicate); - template _IIter find_if_not(_IIter, _IIter, _Predicate); #endif + // for_each + // generate + // generate_n + template bool includes(_IIter1, _IIter1, _IIter2, _IIter2); @@ -306,6 +317,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std) bool next_permutation(_BIter, _BIter, _Compare); +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + bool + none_of(_IIter, _IIter, _Predicate); +#endif + // nth_element // partial_sort @@ -317,6 +334,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std) _RAIter partial_sort_copy(_IIter, _IIter, _RAIter, _RAIter, _Compare); + // partition + +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + template + pair<_OIter1, _OIter2> + partition_copy(_IIter, _IIter, _OIter1, _OIter2, _Predicate); +#endif + template void pop_heap(_RAIter, _RAIter); diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index cee7d613d6a..43b0582db74 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -911,6 +911,52 @@ _GLIBCXX_BEGIN_NAMESPACE(std) } return __result; } + + /** + * @brief Copy the elements of a sequence to separate output sequences + * depending on the truth value of a predicate. + * @param first An input iterator. + * @param last An input iterator. + * @param out_true An output iterator. + * @param out_false An output iterator. + * @param pred A predicate. + * @return A pair designating the ends of the resulting sequences. + * + * Copies each element in the range @p [first,last) for which + * @p pred returns true to the range beginning at @p out_true + * and each element for which @p pred returns false to @p out_false. + */ + template + pair<_OutputIterator1, _OutputIterator2> + partition_copy(_InputIterator __first, _InputIterator __last, + _OutputIterator1 __out_true, _OutputIterator2 __out_false, + _Predicate __pred) + { + // concept requirements + __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) + __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1, + typename iterator_traits<_InputIterator>::value_type>) + __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2, + typename iterator_traits<_InputIterator>::value_type>) + __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, + typename iterator_traits<_InputIterator>::value_type>) + __glibcxx_requires_valid_range(__first, __last); + + for (; __first != __last; ++__first) + if (__pred(*__first)) + { + *__out_true = *__first; + ++__out_true; + } + else + { + *__out_false = *__first; + ++__out_false; + } + + return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); + } #endif /** diff --git a/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc b/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc index 8bfb22b4f60..c52c779c1ed 100644 --- a/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc +++ b/libstdc++-v3/testsuite/25_algorithms/headers/algorithm/synopsis.cc @@ -208,6 +208,11 @@ namespace std template _OIter copy_if(_IIter, _IIter, _OIter, _Predicate); + + template + pair<_OIter1, _OIter2> + partition_copy(_IIter, _IIter, _OIter1, _OIter2, _Predicate); #endif template diff --git a/libstdc++-v3/testsuite/25_algorithms/partition_copy/1.cc b/libstdc++-v3/testsuite/25_algorithms/partition_copy/1.cc new file mode 100644 index 00000000000..94fbf885b0a --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/partition_copy/1.cc @@ -0,0 +1,107 @@ +// { dg-options "-std=gnu++0x" } + +// 2008-06-26 Paolo Carlini + +// Copyright (C) 2008 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include + +using __gnu_test::test_container; +using __gnu_test::input_iterator_wrapper; +using __gnu_test::output_iterator_wrapper; + +typedef test_container Icontainer; +typedef test_container Ocontainer; +int array[] = {0, 5, 2, 1, 3, 4}; + +bool +pred(int i) +{ return i > 2; } + +void +test1() +{ + bool test __attribute__((unused)) = true; + + int true_out[1] = { -1 }; + int false_out[1] = { -1 }; + Icontainer in_con(array, array); + Ocontainer true_out_con(true_out, true_out); + Ocontainer false_out_con(false_out, false_out); + + std::pair, output_iterator_wrapper > res = + std::partition_copy(in_con.begin(), in_con.end(), + true_out_con.begin(), false_out_con.begin(), pred); + + VERIFY( res.first.ptr == true_out ); + VERIFY( res.second.ptr == false_out ); +} + +void +test2() +{ + bool test __attribute__((unused)) = true; + + int true_out[1] = { -1 }; + int false_out[1] = { -1 }; + Icontainer in_con(array, array + 2); + Ocontainer true_out_con(true_out, true_out + 1); + Ocontainer false_out_con(false_out, false_out + 1); + + std::pair, output_iterator_wrapper > res = + std::partition_copy(in_con.begin(), in_con.end(), + true_out_con.begin(), false_out_con.begin(), pred); + + VERIFY( res.first.ptr == true_out + 1 ); + VERIFY( res.second.ptr == false_out + 1 ); + VERIFY( true_out[0] == 5 ); + VERIFY( false_out[0] == 0 ); +} + +void +test3() +{ + bool test __attribute__((unused)) = true; + + int true_out[3] = { -1, -1, -1 }; + int false_out[3] = { -1, -1, -1 }; + Icontainer in_con(array, array + 6); + Ocontainer true_out_con(true_out, true_out + 3); + Ocontainer false_out_con(false_out, false_out + 3); + + std::pair, output_iterator_wrapper > res = + std::partition_copy(in_con.begin(), in_con.end(), + true_out_con.begin(), false_out_con.begin(), pred); + + VERIFY( res.first.ptr == true_out + 3 ); + VERIFY( res.second.ptr == false_out + 3 ); + VERIFY( true_out[0] == 5 && true_out[1] == 3 && true_out[2] == 4 ); + VERIFY( false_out[0] == 0 && false_out[1] == 2 && false_out[2] == 1 ); +} + +int +main() +{ + test1(); + test2(); + test3(); + return 0; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/partition_copy/check_type.cc b/libstdc++-v3/testsuite/25_algorithms/partition_copy/check_type.cc new file mode 100644 index 00000000000..bbcd692ae03 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/partition_copy/check_type.cc @@ -0,0 +1,71 @@ +// 2008-06-26 Paolo Carlini + +// Copyright (C) 2008 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// { dg-do compile } +// { dg-options "-std=gnu++0x" } + +#include +#include + +using __gnu_test::input_iterator_wrapper; +using __gnu_test::output_iterator_wrapper; + +struct X { }; + +struct Z1 +{ + Z1& + operator=(const X&) + { return *this; } +}; + +struct Z2 +{ + Z2& + operator=(const X&) + { return *this; } +}; + +bool +pred_function(const X&) +{ return true; } + +struct pred_obj +{ + bool + operator()(const X&) + { return true; } +}; + +std::pair, output_iterator_wrapper > +test1(input_iterator_wrapper& begin, + input_iterator_wrapper& end, + output_iterator_wrapper& true_output, + output_iterator_wrapper& false_output) +{ return std::partition_copy(begin, end, true_output, false_output, + pred_function); } + +std::pair, output_iterator_wrapper > +test2(input_iterator_wrapper& begin, + input_iterator_wrapper& end, + output_iterator_wrapper& true_output, + output_iterator_wrapper& false_output) +{ return std::partition_copy(begin, end, true_output, false_output, + pred_obj()); } diff --git a/libstdc++-v3/testsuite/25_algorithms/partition_copy/requirements/explicit_instantiation/2.cc b/libstdc++-v3/testsuite/25_algorithms/partition_copy/requirements/explicit_instantiation/2.cc new file mode 100644 index 00000000000..d4cb99adbcb --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/partition_copy/requirements/explicit_instantiation/2.cc @@ -0,0 +1,48 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } + +// 2008-06-26 Paolo Carlini + +// Copyright (C) 2008 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include +#include +#include + +namespace std +{ + using __gnu_test::NonDefaultConstructible; + + typedef NonDefaultConstructible value_type; + typedef value_type* iterator_type; + typedef std::pointer_to_unary_function predicate_type; + + template pair + partition_copy(iterator_type, iterator_type, + iterator_type, iterator_type, predicate_type); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/partition_copy/requirements/explicit_instantiation/pod.cc b/libstdc++-v3/testsuite/25_algorithms/partition_copy/requirements/explicit_instantiation/pod.cc new file mode 100644 index 00000000000..5d396eb02c1 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/partition_copy/requirements/explicit_instantiation/pod.cc @@ -0,0 +1,47 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } + +// 2008-06-26 Paolo Carlini + +// Copyright (C) 2008 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include +#include + +namespace std +{ + using __gnu_test::pod_int; + + typedef pod_int value_type; + typedef value_type* iterator_type; + typedef std::pointer_to_unary_function predicate_type; + + template pair + partition_copy(iterator_type, iterator_type, + iterator_type, iterator_type, predicate_type); +}