PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Permute.h
Go to the documentation of this file.
1
8
9#ifndef PBAT_COMMON_PERMUTE_H
10#define PBAT_COMMON_PERMUTE_H
11
12#include <concepts>
13#include <iterator>
14
15namespace pbat::common {
16
35template <
36 std::random_access_iterator TValuesBegin,
37 std::random_access_iterator TValuesEnd,
38 std::random_access_iterator TPermutationBegin>
39void Permute(TValuesBegin vb, TValuesEnd ve, TPermutationBegin pb)
40{
41 using PermutationIndex = std::iterator_traits<TPermutationBegin>::value_type;
42 static_assert(
43 std::is_integral_v<PermutationIndex> and std::is_signed_v<PermutationIndex>,
44 "Permutation index must be signed integer");
45 auto n = std::distance(vb, ve);
46 using PtrDiffType = decltype(n);
47 for (PtrDiffType i = 0; i < n; ++i)
48 {
49 auto pi = *(pb + i);
50 if (pi < 0)
51 continue;
52 auto value = *(vb + i);
53 auto xi = i;
54 while (pi != i)
55 {
56 *(pb + xi) -= n;
57 *(vb + xi) = *(vb + pi);
58 xi = pi;
59 pi = *(pb + xi);
60 }
61 *(vb + xi) = value;
62 *(pb + xi) -= n;
63 }
64 for (PtrDiffType i = 0; i < n; ++i)
65 {
66 *(pb + i) += n;
67 }
68}
69
70} // namespace pbat::common
71
72#endif // PBAT_COMMON_PERMUTE_H
Common functionality.
Definition ArgSort.h:20
void Permute(TValuesBegin vb, TValuesEnd ve, TPermutationBegin pb)
Permute the values in-place according to the permutation.
Definition Permute.h:39