+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>
Benjamin Kosnik <bkoz@redhat.com>
However, no specification is given what this constructor should do.
</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>:
<em>get and getline when sentry reports failure</em>
</dt>
// 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
// software; you can redistribute it and/or modify it under the
* @if maint
* This is an uglified unique_copy(_InputIterator, _InputIterator,
* _OutputIterator)
- * overloaded for output iterators.
+ * overloaded for forward iterators and output iterator as result.
* @endif
*/
- template<typename _InputIterator, typename _OutputIterator>
+ template<typename _ForwardIterator, typename _OutputIterator>
_OutputIterator
- __unique_copy(_InputIterator __first, _InputIterator __last,
+ __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
_OutputIterator __result,
- output_iterator_tag)
+ forward_iterator_tag, output_iterator_tag)
{
// concept requirements -- taken care of in dispatching function
- typename iterator_traits<_InputIterator>::value_type __value = *__first;
- *__result = __value;
- while (++__first != __last)
- if (!(__value == *__first))
+ _ForwardIterator __next = __first;
+ *__result = *__first;
+ while (++__next != __last)
+ if (!(*__first == *__next))
{
- __value = *__first;
- *++__result = __value;
+ __first = __next;
+ *++__result = *__first;
}
return ++__result;
}
* @if maint
* This is an uglified unique_copy(_InputIterator, _InputIterator,
* _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
*/
template<typename _InputIterator, typename _ForwardIterator>
_ForwardIterator
__unique_copy(_InputIterator __first, _InputIterator __last,
_ForwardIterator __result,
- forward_iterator_tag)
+ input_iterator_tag, forward_iterator_tag)
{
// concept requirements -- taken care of in dispatching function
*__result = *__first;
* This is an uglified
* unique_copy(_InputIterator, _InputIterator, _OutputIterator,
* _BinaryPredicate)
- * overloaded for output iterators.
+ * overloaded for forward iterators and output iterator as result.
+ * @endif
+ */
+ template<typename _ForwardIterator, typename _OutputIterator,
+ typename _BinaryPredicate>
+ _OutputIterator
+ __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
+ _OutputIterator __result, _BinaryPredicate __binary_pred,
+ forward_iterator_tag, output_iterator_tag)
+ {
+ // concept requirements -- iterators already checked
+ __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
+ typename iterator_traits<_ForwardIterator>::value_type,
+ typename iterator_traits<_ForwardIterator>::value_type>)
+
+ _ForwardIterator __next = __first;
+ *__result = *__first;
+ while (++__next != __last)
+ if (!__binary_pred(*__first, *__next))
+ {
+ __first = __next;
+ *++__result = *__first;
+ }
+ return ++__result;
+ }
+
+ /**
+ * @if maint
+ * This is an uglified
+ * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
+ * _BinaryPredicate)
+ * 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,
- output_iterator_tag)
+ _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>)
- typename iterator_traits<_InputIterator>::value_type __value = *__first;
- *__result = __value;
- while (++__first != __last)
- if (!__binary_pred(__value, *__first))
- {
- __value = *__first;
- *++__result = __value;
- }
+ *__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;
}
* This is an uglified
* unique_copy(_InputIterator, _InputIterator, _OutputIterator,
* _BinaryPredicate)
- * overloaded for forward iterators.
+ * overloaded for input iterators and forward iterator as result.
* @endif
*/
template<typename _InputIterator, typename _ForwardIterator,
typename _BinaryPredicate>
_ForwardIterator
__unique_copy(_InputIterator __first, _InputIterator __last,
- _ForwardIterator __result,
- _BinaryPredicate __binary_pred,
- forward_iterator_tag)
+ _ForwardIterator __result, _BinaryPredicate __binary_pred,
+ input_iterator_tag, forward_iterator_tag)
{
// concept requirements -- iterators already checked
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
- typename iterator_traits<_ForwardIterator>::value_type,
- typename iterator_traits<_InputIterator>::value_type>)
+ typename iterator_traits<_ForwardIterator>::value_type,
+ typename iterator_traits<_InputIterator>::value_type>)
*__result = *__first;
while (++__first != __last)
- if (!__binary_pred(*__result, *__first)) *++__result = *__first;
+ if (!__binary_pred(*__result, *__first))
+ *++__result = *__first;
return ++__result;
}
* from groups of consecutive elements that compare equal.
* unique_copy() is stable, so the relative order of elements that are
* copied is unchanged.
+ *
+ * @if maint
+ * _GLIBCXX_RESOLVE_LIB_DEFECTS
+ * DR 241. Does unique_copy() require CopyConstructible and Assignable?
+ * @endif
*/
template<typename _InputIterator, typename _OutputIterator>
inline _OutputIterator
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
- typedef typename iterator_traits<_OutputIterator>::iterator_category
- _IterType;
-
- if (__first == __last) return __result;
- return std::__unique_copy(__first, __last, __result, _IterType());
+ if (__first == __last)
+ return __result;
+ return std::__unique_copy(__first, __last, __result,
+ std::__iterator_category(__first),
+ std::__iterator_category(__result));
}
/**
* true.
* unique_copy() is stable, so the relative order of elements that are
* copied is unchanged.
+ *
+ * @if maint
+ * _GLIBCXX_RESOLVE_LIB_DEFECTS
+ * DR 241. Does unique_copy() require CopyConstructible and Assignable?
+ * @endif
*/
template<typename _InputIterator, typename _OutputIterator,
typename _BinaryPredicate>
typename iterator_traits<_InputIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
- typedef typename iterator_traits<_OutputIterator>::iterator_category
- _IterType;
-
- if (__first == __last) return __result;
- return std::__unique_copy(__first, __last, __result,
- __binary_pred, _IterType());
+ if (__first == __last)
+ return __result;
+ return std::__unique_copy(__first, __last, __result, __binary_pred,
+ std::__iterator_category(__first),
+ std::__iterator_category(__result));
}
/**
-// 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
// software; you can redistribute it and/or modify it under the
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
-// 25.5.8 [lib.alg.unique]
+// 25.2.8 [lib.alg.unique]
#include <algorithm>
#include <testsuite_hooks.h>
using __gnu_test::input_iterator_wrapper;
using __gnu_test::forward_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, forward_iterator_wrapper> Fcontainer;
bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1);
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
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);
+ VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr
+ == array2 + 2 );
+ VERIFY( array2[0] == 0 && array2[1] == 1 );
}
void
bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1);
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
bool test __attribute__((unused)) = true;
Icontainer con1(array1, array1 + 6);
Fcontainer con2(array2, array2 + 2);
- VERIFY(unique_copy(con1.begin(), con1.end(), con2.begin()).ptr
- == array2 + 2);
- VERIFY(array2[0] == 0 && array2[1] == 1);
+ VERIFY( unique_copy(con1.begin(), con1.end(), con2.begin()).ptr
+ == array2 + 2 );
+ VERIFY( array2[0] == 0 && array2[1] == 1 );
}
int
--- /dev/null
+// 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();
+}
--- /dev/null
+// 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;
+}
--- /dev/null
+// 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();
+}