2020-05-27 10:25:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-27 10:25:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Platform.h>
|
2022-12-09 13:46:13 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-05-27 10:25:46 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2022-10-09 21:23:23 +00:00
|
|
|
#if defined(AK_OS_SERENITY) || defined(AK_OS_ANDROID)
|
2020-05-27 10:25:46 +00:00
|
|
|
# include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
2020-10-17 20:25:13 +00:00
|
|
|
#if defined(__unix__)
|
2020-05-27 10:25:46 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2021-09-12 16:06:33 +00:00
|
|
|
#if defined(AK_OS_MACOS)
|
2020-05-29 20:22:01 +00:00
|
|
|
# include <sys/random.h>
|
|
|
|
#endif
|
|
|
|
|
2022-09-20 07:15:12 +00:00
|
|
|
#if defined(AK_OS_WINDOWS)
|
2022-12-12 14:40:53 +00:00
|
|
|
# include <stdlib.h>
|
2022-09-20 07:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-27 10:25:46 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2020-12-20 23:09:48 +00:00
|
|
|
inline void fill_with_random([[maybe_unused]] void* buffer, [[maybe_unused]] size_t length)
|
2020-05-27 10:25:46 +00:00
|
|
|
{
|
2022-10-09 21:23:23 +00:00
|
|
|
#if defined(AK_OS_SERENITY) || defined(AK_OS_ANDROID)
|
2020-05-27 10:25:46 +00:00
|
|
|
arc4random_buf(buffer, length);
|
2020-11-27 22:57:02 +00:00
|
|
|
#elif defined(OSS_FUZZ)
|
2021-09-12 16:06:33 +00:00
|
|
|
#elif defined(__unix__) or defined(AK_OS_MACOS)
|
2020-12-20 23:09:48 +00:00
|
|
|
[[maybe_unused]] int rc = getentropy(buffer, length);
|
2022-09-20 07:15:12 +00:00
|
|
|
#else
|
|
|
|
char* char_buffer = static_cast<char*>(buffer);
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
2022-12-12 14:40:53 +00:00
|
|
|
char_buffer[i] = rand();
|
2022-09-20 07:15:12 +00:00
|
|
|
}
|
2020-05-27 10:25:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T get_random()
|
|
|
|
{
|
|
|
|
T t;
|
|
|
|
fill_with_random(&t, sizeof(T));
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:16:06 +00:00
|
|
|
u32 get_random_uniform(u32 max_bounds);
|
|
|
|
|
2022-12-09 13:46:13 +00:00
|
|
|
template<typename Collection>
|
|
|
|
inline void shuffle(Collection& collection)
|
|
|
|
{
|
|
|
|
// Fisher-Yates shuffle
|
|
|
|
for (size_t i = collection.size() - 1; i >= 1; --i) {
|
|
|
|
size_t j = get_random_uniform(i + 1);
|
|
|
|
AK::swap(collection[i], collection[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 15:09:44 +00:00
|
|
|
// shuffle() implementation for NonnullPtrVector, since its operator[] returns a reference to the pointed-at value
|
|
|
|
// instead of the pointer itself.
|
|
|
|
template<typename Collection>
|
|
|
|
requires(requires(Collection collection) { collection.ptr_at(0); })
|
|
|
|
inline void shuffle(Collection& collection)
|
|
|
|
{
|
|
|
|
// Fisher-Yates shuffle
|
|
|
|
for (size_t i = collection.size() - 1; i >= 1; --i) {
|
|
|
|
size_t j = get_random_uniform(i + 1);
|
|
|
|
AK::swap(collection.ptr_at(i), collection.ptr_at(j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:25:46 +00:00
|
|
|
}
|
2020-10-16 19:59:53 +00:00
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-10-16 19:59:53 +00:00
|
|
|
using AK::fill_with_random;
|
|
|
|
using AK::get_random;
|
2021-05-14 15:16:06 +00:00
|
|
|
using AK::get_random_uniform;
|
2022-12-09 13:46:13 +00:00
|
|
|
using AK::shuffle;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|