Index: include/experimental/algorithm =================================================================== --- include/experimental/algorithm +++ include/experimental/algorithm @@ -0,0 +1,62 @@ +// -*- C++ -*- +//===-------------------------- algorithm ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_EXPERIMENTAL_ALGORITHM +#define _LIBCPP_EXPERIMENTAL_ALGORITHM + +/* + experimental/algorithm synopsis + +#include + +namespace std { +namespace experimental { +inline namespace fundamentals_v1 { + +template +ForwardIterator search(ForwardIterator first, ForwardIterator last, + const Searcher &searcher); +template +SampleIterator sample(PopulationIterator first, PopulationIterator last, + SampleIterator out, Distance n, + UniformRandomNumberGenerator &&g); + +} // namespace fundamentals_v1 +} // namespace experimental +} // namespace std + +*/ + +#include +#include + +_LIBCPP_BEGIN_NAMESPACE_LFTS + +template +_SampleIterator sample(_PopulationIterator __first, _PopulationIterator __last, + _SampleIterator __out, _Distance __n, + _UniformRandomNumberGenerator &&__g) { + _Distance __k = 0; + for (; __first != __last && __k < __n; ++__first, (void)++__k) + __out[__k] = *__first; + _Distance __sz = __k; + for (; __first != __last; ++__first, (void)++__k) { + _Distance __r = _VSTD::uniform_int_distribution<>(0, __k)(__g); + if (__r < __sz) + __out[__r] = *__first; + } + return __out + _VSTD::min(__n, __k); +} + +_LIBCPP_END_NAMESPACE_LFTS + +#endif /* _LIBCPP_EXPERIMENTAL_ALGORITHM */ Index: test/std/experimental/algorithms/alg.random.sample/sample.pass.cpp =================================================================== --- test/std/experimental/algorithms/alg.random.sample/sample.pass.cpp +++ test/std/experimental/algorithms/alg.random.sample/sample.pass.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// SampleIterator sample(PopulationIterator first, PopulationIterator last, +// SampleIterator out, Distance n, +// UniformRandomNumberGenerator &&g); + +#include +#include +#include + +int main() { + int ia[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + const unsigned is = sizeof(ia) / sizeof(ia[0]); + const unsigned os = 4; + int oa[os]; + int oa1[os] = {10, 5, 9, 4}; + int oa2[os] = {5, 2, 10, 4}; + std::minstd_rand g; + int * end = std::experimental::sample(ia, ia + is, oa, os, g); + assert(end - oa == std::min(os, is)); + assert(std::equal(oa, oa + os, oa1)); + end = std::experimental::sample(ia, ia + is, oa, os, g); + assert(end - oa == std::min(os, is)); + assert(std::equal(oa, oa + os, oa2)); +} Index: test/std/experimental/algorithms/alg.random.sample/sample2.pass.cpp =================================================================== --- test/std/experimental/algorithms/alg.random.sample/sample2.pass.cpp +++ test/std/experimental/algorithms/alg.random.sample/sample2.pass.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// SampleIterator sample(PopulationIterator first, PopulationIterator last, +// SampleIterator out, Distance n, +// UniformRandomNumberGenerator &&g); + +#include +#include +#include + +int main() { + int ia[] = {1, 2, 3, 4, 5}; + const unsigned is = sizeof(ia) / sizeof(ia[0]); + const unsigned os = 8; + int oa[os]; + int oa1[] = {1, 2, 3, 4, 5}; + std::minstd_rand g; + int *end = std::experimental::sample(ia, ia + is, oa, os, g); + assert(end - oa == std::min(os, is)); + assert(std::equal(oa, end, oa1)); +}