Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
shuffle.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <Common/assertion.h>
6
7#include <cstddef>
8#include <algorithm>
9#include <iterator>
10#include <utility>
11
12namespace ph::math
13{
14
15namespace detail::shuffle
16{
17
18template<typename T>
20{
21 void operator () (T& a, T& b) const
22 {
23 // enable ADL
24 using std::swap;
25
26 swap(a, b);
27 }
28};
29
30}// end namespace detail::shuffle
31
32template<typename IndexPairConsumer>
34 const std::size_t beginIndex,
35 const std::size_t endIndex,
36 IndexPairConsumer consumer)
37{
38 PH_ASSERT_LE(beginIndex, endIndex);
39
40 for(std::size_t i = beginIndex; i < endIndex; ++i)
41 {
42 const std::size_t ri = math::Random::index(i, endIndex);
43 consumer(i, ri);
44 }
45}
46
47template<
48 typename RandomIterator,
51 RandomIterator begin, RandomIterator end,
52 Swapper swapper = Swapper())
53{
54 PH_ASSERT(begin <= end);
55 const auto NUM_ELEMENTS = static_cast<std::size_t>(std::distance(begin, end));
56
58 0, NUM_ELEMENTS,
59 [=, &swapper](
60 const std::size_t indexA, const std::size_t indexB)
61 {
62 swapper(begin[indexA], begin[indexB]);
63 });
64}
65
66}// end namespace ph::math
static std::size_t index(std::size_t lowerBound, std::size_t upperBound)
Get a uniform random integer value in [lowerBound, upperBound).
Definition Random.ipp:23
Math functions and utilities.
Definition TransformInfo.h:10
void shuffle_durstenfeld_index_pairs(const std::size_t beginIndex, const std::size_t endIndex, IndexPairConsumer consumer)
Definition shuffle.h:33
void shuffle_durstenfeld(RandomIterator begin, RandomIterator end, Swapper swapper=Swapper())
Definition shuffle.h:50
void operator()(T &a, T &b) const
Definition shuffle.h:21