From 8c101cea6f66833ae5c4f5da4bbb1b5fc889b78f Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sun, 25 Feb 2024 19:29:06 +0000 Subject: [PATCH] [+] IRandomDevice::ShuffleIterators [+] IRandomDevice::ShuffleList [+] AuRNG::RngShuffleIterators [+] AuRNG::RngShuffleList --- Include/Aurora/RNG/IRandomDevice.hpp | 28 ++++++++++++++++++++++++++-- Include/Aurora/RNG/RNG.hpp | 28 ++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/Include/Aurora/RNG/IRandomDevice.hpp b/Include/Aurora/RNG/IRandomDevice.hpp index 6be99e53..5b4de86c 100644 --- a/Include/Aurora/RNG/IRandomDevice.hpp +++ b/Include/Aurora/RNG/IRandomDevice.hpp @@ -103,16 +103,40 @@ namespace Aurora::RNG } template - inline T &NextVector(AuList &items) + inline T &NextList(AuList &items) { return *NextRange(items); } template - inline const T &NextVector(const AuList &items) + inline const T &NextList(const AuList &items) { return *NextRange(items); } + + template + inline void ShuffleIterators(T begin, T end) + { + AU_DEBUG_MEMCRUNCH; + + auto uCount = std::distance(begin, end); + auto nextIndexArray = this->NextArrayU32Range(uCount, 0, uCount - 1); + + for (AU_ITERATE_N(i, AuUInt(uCount))) + { + auto nextItrA = begin; + auto nextItrB = begin; + std::advance(nextItrA, i); + std::advance(nextItrB, nextIndexArray[i]); + AuSwap(*nextItrA, *nextItrB); + } + } + + template + inline void ShuffleList(AuList &list) + { + this->ShuffleIterators(list.begin(), list.end()); + } }; AUKN_SHARED_SOO2(Random, IRandomDevice, kSizeRNGDevice, diff --git a/Include/Aurora/RNG/RNG.hpp b/Include/Aurora/RNG/RNG.hpp index f6f303eb..c0a28b0a 100644 --- a/Include/Aurora/RNG/RNG.hpp +++ b/Include/Aurora/RNG/RNG.hpp @@ -124,14 +124,38 @@ namespace Aurora::RNG } template - static auline T &RngVector(AuList &items) + static auline T &RngList(AuList &items) { return *RngRange(items); } template - static auline const T &RngVector(const AuList &items) + static auline const T &RngList(const AuList &items) { return *RngRange(items); } + + template + inline void RngShuffleIterators(T begin, T end) + { + AU_DEBUG_MEMCRUNCH; + + auto uCount = std::distance(begin, end); + auto nextIndexArray = RngArrayU32Range(uCount, 0, uCount - 1); + + for (AU_ITERATE_N(i, AuUInt(uCount))) + { + auto nextItrA = begin; + auto nextItrB = begin; + std::advance(nextItrA, i); + std::advance(nextItrB, nextIndexArray[i]); + AuSwap(*nextItrA, *nextItrB); + } + } + + template + inline void RngShuffleList(AuList &list) + { + RngShuffleIterators(list.begin(), list.end()); + } } \ No newline at end of file