PR libstdc++/26133 (DR 241, [WP])

2006-02-08  Paolo Carlini  <pcarlini@suse.de>

	PR libstdc++/26133  (DR 241, [WP])
	* include/bits/stl_algo.h (__unique_copy(,,, forward_iterator_tag,
	output_iterator_tag), __unique_copy(,,, input_iterator_tag,
	output_iterator_tag), __unique_copy(,,, input_iterator_tag,
	forward_iterator_tag), and predicated counterparts): Add.
	(__unique_copy(,,, output_iterator_tag), __unique_copy(,,,
	forward_iterator_tag), and predicated counterparts): Remove.
	(unique_copy): Adjust, dispatch to the three helpers above.
	* testsuite/25_algorithms/unique_copy/2.cc: New.
	* testsuite/25_algorithms/unique_copy/26133.cc: Likewise.
	* testsuite/25_algorithms/unique_copy/3.cc: Likewise.	
	* docs/html/ext/howto.html: Add an entry for DR 241.

	* testsuite/25_algorithms/unique_copy/1.cc: Minor cosmetic changes.

From-SVN: r110772
This commit is contained in:
Paolo Carlini 2006-02-08 21:51:55 +00:00 committed by Paolo Carlini
parent 5f8f531322
commit 3089d5a419
7 changed files with 382 additions and 56 deletions

View File

@ -1,3 +1,20 @@
2006-02-08 Paolo Carlini <pcarlini@suse.de>
PR libstdc++/26133 (DR 241, [WP])
* include/bits/stl_algo.h (__unique_copy(,,, forward_iterator_tag,
output_iterator_tag), __unique_copy(,,, input_iterator_tag,
output_iterator_tag), __unique_copy(,,, input_iterator_tag,
forward_iterator_tag), and predicated counterparts): Add.
(__unique_copy(,,, output_iterator_tag), __unique_copy(,,,
forward_iterator_tag), and predicated counterparts): Remove.
(unique_copy): Adjust, dispatch to the three helpers above.
* testsuite/25_algorithms/unique_copy/2.cc: New.
* testsuite/25_algorithms/unique_copy/26133.cc: Likewise.
* testsuite/25_algorithms/unique_copy/3.cc: Likewise.
* docs/html/ext/howto.html: Add an entry for DR 241.
* testsuite/25_algorithms/unique_copy/1.cc: Minor cosmetic changes.
2006-02-07 Jakub Jelinek <jakub@redhat.com> 2006-02-07 Jakub Jelinek <jakub@redhat.com>
Benjamin Kosnik <bkoz@redhat.com> Benjamin Kosnik <bkoz@redhat.com>

View File

@ -413,6 +413,13 @@
However, no specification is given what this constructor should do. However, no specification is given what this constructor should do.
</dd> </dd>
<dt><a href="lwg-defects.html#241">241</a>:
<em>Does unique_copy() require CopyConstructible and Assignable?</em>
</dt>
<dd>Add an helper for forward_iterator/output_iterator, fix the existing
one for input_iterator/output_iterator not to rely on Assignability.
</dd>
<dt><a href="lwg-defects.html#243">243</a>: <dt><a href="lwg-defects.html#243">243</a>:
<em>get and getline when sentry reports failure</em> <em>get and getline when sentry reports failure</em>
</dt> </dt>

View File

@ -1,6 +1,7 @@
// Algorithm implementation -*- C++ -*- // Algorithm implementation -*- C++ -*-
// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
// Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
@ -1294,23 +1295,23 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
* @if maint * @if maint
* This is an uglified unique_copy(_InputIterator, _InputIterator, * This is an uglified unique_copy(_InputIterator, _InputIterator,
* _OutputIterator) * _OutputIterator)
* overloaded for output iterators. * overloaded for forward iterators and output iterator as result.
* @endif * @endif
*/ */
template<typename _InputIterator, typename _OutputIterator> template<typename _ForwardIterator, typename _OutputIterator>
_OutputIterator _OutputIterator
__unique_copy(_InputIterator __first, _InputIterator __last, __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
_OutputIterator __result, _OutputIterator __result,
output_iterator_tag) forward_iterator_tag, output_iterator_tag)
{ {
// concept requirements -- taken care of in dispatching function // concept requirements -- taken care of in dispatching function
typename iterator_traits<_InputIterator>::value_type __value = *__first; _ForwardIterator __next = __first;
*__result = __value; *__result = *__first;
while (++__first != __last) while (++__next != __last)
if (!(__value == *__first)) if (!(*__first == *__next))
{ {
__value = *__first; __first = __next;
*++__result = __value; *++__result = *__first;
} }
return ++__result; return ++__result;
} }
@ -1319,14 +1320,43 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
* @if maint * @if maint
* This is an uglified unique_copy(_InputIterator, _InputIterator, * This is an uglified unique_copy(_InputIterator, _InputIterator,
* _OutputIterator) * _OutputIterator)
* overloaded for forward iterators. * overloaded for input iterators and output iterator as result.
* @endif
*/
template<typename _InputIterator, typename _OutputIterator>
_OutputIterator
__unique_copy(_InputIterator __first, _InputIterator __last,
_OutputIterator __result,
input_iterator_tag, output_iterator_tag)
{
// concept requirements -- taken care of in dispatching function
*__result = *__first;
while (true)
{
typename
iterator_traits<_InputIterator>::value_type __value = *__first;
if (++__first == __last)
break;
if (!(__value == *__first))
*++__result = *__first;
}
return ++__result;
}
/**
* @if maint
* This is an uglified unique_copy(_InputIterator, _InputIterator,
* _OutputIterator)
* overloaded for input iterators and forward iterator as result.
* @endif * @endif
*/ */
template<typename _InputIterator, typename _ForwardIterator> template<typename _InputIterator, typename _ForwardIterator>
_ForwardIterator _ForwardIterator
__unique_copy(_InputIterator __first, _InputIterator __last, __unique_copy(_InputIterator __first, _InputIterator __last,
_ForwardIterator __result, _ForwardIterator __result,
forward_iterator_tag) input_iterator_tag, forward_iterator_tag)
{ {
// concept requirements -- taken care of in dispatching function // concept requirements -- taken care of in dispatching function
*__result = *__first; *__result = *__first;
@ -1341,29 +1371,28 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
* This is an uglified * This is an uglified
* unique_copy(_InputIterator, _InputIterator, _OutputIterator, * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
* _BinaryPredicate) * _BinaryPredicate)
* overloaded for output iterators. * overloaded for forward iterators and output iterator as result.
* @endif * @endif
*/ */
template<typename _InputIterator, typename _OutputIterator, template<typename _ForwardIterator, typename _OutputIterator,
typename _BinaryPredicate> typename _BinaryPredicate>
_OutputIterator _OutputIterator
__unique_copy(_InputIterator __first, _InputIterator __last, __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
_OutputIterator __result, _OutputIterator __result, _BinaryPredicate __binary_pred,
_BinaryPredicate __binary_pred, forward_iterator_tag, output_iterator_tag)
output_iterator_tag)
{ {
// concept requirements -- iterators already checked // concept requirements -- iterators already checked
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
typename iterator_traits<_InputIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type,
typename iterator_traits<_InputIterator>::value_type>) typename iterator_traits<_ForwardIterator>::value_type>)
typename iterator_traits<_InputIterator>::value_type __value = *__first; _ForwardIterator __next = __first;
*__result = __value; *__result = *__first;
while (++__first != __last) while (++__next != __last)
if (!__binary_pred(__value, *__first)) if (!__binary_pred(*__first, *__next))
{ {
__value = *__first; __first = __next;
*++__result = __value; *++__result = *__first;
} }
return ++__result; return ++__result;
} }
@ -1373,25 +1402,60 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
* This is an uglified * This is an uglified
* unique_copy(_InputIterator, _InputIterator, _OutputIterator, * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
* _BinaryPredicate) * _BinaryPredicate)
* overloaded for forward iterators. * overloaded for input iterators and output iterator as result.
* @endif
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryPredicate>
_OutputIterator
__unique_copy(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _BinaryPredicate __binary_pred,
input_iterator_tag, output_iterator_tag)
{
// concept requirements -- iterators already checked
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
typename iterator_traits<_InputIterator>::value_type,
typename iterator_traits<_InputIterator>::value_type>)
*__result = *__first;
while (true)
{
typename
iterator_traits<_InputIterator>::value_type __value = *__first;
if (++__first == __last)
break;
if (!__binary_pred(__value, *__first))
*++__result = *__first;
}
return ++__result;
}
/**
* @if maint
* This is an uglified
* unique_copy(_InputIterator, _InputIterator, _OutputIterator,
* _BinaryPredicate)
* overloaded for input iterators and forward iterator as result.
* @endif * @endif
*/ */
template<typename _InputIterator, typename _ForwardIterator, template<typename _InputIterator, typename _ForwardIterator,
typename _BinaryPredicate> typename _BinaryPredicate>
_ForwardIterator _ForwardIterator
__unique_copy(_InputIterator __first, _InputIterator __last, __unique_copy(_InputIterator __first, _InputIterator __last,
_ForwardIterator __result, _ForwardIterator __result, _BinaryPredicate __binary_pred,
_BinaryPredicate __binary_pred, input_iterator_tag, forward_iterator_tag)
forward_iterator_tag)
{ {
// concept requirements -- iterators already checked // concept requirements -- iterators already checked
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type,
typename iterator_traits<_InputIterator>::value_type>) typename iterator_traits<_InputIterator>::value_type>)
*__result = *__first; *__result = *__first;
while (++__first != __last) while (++__first != __last)
if (!__binary_pred(*__result, *__first)) *++__result = *__first; if (!__binary_pred(*__result, *__first))
*++__result = *__first;
return ++__result; return ++__result;
} }
@ -1407,6 +1471,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
* from groups of consecutive elements that compare equal. * from groups of consecutive elements that compare equal.
* unique_copy() is stable, so the relative order of elements that are * unique_copy() is stable, so the relative order of elements that are
* copied is unchanged. * copied is unchanged.
*
* @if maint
* _GLIBCXX_RESOLVE_LIB_DEFECTS
* DR 241. Does unique_copy() require CopyConstructible and Assignable?
* @endif
*/ */
template<typename _InputIterator, typename _OutputIterator> template<typename _InputIterator, typename _OutputIterator>
inline _OutputIterator inline _OutputIterator
@ -1421,11 +1490,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
typename iterator_traits<_InputIterator>::value_type>) typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last); __glibcxx_requires_valid_range(__first, __last);
typedef typename iterator_traits<_OutputIterator>::iterator_category if (__first == __last)
_IterType; return __result;
return std::__unique_copy(__first, __last, __result,
if (__first == __last) return __result; std::__iterator_category(__first),
return std::__unique_copy(__first, __last, __result, _IterType()); std::__iterator_category(__result));
} }
/** /**
@ -1442,6 +1511,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
* true. * true.
* unique_copy() is stable, so the relative order of elements that are * unique_copy() is stable, so the relative order of elements that are
* copied is unchanged. * copied is unchanged.
*
* @if maint
* _GLIBCXX_RESOLVE_LIB_DEFECTS
* DR 241. Does unique_copy() require CopyConstructible and Assignable?
* @endif
*/ */
template<typename _InputIterator, typename _OutputIterator, template<typename _InputIterator, typename _OutputIterator,
typename _BinaryPredicate> typename _BinaryPredicate>
@ -1456,12 +1530,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
typename iterator_traits<_InputIterator>::value_type>) typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last); __glibcxx_requires_valid_range(__first, __last);
typedef typename iterator_traits<_OutputIterator>::iterator_category if (__first == __last)
_IterType; return __result;
return std::__unique_copy(__first, __last, __result, __binary_pred,
if (__first == __last) return __result; std::__iterator_category(__first),
return std::__unique_copy(__first, __last, __result, std::__iterator_category(__result));
__binary_pred, _IterType());
} }
/** /**

View File

@ -1,4 +1,4 @@
// Copyright (C) 2005 Free Software Foundation, Inc. // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
@ -16,7 +16,7 @@
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA. // USA.
// 25.5.8 [lib.alg.unique] // 25.2.8 [lib.alg.unique]
#include <algorithm> #include <algorithm>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
@ -26,7 +26,7 @@ using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper; using __gnu_test::input_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper; using __gnu_test::forward_iterator_wrapper;
using __gnu_test::output_iterator_wrapper; using __gnu_test::output_iterator_wrapper;
using std::unique; using std::unique_copy;
typedef test_container<int, input_iterator_wrapper> Icontainer; typedef test_container<int, input_iterator_wrapper> Icontainer;
typedef test_container<int, forward_iterator_wrapper> Fcontainer; typedef test_container<int, forward_iterator_wrapper> Fcontainer;
@ -41,7 +41,7 @@ test1()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1); Icontainer con1(array1, array1);
Ocontainer con2(array2, array2); Ocontainer con2(array2, array2);
VERIFY(unique_copy(con1.begin(), con1.end(), con2.begin()).ptr == array2); VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr == array2 );
} }
void void
@ -50,9 +50,9 @@ test2()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1 + 6); Icontainer con1(array1, array1 + 6);
Ocontainer con2(array2, array2 + 2); Ocontainer con2(array2, array2 + 2);
VERIFY(unique_copy(con1.begin(), con1.end(), con2.begin()).ptr VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr
== array2 + 2); == array2 + 2 );
VERIFY(array2[0] == 0 && array2[1] == 1); VERIFY( array2[0] == 0 && array2[1] == 1 );
} }
void void
@ -61,7 +61,7 @@ test3()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1); Icontainer con1(array1, array1);
Fcontainer con2(array2, array2); Fcontainer con2(array2, array2);
VERIFY(unique_copy(con1.begin(), con1.end(), con2.begin()).ptr == array2); VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr == array2 );
} }
void void
@ -70,9 +70,9 @@ test4()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1 + 6); Icontainer con1(array1, array1 + 6);
Fcontainer con2(array2, array2 + 2); Fcontainer con2(array2, array2 + 2);
VERIFY(unique_copy(con1.begin(), con1.end(), con2.begin()).ptr VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr
== array2 + 2); == array2 + 2 );
VERIFY(array2[0] == 0 && array2[1] == 1); VERIFY( array2[0] == 0 && array2[1] == 1 );
} }
int int

View File

@ -0,0 +1,85 @@
// Copyright (C) 2006 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.
// 25.2.8 [lib.alg.unique]
#include <algorithm>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::forward_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
using std::unique_copy;
using std::equal_to;
typedef test_container<int, forward_iterator_wrapper> Fcontainer;
typedef test_container<int, output_iterator_wrapper> Ocontainer;
int array1[] = {0, 0, 0, 1, 1, 1};
int array2[2];
void
test1()
{
bool test __attribute__((unused)) = true;
Fcontainer con1(array1, array1);
Ocontainer con2(array2, array2);
VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr == array2 );
}
void
test2()
{
bool test __attribute__((unused)) = true;
Fcontainer con1(array1, array1 + 6);
Ocontainer con2(array2, array2 + 2);
VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr
== array2 + 2 );
VERIFY( array2[0] == 0 && array2[1] == 1 );
}
void
test3()
{
bool test __attribute__((unused)) = true;
Fcontainer con1(array1, array1);
Ocontainer con2(array2, array2);
VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin(),
equal_to<int>()).ptr == array2 );
}
void
test4()
{
bool test __attribute__((unused)) = true;
Fcontainer con1(array1, array1 + 6);
Ocontainer con2(array2, array2 + 2);
VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin(),
equal_to<int>()).ptr == array2 + 2 );
VERIFY( array2[0] == 0 && array2[1] == 1 );
}
int
main()
{
test1();
test2();
test3();
test4();
}

View File

@ -0,0 +1,51 @@
// Copyright (C) 2006 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 <algorithm>
#include <iterator>
#include <sstream>
#include <testsuite_hooks.h>
struct no_assign
{
int const x;
no_assign() : x(23) { }
operator int() const { return x; }
};
// libstdc++/26133
void test01()
{
bool test __attribute__((unused)) = true;
std::ostringstream oss1, oss2;
no_assign in[4];
std::unique_copy(in, in + 4, std::ostream_iterator<int>(oss1, "\n"));
VERIFY( oss1.str() == "23\n" );
std::unique_copy(in, in + 4, std::ostream_iterator<int>(oss2, "\n"),
std::equal_to<int>());
VERIFY( oss2.str() == "23\n" );
}
int main()
{
test01();
return 0;
}

View File

@ -0,0 +1,93 @@
// Copyright (C) 2006 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.
// 25.2.8 [lib.alg.unique]
#include <algorithm>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::input_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
using std::unique_copy;
using std::equal_to;
struct no_assign
{
int const x;
no_assign() : x(23) { }
no_assign(int val) : x(val) { }
operator int() const { return x; }
};
typedef test_container<no_assign, input_iterator_wrapper> Icontainer;
typedef test_container<int, output_iterator_wrapper> Ocontainer;
no_assign array1[] = {0, 0, 0, 1, 1, 1};
int array2[2];
void
test1()
{
bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1);
Ocontainer con2(array2, array2);
VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr == array2 );
}
void
test2()
{
bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1 + 6);
Ocontainer con2(array2, array2 + 2);
VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr
== array2 + 2 );
VERIFY( array2[0] == 0 && array2[1] == 1 );
}
void
test3()
{
bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1);
Ocontainer con2(array2, array2);
VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin(),
equal_to<int>()).ptr == array2 );
}
void
test4()
{
bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1 + 6);
Ocontainer con2(array2, array2 + 2);
VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin(),
equal_to<int>()).ptr == array2 + 2 );
VERIFY( array2[0] == 0 && array2[1] == 1 );
}
int
main()
{
test1();
test2();
test3();
test4();
}