Skip to content

Commit

Permalink
Add random generation to a span
Browse files Browse the repository at this point in the history
  • Loading branch information
alainesp committed Feb 3, 2024
1 parent 8feefe1 commit d841369
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions wy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <random>
#include <string>
#include <version>
#ifdef __cpp_lib_span
#include <span>
#endif

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// wyhash.h
Expand Down Expand Up @@ -313,6 +316,39 @@ namespace wy {
// Final size
vec.resize(size);
}
#ifdef __cpp_lib_span
/// <summary>
/// Generate a random stream of bytes.
/// </summary>
/// <typeparam name="T">The type of elements on the span to fill with random data</typeparam>
/// <param name="data">out: A span of random elements</param>
template<class T = uint8_t> void generate_stream(std::span<T> data) noexcept
{
uint64_t* dataPtr = reinterpret_cast<uint64_t*>(data.data());

// Generate random values
for (size_t i = 0; i < data.size() * sizeof(T) / sizeof(uint64_t); i++, dataPtr++)
{
#if WYHASH_LITTLE_ENDIAN
uint64_t val = operator()();
#else
uint64_t val = byteswap64(operator()());
#endif
memcpy(dataPtr, &val, sizeof(uint64_t));
}
// Final part
size_t rest = data.size() * sizeof(T) % sizeof(uint64_t);
if (rest)
{
#if WYHASH_LITTLE_ENDIAN
uint64_t val = operator()();
#else
uint64_t val = byteswap64(operator()());
#endif
memcpy(dataPtr, &val, rest);
}
}
#endif
};

/// <summary>
Expand Down

0 comments on commit d841369

Please sign in to comment.