Commit 4a787fa8 by Paolo Carlini

[multiple changes]

2004-06-12  Paolo Carlini  <pcarlini@suse.de>

	* include/ext/algorithm: Trivial formatting fixes.
	* include/ext/functional: Likewise.
	* include/ext/hash_fun.h: Likewise.
	* include/ext/iterator: Likewise.

2004-06-12  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/basic_string.tcc (find(const _CharT*, size_type,
	size_type)): Reimplement using std::search.
	* src/string-inst.cc: Instantiate std::search for char/wchar_t.

2004-06-12  Dhruv Matani  <dhruvbird@gmx.net>

	* testsuite/performance/21_strings/string_find.cc: New.

From-SVN: r83022
parent 019c8e80
2004-06-12 Paolo Carlini <pcarlini@suse.de>
* include/ext/algorithm: Trivial formatting fixes.
* include/ext/functional: Likewise.
* include/ext/hash_fun.h: Likewise.
* include/ext/iterator: Likewise.
2004-06-12 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.tcc (find(const _CharT*, size_type,
size_type)): Reimplement using std::search.
* src/string-inst.cc: Instantiate std::search for char/wchar_t.
2004-06-12 Dhruv Matani <dhruvbird@gmx.net>
* testsuite/performance/21_strings/string_find.cc: New.
2004-06-10 Aaron W. LaFramboise <aaronraolete36@aaronwl.com>
* include/bits/istream.tcc (istream::ignore): Fix for -Wuninitialized.
......
......@@ -685,12 +685,17 @@ namespace std
find(const _CharT* __s, size_type __pos, size_type __n) const
{
__glibcxx_requires_string_len(__s, __n);
size_type __ret = npos;
const size_type __size = this->size();
const _CharT* __data = _M_data();
for (; __pos + __n <= __size; ++__pos)
if (traits_type::compare(__data + __pos, __s, __n) == 0)
return __pos;
return npos;
if (__pos + __n <= __size)
{
const _CharT* __data = _M_data();
const _CharT* __p = std::search(__data + __pos, __data + __size,
__s, __s + __n, traits_type::eq);
if (__p != __data + __size || __n == 0)
__ret = __p - __data;
}
return __ret;
}
template<typename _CharT, typename _Traits, typename _Alloc>
......@@ -698,8 +703,8 @@ namespace std
basic_string<_CharT, _Traits, _Alloc>::
find(_CharT __c, size_type __pos) const
{
const size_type __size = this->size();
size_type __ret = npos;
const size_type __size = this->size();
if (__pos < __size)
{
const _CharT* __data = _M_data();
......
// Algorithm extensions -*- C++ -*-
// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
// Copyright (C) 2001, 2002, 2004 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
......@@ -84,11 +84,12 @@ namespace __gnu_cxx
_OutputIterator __result,
input_iterator_tag)
{
for ( ; __count > 0; --__count) {
*__result = *__first;
++__first;
++__result;
}
for ( ; __count > 0; --__count)
{
*__result = *__first;
++__first;
++__result;
}
return pair<_InputIterator, _OutputIterator>(__first, __result);
}
......@@ -99,8 +100,9 @@ namespace __gnu_cxx
random_access_iterator_tag)
{
_RAIterator __last = __first + __count;
return pair<_RAIterator, _OutputIterator>(__last,
std::copy(__first, __last, __result));
return pair<_RAIterator, _OutputIterator>(__last, std::copy(__first,
__last,
__result));
}
/**
......@@ -132,23 +134,24 @@ namespace __gnu_cxx
template<typename _InputIterator1, typename _InputIterator2>
int
__lexicographical_compare_3way(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _InputIterator2 __last2)
__lexicographical_compare_3way(_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
_InputIterator2 __last2)
{
while (__first1 != __last1 && __first2 != __last2) {
if (*__first1 < *__first2)
return -1;
if (*__first2 < *__first1)
return 1;
++__first1;
++__first2;
}
if (__first2 == __last2) {
while (__first1 != __last1 && __first2 != __last2)
{
if (*__first1 < *__first2)
return -1;
if (*__first2 < *__first1)
return 1;
++__first1;
++__first2;
}
if (__first2 == __last2)
return !(__first1 == __last1);
}
else {
else
return -1;
}
}
inline int
......@@ -169,11 +172,10 @@ namespace __gnu_cxx
const char* __first2, const char* __last2)
{
#if CHAR_MAX == SCHAR_MAX
return __lexicographical_compare_3way(
(const signed char*) __first1,
(const signed char*) __last1,
(const signed char*) __first2,
(const signed char*) __last2);
return __lexicographical_compare_3way((const signed char*) __first1,
(const signed char*) __last1,
(const signed char*) __first2,
(const signed char*) __last2);
#else
return __lexicographical_compare_3way((const unsigned char*) __first1,
(const unsigned char*) __last1,
......@@ -198,8 +200,10 @@ namespace __gnu_cxx
*/
template<typename _InputIterator1, typename _InputIterator2>
int
lexicographical_compare_3way(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _InputIterator2 __last2)
lexicographical_compare_3way(_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
_InputIterator2 __last2)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
......@@ -211,12 +215,12 @@ namespace __gnu_cxx
__glibcxx_requires_valid_range(__first1, __last1);
__glibcxx_requires_valid_range(__first2, __last2);
return __lexicographical_compare_3way(__first1, __last1, __first2, __last2);
return __lexicographical_compare_3way(__first1, __last1, __first2,
__last2);
}
// count and count_if: this version, whose return type is void, was present
// in the HP STL, and is retained as an extension for backward compatibility.
template<typename _InputIterator, typename _Tp, typename _Size>
void
count(_InputIterator __first, _InputIterator __last,
......@@ -259,7 +263,8 @@ namespace __gnu_cxx
* @ingroup SGIextensions
* @doctodo
*/
template<typename _ForwardIterator, typename _OutputIterator, typename _Distance>
template<typename _ForwardIterator, typename _OutputIterator,
typename _Distance>
_OutputIterator
random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
_OutputIterator __out, const _Distance __n)
......@@ -273,16 +278,17 @@ namespace __gnu_cxx
_Distance __remaining = std::distance(__first, __last);
_Distance __m = min(__n, __remaining);
while (__m > 0) {
if ((std::rand() % __remaining) < __m) {
while (__m > 0)
{
if ((std::rand() % __remaining) < __m)
{
*__out = *__first;
++__out;
--__m;
}
--__remaining;
++__first;
}
--__remaining;
++__first;
}
return __out;
}
......@@ -291,8 +297,8 @@ namespace __gnu_cxx
* @ingroup SGIextensions
* @doctodo
*/
template<typename _ForwardIterator, typename _OutputIterator, typename _Distance,
typename _RandomNumberGenerator>
template<typename _ForwardIterator, typename _OutputIterator,
typename _Distance, typename _RandomNumberGenerator>
_OutputIterator
random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
_OutputIterator __out, const _Distance __n,
......@@ -309,20 +315,22 @@ namespace __gnu_cxx
_Distance __remaining = std::distance(__first, __last);
_Distance __m = min(__n, __remaining);
while (__m > 0) {
if (__rand(__remaining) < __m) {
while (__m > 0)
{
if (__rand(__remaining) < __m)
{
*__out = *__first;
++__out;
--__m;
}
--__remaining;
++__first;
}
--__remaining;
++__first;
}
return __out;
}
template<typename _InputIterator, typename _RandomAccessIterator, typename _Distance>
template<typename _InputIterator, typename _RandomAccessIterator,
typename _Distance>
_RandomAccessIterator
__random_sample(_InputIterator __first, _InputIterator __last,
_RandomAccessIterator __out,
......@@ -333,14 +341,14 @@ namespace __gnu_cxx
for ( ; __first != __last && __m < __n; ++__m, ++__first)
__out[__m] = *__first;
while (__first != __last) {
++__t;
_Distance __M = std::rand() % (__t);
if (__M < __n)
__out[__M] = *__first;
++__first;
}
while (__first != __last)
{
++__t;
_Distance __M = std::rand() % (__t);
if (__M < __n)
__out[__M] = *__first;
++__first;
}
return __out + __m;
}
......@@ -361,14 +369,14 @@ namespace __gnu_cxx
for ( ; __first != __last && __m < __n; ++__m, ++__first)
__out[__m] = *__first;
while (__first != __last) {
++__t;
_Distance __M = __rand(__t);
if (__M < __n)
__out[__M] = *__first;
++__first;
}
while (__first != __last)
{
++__t;
_Distance __M = __rand(__t);
if (__M < __n)
__out[__M] = *__first;
++__first;
}
return __out + __m;
}
......@@ -380,7 +388,8 @@ namespace __gnu_cxx
template<typename _InputIterator, typename _RandomAccessIterator>
inline _RandomAccessIterator
random_sample(_InputIterator __first, _InputIterator __last,
_RandomAccessIterator __out_first, _RandomAccessIterator __out_last)
_RandomAccessIterator __out_first,
_RandomAccessIterator __out_last)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
......@@ -402,7 +411,8 @@ namespace __gnu_cxx
typename _RandomNumberGenerator>
inline _RandomAccessIterator
random_sample(_InputIterator __first, _InputIterator __last,
_RandomAccessIterator __out_first, _RandomAccessIterator __out_last,
_RandomAccessIterator __out_first,
_RandomAccessIterator __out_last,
_RandomNumberGenerator& __rand)
{
// concept requirements
......@@ -427,7 +437,8 @@ namespace __gnu_cxx
is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
{
// concept requirements
__glibcxx_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
__glibcxx_function_requires(_RandomAccessIteratorConcept<
_RandomAccessIterator>)
__glibcxx_function_requires(_LessThanComparableConcept<
typename iterator_traits<_RandomAccessIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
......@@ -446,7 +457,8 @@ namespace __gnu_cxx
_StrictWeakOrdering __comp)
{
// concept requirements
__glibcxx_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
__glibcxx_function_requires(_RandomAccessIteratorConcept<
_RandomAccessIterator>)
__glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
typename iterator_traits<_RandomAccessIterator>::value_type,
typename iterator_traits<_RandomAccessIterator>::value_type>)
......@@ -478,11 +490,9 @@ namespace __gnu_cxx
return true;
_ForwardIterator __next = __first;
for (++__next; __next != __last; __first = __next, ++__next) {
for (++__next; __next != __last; __first = __next, ++__next)
if (*__next < *__first)
return false;
}
return true;
}
......@@ -493,7 +503,8 @@ namespace __gnu_cxx
*/
template<typename _ForwardIterator, typename _StrictWeakOrdering>
bool
is_sorted(_ForwardIterator __first, _ForwardIterator __last, _StrictWeakOrdering __comp)
is_sorted(_ForwardIterator __first, _ForwardIterator __last,
_StrictWeakOrdering __comp)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
......@@ -506,11 +517,9 @@ namespace __gnu_cxx
return true;
_ForwardIterator __next = __first;
for (++__next; __next != __last; __first = __next, ++__next) {
for (++__next; __next != __last; __first = __next, ++__next)
if (__comp(*__next, *__first))
return false;
}
return true;
}
} // namespace __gnu_cxx
......
// 'struct hash' from SGI -*- C++ -*-
// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
// Copyright (C) 2001, 2002, 2003, 2004 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
......@@ -68,55 +68,105 @@ namespace __gnu_cxx
{
using std::size_t;
template <class _Key> struct hash { };
template<class _Key>
struct hash { };
inline size_t
__stl_hash_string(const char* __s)
{
unsigned long __h = 0;
for ( ; *__s; ++__s)
__h = 5*__h + *__s;
__h = 5 * __h + *__s;
return size_t(__h);
}
template<> struct hash<char*>
{
size_t operator()(const char* __s) const
{ return __stl_hash_string(__s); }
};
template<> struct hash<const char*>
{
size_t operator()(const char* __s) const
{ return __stl_hash_string(__s); }
};
template<> struct hash<char>
{ size_t operator()(char __x) const { return __x; } };
template<> struct hash<unsigned char>
{ size_t operator()(unsigned char __x) const { return __x; } };
template<> struct hash<signed char>
{ size_t operator()(unsigned char __x) const { return __x; } };
template<> struct hash<short>
{ size_t operator()(short __x) const { return __x; } };
template<> struct hash<unsigned short>
{ size_t operator()(unsigned short __x) const { return __x; } };
template<> struct hash<int>
{ size_t operator()(int __x) const { return __x; } };
template<> struct hash<unsigned int>
{ size_t operator()(unsigned int __x) const { return __x; } };
template<> struct hash<long>
{ size_t operator()(long __x) const { return __x; } };
template<> struct hash<unsigned long>
{ size_t operator()(unsigned long __x) const { return __x; } };
template<>
struct hash<char*>
{
size_t
operator()(const char* __s) const
{ return __stl_hash_string(__s); }
};
template<>
struct hash<const char*>
{
size_t
operator()(const char* __s) const
{ return __stl_hash_string(__s); }
};
template<>
struct hash<char>
{
size_t
operator()(char __x) const
{ return __x; }
};
template<>
struct hash<unsigned char>
{
size_t
operator()(unsigned char __x) const
{ return __x; }
};
template<>
struct hash<signed char>
{
size_t
operator()(unsigned char __x) const
{ return __x; }
};
template<>
struct hash<short>
{
size_t
operator()(short __x) const
{ return __x; }
};
template<>
struct hash<unsigned short>
{
size_t
operator()(unsigned short __x) const
{ return __x; }
};
template<>
struct hash<int>
{
size_t
operator()(int __x) const
{ return __x; }
};
template<>
struct hash<unsigned int>
{
size_t
operator()(unsigned int __x) const
{ return __x; }
};
template<>
struct hash<long>
{
size_t
operator()(long __x) const
{ return __x; }
};
template<>
struct hash<unsigned long>
{
size_t
operator()(unsigned long __x) const
{ return __x; }
};
} // namespace __gnu_cxx
#endif
// HP/SGI iterator extensions -*- C++ -*-
// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
// Copyright (C) 2001, 2002, 2004 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
......@@ -81,7 +81,11 @@ namespace __gnu_cxx
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
while (__first != __last) { ++__first; ++__n; }
while (__first != __last)
{
++__first;
++__n;
}
}
template<typename _RandomAccessIterator, typename _Distance>
......@@ -90,7 +94,8 @@ namespace __gnu_cxx
_Distance& __n, std::random_access_iterator_tag)
{
// concept requirements
__glibcxx_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
__glibcxx_function_requires(_RandomAccessIteratorConcept<
_RandomAccessIterator>)
__n += __last - __first;
}
......
......@@ -76,6 +76,11 @@ namespace std
C*
S::_S_construct(const C*, const C*, const allocator<C>&,
forward_iterator_tag);
// Used in str::find.
template
const C*
search(const C*, const C*, const C*, const C*, bool(*)(const C&, const C&));
} // namespace std
namespace __gnu_cxx
......
// Copyright (C) 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// 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 <string>
#include <testsuite_performance.h>
void
test_pair(const std::string& s, const std::string& f, int n)
{
std::string::size_type sz = 0;
for (int i = 0; i < n; ++i)
sz = s.find(f);
}
int main()
{
using namespace std;
using namespace __gnu_test;
time_counter time;
resource_counter resource;
const unsigned int iterations = 2000000;
string s, f;
s = "aabbaabbaaxd adbffdadgaxaabbbddhatyaaaabbbaabbaabbcsy";
f = "aabbaabbc";
start_counters(time, resource);
test_pair(s, f, iterations);
stop_counters(time, resource);
report_performance(__FILE__, "1", time, resource);
clear_counters(time, resource);
f = "aabbb";
start_counters(time, resource);
test_pair(s, f, iterations);
stop_counters(time, resource);
report_performance(__FILE__, "2", time, resource);
clear_counters(time, resource);
f = "xd";
start_counters(time, resource);
test_pair(s, f, iterations);
stop_counters(time, resource);
report_performance(__FILE__, "3", time, resource);
clear_counters(time, resource);
s = "dhruv is a very very good boy ;-)";
f = "very";
start_counters(time, resource);
test_pair(s, f, iterations);
stop_counters(time, resource);
report_performance(__FILE__, "4", time, resource);
clear_counters(time, resource);
f = "bad";
start_counters(time, resource);
test_pair(s, f, iterations);
stop_counters(time, resource);
report_performance(__FILE__, "5", time, resource);
clear_counters(time, resource);
f = "extra irritating";
start_counters(time, resource);
test_pair(s, f, iterations);
stop_counters(time, resource);
report_performance(__FILE__, "6", time, resource);
clear_counters(time, resource);
s = "this is a very this is a very this is a verty this is a very "
"this is a very long sentence";
f = "this is a very long sentence";
start_counters(time, resource);
test_pair(s, f, iterations);
stop_counters(time, resource);
report_performance(__FILE__, "7", time, resource);
clear_counters(time, resource);
return 0;
}
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