Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Mark LWG 2716 as complete - shuffle and sample disallows lvalue URNGs.
Browse files Browse the repository at this point in the history
Libc++'s implementation of shuffle and sample already support lvalue and rvalue
RNG's. This patch adds tests for both categories and marks the issue as complete.

This patch also contains drive-by change for std::experimental::sample which
improves the diagnostics produced when the correct iterator categories are
not supplied.


git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@279947 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
EricWF committed Aug 28, 2016
1 parent 113315b commit 17c38db
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
3 changes: 3 additions & 0 deletions include/experimental/algorithm
Expand Up @@ -107,6 +107,9 @@ _SampleIterator sample(_PopulationIterator __first,
_PopCategory;
typedef typename iterator_traits<_PopulationIterator>::difference_type
_Difference;
static_assert(__is_forward_iterator<_PopulationIterator>::value ||
__is_random_access_iterator<_SampleIterator>::value,
"SampleIterator must meet the requirements of RandomAccessIterator");
typedef typename common_type<_Distance, _Difference>::type _CommonType;
_LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
return _VSTD_LFTS::__sample(
Expand Down
4 changes: 2 additions & 2 deletions test/libcxx/iterators/trivial_iterators.pass.cpp
Expand Up @@ -42,7 +42,7 @@ class my_input_iterator
{
It it_;

template <class U> friend class input_iterator;
template <class U> friend class my_input_iterator;
public:
typedef my_input_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
Expand All @@ -55,7 +55,7 @@ class my_input_iterator
my_input_iterator() : it_() {}
explicit my_input_iterator(It it) : it_(it) {}
template <class U>
my_input_iterator(const input_iterator<U>& u) :it_(u.it_) {}
my_input_iterator(const my_input_iterator<U>& u) :it_(u.it_) {}

reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
Expand Down
Expand Up @@ -29,7 +29,7 @@ int main()
std::shuffle(ia, ia+sa, g);
LIBCPP_ASSERT(std::equal(ia, ia+sa, ia1));
assert(std::is_permutation(ia, ia+sa, ia1));
std::shuffle(ia, ia+sa, g);
std::shuffle(ia, ia+sa, std::move(g));
LIBCPP_ASSERT(std::equal(ia, ia+sa, ia2));
assert(std::is_permutation(ia, ia+sa, ia2));
}
Expand Up @@ -32,5 +32,8 @@ template <class PopulationIterator, class SampleIterator> void test() {
}

int main() {
// expected-error@experimental/algorithm:* {{static_assert failed "SampleIterator must meet the requirements of RandomAccessIterator"}}
// expected-error@experimental/algorithm:* 2 {{does not provide a subscript operator}}
// expected-error@experimental/algorithm:* {{invalid operands}}
test<input_iterator<int *>, output_iterator<int *> >();
}
Expand Up @@ -46,8 +46,8 @@ template <>
struct TestExpectations<std::input_iterator_tag>
: public ReservoirSampleExpectations {};

template <template<class> class PopulationIteratorType, class PopulationItem,
template<class> class SampleIteratorType, class SampleItem>
template <template<class...> class PopulationIteratorType, class PopulationItem,
template<class...> class SampleIteratorType, class SampleItem>
void test() {
typedef PopulationIteratorType<PopulationItem *> PopulationIterator;
typedef SampleIteratorType<SampleItem *> SampleIterator;
Expand All @@ -68,13 +68,13 @@ void test() {
assert(std::equal(oa, oa + os, oa1));
end = std::experimental::sample(PopulationIterator(ia),
PopulationIterator(ia + is),
SampleIterator(oa), os, g);
SampleIterator(oa), os, std::move(g));
assert(end.base() - oa == std::min(os, is));
assert(std::equal(oa, oa + os, oa2));
}

template <template<class> class PopulationIteratorType, class PopulationItem,
template<class> class SampleIteratorType, class SampleItem>
template <template<class...> class PopulationIteratorType, class PopulationItem,
template<class...> class SampleIteratorType, class SampleItem>
void test_empty_population() {
typedef PopulationIteratorType<PopulationItem *> PopulationIterator;
typedef SampleIteratorType<SampleItem *> SampleIterator;
Expand All @@ -88,8 +88,8 @@ void test_empty_population() {
assert(end.base() == oa);
}

template <template<class> class PopulationIteratorType, class PopulationItem,
template<class> class SampleIteratorType, class SampleItem>
template <template<class...> class PopulationIteratorType, class PopulationItem,
template<class...> class SampleIteratorType, class SampleItem>
void test_empty_sample() {
typedef PopulationIteratorType<PopulationItem *> PopulationIterator;
typedef SampleIteratorType<SampleItem *> SampleIterator;
Expand All @@ -103,8 +103,8 @@ void test_empty_sample() {
assert(end.base() == oa);
}

template <template<class> class PopulationIteratorType, class PopulationItem,
template<class> class SampleIteratorType, class SampleItem>
template <template<class...> class PopulationIteratorType, class PopulationItem,
template<class...> class SampleIteratorType, class SampleItem>
void test_small_population() {
// The population size is less than the sample size.
typedef PopulationIteratorType<PopulationItem *> PopulationIterator;
Expand Down
2 changes: 1 addition & 1 deletion www/cxx1z_status.html
Expand Up @@ -299,7 +299,7 @@ <h3>Library Working group Issues Status</h3>
<tr><td><a href="http://wg21.link/LWG2709">2709</a></td><td>offsetof is unnecessarily imprecise</td><td>Oulu</td><td></td></tr>
<tr><td><a href="http://wg21.link/LWG2710">2710</a></td><td>"Effects: Equivalent to ..." doesn't count "Synchronization:" as determined semantics</td><td>Oulu</td><td>Complete</td></tr>
<tr><td><a href="http://wg21.link/LWG2711">2711</a></td><td>path is convertible from approximately everything under the sun</td><td>Oulu</td><td>Complete</td></tr>
<tr><td><a href="http://wg21.link/LWG2716">2716</a></td><td>Specification of shuffle and sample disallows lvalue URNGs</td><td>Oulu</td><td></td></tr>
<tr><td><a href="http://wg21.link/LWG2716">2716</a></td><td>Specification of shuffle and sample disallows lvalue URNGs</td><td>Oulu</td><td>Complete</td></tr>
<tr><td><a href="http://wg21.link/LWG2718">2718</a></td><td>Parallelism bug in [algorithms.parallel.exec] p2</td><td>Oulu</td><td></td></tr>
<tr><td><a href="http://wg21.link/LWG2719">2719</a></td><td>permissions function should not be noexcept due to narrow contract</td><td>Oulu</td><td>Complete</td></tr>
<tr><td><a href="http://wg21.link/LWG2720">2720</a></td><td>permissions function incorrectly specified for symlinks</td><td>Oulu</td><td>Complete</td></tr>
Expand Down

0 comments on commit 17c38db

Please sign in to comment.