Commit 43e9f722 by Jonathan Wakely Committed by Jonathan Wakely

Add <experimental/algorithm> and <experimental/functional>.

	* doc/xml/manual/status_cxx2014.xml: Update TS status.
	* include/Makefile.am: Add new headers.
	* include/Makefile.in: Regenerate.
	* include/experimental/algorithm: New.
	* include/experimental/functional: New.
	* testsuite/experimental/algorithm/sample.cc: New.
	* testsuite/experimental/algorithm/search.cc: New.
	* testsuite/experimental/functional/not_fn.cc: New.
	* testsuite/experimental/functional/searchers.cc: New.
	* testsuite/experimental/functional/value.cc: New.
	* testsuite/experimental/feat-lib-fund.cc: Add headers and reorder.

From-SVN: r216847
parent 86fea2cb
2014-10-29 Jonathan Wakely <jwakely@redhat.com>
* doc/xml/manual/status_cxx2014.xml: Update TS status.
* include/Makefile.am: Add new headers.
* include/Makefile.in: Regenerate.
* include/experimental/algorithm: New.
* include/experimental/functional: New.
* testsuite/experimental/algorithm/sample.cc: New.
* testsuite/experimental/algorithm/search.cc: New.
* testsuite/experimental/functional/not_fn.cc: New.
* testsuite/experimental/functional/searchers.cc: New.
* testsuite/experimental/functional/value.cc: New.
* testsuite/experimental/feat-lib-fund.cc: Add headers and reorder.
2014-10-25 François Dumont <fdumont@gcc.gnu.org>
* doc/xml/manual/status_cxx2011.xml: Update unordered container
......
......@@ -329,14 +329,13 @@ not in any particular release.
</row>
<row>
<?dbhtml bgcolor="#C8B0B0" ?>
<entry>
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="">
N3905
</link>
</entry>
<entry>Faster string searching (Boyer-Moore et al.)</entry>
<entry>N</entry>
<entry>Y</entry>
<entry>Library Fundamentals TS</entry>
</row>
......@@ -387,14 +386,13 @@ not in any particular release.
</row>
<row>
<?dbhtml bgcolor="#C8B0B0" ?>
<entry>
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3925.pdf">
N3925
</link>
</entry>
<entry>A sample proposal</entry>
<entry>N</entry>
<entry>Y</entry>
<entry>Library Fundamentals TS</entry>
</row>
......
......@@ -639,8 +639,10 @@ decimal_headers = \
experimental_srcdir = ${glibcxx_srcdir}/include/experimental
experimental_builddir = ./experimental
experimental_headers = \
${experimental_srcdir}/algorithm \
${experimental_srcdir}/any \
${experimental_srcdir}/chrono \
${experimental_srcdir}/functional \
${experimental_srcdir}/optional \
${experimental_srcdir}/ratio \
${experimental_srcdir}/string_view \
......
......@@ -905,8 +905,10 @@ decimal_headers = \
experimental_srcdir = ${glibcxx_srcdir}/include/experimental
experimental_builddir = ./experimental
experimental_headers = \
${experimental_srcdir}/algorithm \
${experimental_srcdir}/any \
${experimental_srcdir}/chrono \
${experimental_srcdir}/functional \
${experimental_srcdir}/optional \
${experimental_srcdir}/ratio \
${experimental_srcdir}/string_view \
......
// <experimental/algorithm> -*- C++ -*-
// Copyright (C) 2014 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 3, 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.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file experimental/algorithm
* This is a TS C++ Library header.
*/
#ifndef _GLIBCXX_EXPERIMENTAL_ALGORITHM
#define _GLIBCXX_EXPERIMENTAL_ALGORITHM 1
#pragma GCC system_header
#if __cplusplus <= 201103L
# include <bits/c++14_warning.h>
#else
#include <algorithm>
#include <random>
namespace std _GLIBCXX_VISIBILITY(default)
{
namespace experimental
{
inline namespace fundamentals_v1
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _ForwardIterator, typename _Searcher>
inline _ForwardIterator
search(_ForwardIterator __first, _ForwardIterator __last,
const _Searcher& __searcher)
{ return __searcher(__first, __last); }
#define __cpp_lib_experimental_sample 201402
/// Reservoir sampling algorithm.
template<typename _InputIterator, typename _RandomAccessIterator,
typename _Size, typename _UniformRandomNumberGenerator>
_RandomAccessIterator
__sample(_InputIterator __first, _InputIterator __last, input_iterator_tag,
_RandomAccessIterator __out, random_access_iterator_tag,
_Size __n, _UniformRandomNumberGenerator&& __g)
{
using __distrib_type = std::uniform_int_distribution<_Size>;
using __param_type = typename __distrib_type::param_type;
__distrib_type __d{};
_Size __sample_sz = 0;
while (__first != __last && __sample_sz != __n)
__out[__sample_sz++] = *__first++;
for (auto __pop_sz = __sample_sz; __first != __last;
++__first, ++__pop_sz)
{
const auto __k = __d(__g, __param_type{0, __pop_sz});
if (__k < __n)
__out[__k] = *__first;
}
return __out + __sample_sz;
}
/// Selection sampling algorithm.
template<typename _ForwardIterator, typename _OutputIterator, typename _Cat,
typename _Size, typename _UniformRandomNumberGenerator>
_OutputIterator
__sample(_ForwardIterator __first, _ForwardIterator __last,
forward_iterator_tag,
_OutputIterator __out, _Cat,
_Size __n, _UniformRandomNumberGenerator&& __g)
{
using __distrib_type = std::uniform_int_distribution<_Size>;
using __param_type = typename __distrib_type::param_type;
__distrib_type __d{};
_Size __unsampled_sz = std::distance(__first, __last);
for (__n = std::min(__n, __unsampled_sz); __n != 0; ++__first)
if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
{
*__out++ = *__first;
--__n;
}
return __out;
}
/// Take a random sample from a population.
template<typename _PopulationIterator, typename _SampleIterator,
typename _Distance, typename _UniformRandomNumberGenerator>
_SampleIterator
sample(_PopulationIterator __first, _PopulationIterator __last,
_SampleIterator __out, _Distance __n,
_UniformRandomNumberGenerator&& __g)
{
using __pop_cat = typename
std::iterator_traits<_PopulationIterator>::iterator_category;
using __samp_cat = typename
std::iterator_traits<_SampleIterator>::iterator_category;
static_assert(
__or_<is_convertible<__pop_cat, forward_iterator_tag>,
is_convertible<__samp_cat, random_access_iterator_tag>>::value,
"output range must use a RandomAccessIterator when input range"
" does not meet the ForwardIterator requirements");
static_assert(is_integral<_Distance>::value,
"sample size must be an integer type");
return std::experimental::__sample(
__first, __last, __pop_cat{}, __out, __samp_cat{},
__n, std::forward<_UniformRandomNumberGenerator>(__g));
}
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace fundamentals_v1
} // namespace experimental
} // namespace std
#endif // C++14
#endif // _GLIBCXX_EXPERIMENTAL_ALGORITHM
// Copyright (C) 2014 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 3, 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 COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++14" }
#include <experimental/algorithm>
#include <iterator>
#include <sstream>
#include <forward_list>
#include <vector>
#include <testsuite_hooks.h>
std::mt19937 rng;
using std::experimental::sample;
using std::istream_iterator;
using std::ostream_iterator;
void
test01()
{
const int pop[] = { 1, 2 };
int samp[10] = { };
// population smaller than desired sample size
auto it = sample(pop, pop + 2, samp, 10, rng);
VERIFY( it == samp + 2 );
VERIFY( std::accumulate(samp, samp + 10, 0) == 3 );
}
void
test02()
{
const int pop[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int samp[10] = { };
auto it = sample(pop, std::end(pop), samp, 10, rng);
VERIFY( it == samp + 10 );
std::sort(samp, it);
auto it2 = std::unique(samp, it);
VERIFY( it2 == it );
}
void
test03()
{
std::istringstream pop("0 1 2 3 4 5 6 7 8 9");
int samp[5] = { };
// input iterator for population
auto it = sample(istream_iterator<int>{pop}, {}, samp, 5, rng);
VERIFY( it == samp + 5 );
std::sort(samp, it);
auto it2 = std::unique(samp, it);
VERIFY( it2 == it );
}
void
test04()
{
std::forward_list<int> pop{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::stringstream samp;
// forward iterator for population and output iterator for result
sample(pop.begin(), pop.end(), ostream_iterator<int>{samp, " "}, 5, rng);
// samp.rdbuf()->pubseekoff(0, std::ios::beg);
std::vector<int> v(istream_iterator<int>{samp}, {});
VERIFY( v.size() == 5 );
std::sort(v.begin(), v.end());
auto it = std::unique(v.begin(), v.end());
VERIFY( it == v.end() );
}
int
main()
{
test01();
test02();
test03();
test04();
}
// Copyright (C) 2014 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 3, 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 COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++14" }
#include <experimental/algorithm>
#include <testsuite_hooks.h>
struct nocopy
{
nocopy() = default;
nocopy(const nocopy&) = delete;
nocopy& operator=(const nocopy&) = delete;
int* operator()(int* f, int* l) const { return f; }
};
void
test01()
{
int i[] = { 1, 2 };
auto res = std::experimental::search(i, i + 2, nocopy{});
VERIFY( res == i );
}
int
main()
{
test01();
}
// { dg-options "-std=gnu++14" }
// { dg-do compile }
#include <experimental/optional>
#include <experimental/string_view>
#if !__has_include(<experimental/tuple>)
# error "<experimental/tuple>"
#endif
#if !__has_include(<experimental/type_traits>)
# error "<experimental/type_traits>"
#endif
#if !__has_include(<experimental/ratio>)
# error "<experimental/ratio>"
#endif
#if !__has_include(<experimental/chrono>)
# error "<experimental/chrono>"
#endif
#if !__has_include(<experimental/system_error>)
# error "<experimental/system_error>"
#endif
#if !__has_include(<experimental/functional>)
# error "<experimental/functional>"
#endif
#if !__has_include(<experimental/optional>)
# error "<experimental/optional>"
#endif
//#if !__has_include(<experimental/net>)
//# error "<experimental/net>"
//#endif
#if !__has_include(<experimental/any>)
# error "<experimental/any>"
#endif
#if !__has_include(<experimental/string_view>)
# error "<experimental/string_view>"
#endif
//#if !__has_include(<experimental/memory>)
//# error "<experimental/memory>"
//#endif
//#if !__has_include(<experimental/memory_resource>)
//# error "<experimental/memory_resource>"
//#endif
#if !__has_include(<experimental/string_view>)
# error "<experimental/string_view>"
#endif
//#if !__has_include(<experimental/future>)
//# error "<experimental/future>"
//#endif
#if !__has_include(<experimental/tuple>)
# error "<experimental/tuple>"
#if !__has_include(<experimental/algorithm>)
# error "<experimental/algorithm>"
#endif
//#if !__has_include(<experimental/net>)
//# error "<experimental/net>"
//#endif
// Copyright (C) 2014 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 3, 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 COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++14" }
#include <experimental/functional>
#include <testsuite_hooks.h>
int func(int, char) { return 0; }
struct F
{
bool operator()() { return false; }
bool operator()() const { return true; }
bool operator()(int) { return false; }
bool operator()(int) volatile { return true; }
};
void
test01()
{
using std::experimental::not_fn;
auto f1 = not_fn(func);
VERIFY( f1(1, '2') == true );
auto f2 = not_fn( [] { return true; } );
VERIFY( f2() == false );
auto f3 = not_fn( F{} );
VERIFY( f3() == true );
VERIFY( f3(1) == true );
const auto f4 = f3;
VERIFY( f4() == false );
volatile auto f5 = f3;
VERIFY( f5(1) == false );
}
int
main()
{
test01();
}
// Copyright (C) 2014 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 3, 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 COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++14" }
#include <experimental/functional>
#include <cstring>
#ifdef _GLIBCXX_USE_WCHAR_T
# include <cwchar>
#endif
#include <testsuite_hooks.h>
using std::experimental::make_default_searcher;
using std::experimental::make_boyer_moore_searcher;
using std::experimental::make_boyer_moore_horspool_searcher;
void
test01()
{
const char s[] = { 'a', (char)-97, 'a', '\0' };
const char* needles[] = {
s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd"
};
const char* haystacks[] = {
s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd",
"aaaaaaa", "aabaa", "aaacab", "cdabcdab", "abcdabcd", "xyzabcdxyz"
};
for (auto n : needles)
{
auto ne = n + std::strlen(n);
auto d = make_default_searcher(n, ne);
auto bm = make_boyer_moore_searcher(n, ne);
auto bmh = make_boyer_moore_horspool_searcher(n, ne);
for (auto h : haystacks)
{
auto he = h + std::strlen(h);
auto res = std::search(h, he, n, ne);
auto d_res = d(h, he);
VERIFY( d_res == res );
auto bm_res = bm(h, he);
VERIFY( bm_res == res );
auto bmh_res = bmh(h, he);
VERIFY( bmh_res == res );
}
}
}
void
test02()
{
#ifdef _GLIBCXX_USE_WCHAR_T
const wchar_t s[] = { L'a', (wchar_t)-97, L'a', L'\0' };
const wchar_t* needles[] = {
s, L"", L"a", L"aa", L"aaa", L"ab", L"cd", L"abcd", L"abcdabcd", L"abcabcd"
};
const wchar_t* haystacks[] = {
s, L"", L"a", L"aa", L"aaa", L"ab", L"cd", L"abcd", L"abcdabcd", L"abcabcd",
L"aaaaaaa", L"aabaa", L"aaacab", L"cdabcdab", L"abcdabcd", L"xyzabcdxyz"
};
for (auto n : needles)
{
auto ne = n + std::wcslen(n);
auto d = make_default_searcher(n, ne);
auto bm = make_boyer_moore_searcher(n, ne);
auto bmh = make_boyer_moore_horspool_searcher(n, ne);
for (auto h : haystacks)
{
auto he = h + std::wcslen(h);
auto res = std::search(h, he, n, ne);
auto d_res = d(h, he);
VERIFY( d_res == res );
auto bm_res = bm(h, he);
VERIFY( bm_res == res );
auto bmh_res = bmh(h, he);
VERIFY( bmh_res == res );
}
}
#endif
}
void
test03()
{
// custom predicate
struct
{
static unsigned char
norm(unsigned char c) { return std::isalnum(c) ? c : '#'; }
// equality
bool operator()(char l, char r) const { return norm(l) == norm(r); }
// hash
std::size_t operator()(char c) const { return std::hash<char>{}(norm(c)); }
} eq;
const char* needle = " foo 123 ";
const char* haystack = "*****foo*123******";
const char* ne = needle + std::strlen(needle);
const char* he = haystack + std::strlen(haystack);
auto d = make_default_searcher(needle, ne, eq);
auto bm = make_boyer_moore_searcher(needle, ne, eq, eq);
auto bmh = make_boyer_moore_horspool_searcher(needle, ne, eq, eq);
auto res = std::search(haystack, he, needle, ne, eq);
auto d_res = d(haystack, he);
VERIFY( d_res == res );
auto bm_res = bm(haystack, he);
VERIFY( bm_res == res );
auto bmh_res = bmh(haystack, he);
VERIFY( bmh_res == res );
}
int
main()
{
test01();
test02();
test03();
}
// Copyright (C) 2014 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 3, 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 COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++14" }
// { dg-do compile }
#include <experimental/functional>
// These tests are rather simple, the front-end tests already test
// variable templates, and the library tests for the underlying
// traits are more elaborate. These are just simple sanity tests.
int f(int);
using B = decltype(std::bind(f, std::placeholders::_1));
static_assert(!std::experimental::is_bind_expression_v<int>
&& !std::is_bind_expression<int>::value, "");
static_assert(std::experimental::is_bind_expression_v<B>
&& std::is_bind_expression<B>::value, "");
using PH = decltype(std::placeholders::_1);
static_assert(!std::experimental::is_placeholder_v<int>
&& !std::is_placeholder<int>::value, "");
static_assert(std::experimental::is_placeholder_v<PH>
&& std::is_placeholder<PH>::value, "");
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