From 5d6d60e5c4cbc4f133602733ab1dc3886a976b66 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 26 Nov 2020 20:30:05 +0000 Subject: [PATCH] in-place un/shuffling works --- lean/Makefile | 2 +- lean/include/map.h | 4 +++ lean/include/shuffle.hpp | 36 +++++++++++++++++++ lean/src/main.c | 8 +++-- lean/src/map.c | 4 +-- lean/src/work.cpp | 75 +++++++++++++++++++++++++++++++++++++++- 6 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 lean/include/shuffle.hpp diff --git a/lean/Makefile b/lean/Makefile index 1c81948..11b9bdc 100644 --- a/lean/Makefile +++ b/lean/Makefile @@ -16,7 +16,7 @@ CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors CFLAGS += $(COMMON_FLAGS) --std=gnu11 CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -fno-exceptions -LDFLAGS += +LDFLAGS += -lfmt RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) diff --git a/lean/include/map.h b/lean/include/map.h index 244be43..307481a 100644 --- a/lean/include/map.h +++ b/lean/include/map.h @@ -24,6 +24,7 @@ void* map_and_then(const char* file, map_cb callback, void* user); #ifdef __cplusplus } #include +#include "reinterpret.h" #include namespace mm { struct mmap { @@ -52,6 +53,9 @@ namespace mm { } } + inline const span as_span() const { return span(as_ptr(), size()); } + inline span as_span() { return span(as_ptr(), size()); } + inline const std::uint8_t* as_ptr() const { return (const std::uint8_t*)inner.ptr; } inline std::uint8_t* as_ptr() { return (std::uint8_t*)inner.ptr; } diff --git a/lean/include/shuffle.hpp b/lean/include/shuffle.hpp new file mode 100644 index 0000000..f56724f --- /dev/null +++ b/lean/include/shuffle.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include +#include + +namespace rng { + template + inline void shuffle(R& rng, span span) + { + fmt::print(" -> shuffling {} objects...", span.size()); + for(std::size_t i=span.size()-1;i>0;i--) + { + auto j = rng.next_long(i); + std::swap(span[i], span[j]); + } + fmt::print(" OK\n"); + } + + template + inline void unshuffle(R& rng, span span) + { + std::vector rng_values; + + fmt::print(" -> unshuffling {} objects...", span.size()); + for(std::size_t i=span.size()-1;i>0;i--) + rng_values.push_back(rng.next_long(i)); + + for(std::size_t i=1;i +#include +#include + +#include + #include -#include #include +#include +#include +#include + +#include + +template +std::tuple minmax_t(const span& array, Fn keep) +{ + T lowest; + T highest; + for(std::size_t i=0;i highest) highest = item; + } + } + fmt::print("MMX {}, {}\n", lowest, highest); + return {lowest, highest}; +} + +template +inline std::tuple minmax_t(const span& array) +{ + return minmax_t(array, [](T _val) { return true; }); +} namespace work { @@ -8,6 +44,35 @@ namespace work template int xshuffle_ip(const char* file) { + mm::mmap map(file); + + if constexpr(unshuffle) + { + auto [byte_l, byte_h] = minmax_t(map.as_span().reinterpret()); + rng::drng drng((std::int32_t) ((0xfffa << 16) | (byte_l<<7) | byte_h )); + rng::unshuffle(drng, map.as_span()); + + auto [float_l, float_h] = minmax_t(map.as_span().reinterpret(), [](float f) -> bool { return !( (f!=f) || f < -FLT_MAX || f > FLT_MAX); }); + rng::frng frng(float_l, float_h); + rng::unshuffle(frng, map.as_span().reinterpret()); + + auto [long_l, long_h] = minmax_t(map.as_span().reinterpret()); + rng::xoroshiro128plus xorng(*(const std::uint64_t*)&long_l, *(const std::uint64_t*)&long_h); + rng::unshuffle(xorng, map.as_span().reinterpret()); + } else { + auto [long_l, long_h] = minmax_t(map.as_span().reinterpret()); + rng::xoroshiro128plus xorng(*(const std::uint64_t*)&long_l, *(const std::uint64_t*)&long_h); + rng::shuffle(xorng, map.as_span().reinterpret()); + + auto [float_l, float_h] = minmax_t(map.as_span().reinterpret(), [](float f) -> bool { return !( (f!=f) || f < -FLT_MAX || f > FLT_MAX); }); + rng::frng frng(float_l, float_h); + rng::shuffle(frng, map.as_span().reinterpret()); + + auto [byte_l, byte_h] = minmax_t(map.as_span().reinterpret()); + rng::drng drng((std::int32_t) ((0xfffa << 16) | (byte_l<<7) | byte_h )); + rng::shuffle(drng, map.as_span()); + } + return 0; } @@ -15,6 +80,14 @@ namespace work template int xshuffle_op(const char* ifile, const char* ofile, bool is_buffered) { + + if constexpr(unshuffle) + { + + } else { + + } + panic("Unimplemented"); return 0; }