trsl logo
random_permutation_iterator.hpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2011.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
8 #ifndef TRSL_RANDOM_PERMUTATION_ITERATOR_HPP
9 #define TRSL_RANDOM_PERMUTATION_ITERATOR_HPP
10 
12 #include <trsl/common.hpp>
13 #include <trsl/error_handling.hpp>
14 
15 namespace trsl
16 {
17 
38  template<class ElementIterator>
39  reorder_iterator<ElementIterator>
40  random_permutation_iterator(ElementIterator first,
41  ElementIterator last,
42  unsigned permutationSize)
43  {
44  ptrdiff_t size = std::distance(first, last);
45  if (size < 0)
46  throw bad_parameter_value(
47  "random_permutation_iterator: "
48  "bad input range.");
49  if (permutationSize > unsigned(size))
50  throw bad_parameter_value(
51  "random_permutation_iterator: "
52  "parameter permutationSize out of range.");
53 
54  typedef
55  typename reorder_iterator<ElementIterator>::index_container
56  index_container;
57  typedef
58  typename reorder_iterator<ElementIterator>::index_container_ptr
59  index_container_ptr;
60  typedef
61  typename reorder_iterator<ElementIterator>::index_t
62  index_t;
63 
64  index_container_ptr index_collection(new index_container);
65 
66  index_collection->resize(size);
67  for (index_t i = 0; i < index_t(size); ++i)
68  (*index_collection)[i] = i;
69  if (permutationSize == unsigned(size))
70  std::random_shuffle(index_collection->begin(),
71  index_collection->end(),
73  else
74  {
75  detail::partial_random_shuffle(index_collection->begin(),
76  index_collection->begin()+permutationSize,
77  index_collection->end(),
79  index_collection->resize(permutationSize);
80  }
81 
82  return reorder_iterator<ElementIterator>(first, index_collection);
83  }
84 
101  template<class ElementIterator>
102  reorder_iterator<ElementIterator>
103  random_permutation_iterator(ElementIterator first,
104  ElementIterator last)
105  {
106  return random_permutation_iterator(first,
107  last,
108  std::distance(first, last));
109  }
110 
111 } // namespace trsl
112 
113 #endif // include guard
common.hpp
trsl::bad_parameter_value
Thrown when a TRSL component receives a parameter that has a forbidden value.
Definition: error_handling.hpp:27
trsl
Public namespace.
Definition: common.hpp:29
error_handling.hpp
trsl::rand_gen::uniform_int
unsigned int uniform_int(unsigned int n)
Returns an integer in [0,n[. Used internally.
Definition: common.hpp:68
reorder_iterator.hpp
trsl::reorder_iterator
Provides an iterator over a permutation of a range.
Definition: reorder_iterator.hpp:31
trsl::random_permutation_iterator
reorder_iterator< ElementIterator > random_permutation_iterator(ElementIterator first, ElementIterator last, unsigned permutationSize)
Constructs a reorder_iterator that will iterate through a random subset of size permutationSize of a ...
Definition: random_permutation_iterator.hpp:40
© Copyright 2007-2011 Renaud Detry.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt.)
Revised Wed Jan 8 2020 14:43:31.