13 #include "oops/util/Random.h"
19 opsCompatibilityMode_(opsCompatibilityMode) {
31 }
else if (numIds > 1) {
39 auto orderedCategory = [&](
size_t index) {
return categories[
orderedIds_[index]]; };
42 ptrdiff_t lastIndexInGroup = -1;
43 ptrdiff_t lastIndexInLastGroup = -1;
44 while (lastIndexInGroup + 1 < numIds) {
45 const size_t firstIndexInGroup =
encodedGroups_[lastIndexInGroup + 1];
46 if (firstIndexInGroup + 1 >= numIds)
53 [&categories](
size_t id) { return categories[id]; });
55 std::stable_sort(
orderedIds_.begin() + firstIndexInGroup,
57 [&categories](
size_t idA,
size_t idB)
58 { return categories[idA] < categories[idB]; });
62 size_t newFirstIndex = firstIndexInGroup;
63 for (
size_t newLastIndex = firstIndexInGroup;
64 newLastIndex <= lastIndexInGroup;
66 if (newLastIndex != lastIndexInGroup &&
67 orderedCategory(newLastIndex) == orderedCategory(newLastIndex + 1))
70 if (newLastIndex > newFirstIndex) {
74 lastIndexInLastGroup = newLastIndex;
76 newFirstIndex = newLastIndex + 1;
80 if (lastIndexInLastGroup + 1 < numIds)
86 std::vector<size_t>::iterator nonConstGroupBegin =
88 std::vector<size_t>::iterator nonConstGroupEnd =
90 util::shuffle(nonConstGroupBegin, nonConstGroupEnd);
95 std::vector<size_t> dummy;
96 util::shuffle(dummy.begin(), dummy.end(), seed, force);
A range of indices of all array elements belonging to a particular equivalence class.
void groupByImpl(const std::vector< T > &categories)
MultiElementGroupRange multiElementGroups() const
Return the range of equivalence classes consisting of more than one element.
void shuffleGroups()
Randomly shuffle the elements of each equivalence class.
std::vector< size_t > orderedIds_
Indices of elements of the partitioned array ordered by equivalence class.
RecursiveSplitter(size_t numIds, bool opsCompatibilityMode=false)
Initialize partitioning of an array of numIds elements.
std::vector< size_t > encodedGroups_
Encoded locations of multi-element equivalence classes in orderedIds_.
bool opsCompatibilityMode_
void initializeEncodedGroups()
void setSeed(unsigned int seed, bool force)
Initialise the random number generator used by shuffleGroups() with a seed.
void metOfficeSort(RandomIt first, RandomIt last, const UnaryOperation &key)
Sort the range [first, last) in the order of ascending keys using the same algorithm as the Ops_Integ...