mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-08-06 08:01:47 +02:00
Rename source to module.
This commit is contained in:
197
module/Library/Numeric/LongInt.cpp
Normal file
197
module/Library/Numeric/LongInt.cpp
Normal file
@@ -0,0 +1,197 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Numeric/LongInt.hpp"
|
||||
#include "Library/Numeric/Random.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
#include "Base/DynArg.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <cstdio>
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQMODE_DECL_TYPENAME(TypenameS, _SC("SLongInt"))
|
||||
SQMODE_DECL_TYPENAME(TypenameU, _SC("ULongInt"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
LongInt< Int64 >::LongInt(CSStr text)
|
||||
: m_Data(0), m_Text()
|
||||
{
|
||||
m_Data = std::strtoll(text, nullptr, 10);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
LongInt< Int64 >::LongInt(CSStr text, Uint32 base)
|
||||
: m_Data(0), m_Text()
|
||||
{
|
||||
m_Data = std::strtoll(text, nullptr, base);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
LongInt< Int64 > & LongInt< Int64 >::operator = (CSStr text)
|
||||
{
|
||||
m_Data = std::strtoll(text, nullptr, 10);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr LongInt< Int64 >::ToString()
|
||||
{
|
||||
if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0)
|
||||
{
|
||||
m_Text[0] = 0;
|
||||
}
|
||||
|
||||
return m_Text;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void LongInt< Int64 >::Random()
|
||||
{
|
||||
m_Data = GetRandomInt64();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void LongInt< Int64 >::Random(Type n)
|
||||
{
|
||||
m_Data = GetRandomInt64(n);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void LongInt< Int64 >::Random(Type m, Type n)
|
||||
{
|
||||
m_Data = GetRandomInt64(m, n);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
LongInt< Uint64 >::LongInt(CSStr text)
|
||||
: m_Data(0), m_Text()
|
||||
{
|
||||
m_Data = std::strtoull(text, nullptr, 10);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
LongInt< Uint64 >::LongInt(CSStr text, Uint32 base)
|
||||
: m_Data(0), m_Text()
|
||||
{
|
||||
m_Data = std::strtoull(text, nullptr, base);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
LongInt< Uint64 > & LongInt< Uint64 >::operator = (CSStr text)
|
||||
{
|
||||
m_Data = std::strtoull(text, nullptr, 10);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr LongInt< Uint64 >::ToString()
|
||||
{
|
||||
if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0)
|
||||
{
|
||||
m_Text[0] = 0;
|
||||
}
|
||||
|
||||
return m_Text;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void LongInt< Uint64 >::Random()
|
||||
{
|
||||
m_Data = GetRandomUint64();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void LongInt< Uint64 >::Random(Type n)
|
||||
{
|
||||
m_Data = GetRandomUint64(n);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void LongInt< Uint64 >::Random(Type m, Type n)
|
||||
{
|
||||
m_Data = GetRandomUint64(m, n);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_LongInt(HSQUIRRELVM vm)
|
||||
{
|
||||
RootTable(vm).Bind(TypenameS::Str,
|
||||
Class< SLongInt >(vm, TypenameS::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< SLongInt::Type >()
|
||||
.template Ctor< CCStr, SQInteger >()
|
||||
// Properties
|
||||
.Prop(_SC("Str"), &SLongInt::GetCStr, &SLongInt::SetStr)
|
||||
.Prop(_SC("Num"), &SLongInt::GetSNum, &SLongInt::SetNum)
|
||||
// Core Meta-methods
|
||||
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< SLongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, SLongInt, ULongInt >)
|
||||
.SquirrelFunc(_SC("_typename"), &TypenameS::Fn)
|
||||
.Func(_SC("_tostring"), &SLongInt::ToString)
|
||||
// Core Functions
|
||||
.Func(_SC("tointeger"), &SLongInt::ToSqInteger)
|
||||
.Func(_SC("tofloat"), &SLongInt::ToSqFloat)
|
||||
.Func(_SC("tostring"), &SLongInt::ToSqString)
|
||||
.Func(_SC("tobool"), &SLongInt::ToSqBool)
|
||||
.Func(_SC("tochar"), &SLongInt::ToSqChar)
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< SLongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, SLongInt, ULongInt >)
|
||||
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< SLongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, SLongInt, ULongInt >)
|
||||
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< SLongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, SLongInt, ULongInt >)
|
||||
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< SLongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, SLongInt, ULongInt >)
|
||||
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< SLongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, SLongInt, ULongInt >)
|
||||
.Func< SLongInt (SLongInt::*)(void) const >(_SC("_unm"), &SLongInt::operator -)
|
||||
// Functions
|
||||
.Func(_SC("GetStr"), &SLongInt::GetCStr)
|
||||
.Func(_SC("SetStr"), &SLongInt::SetStr)
|
||||
.Func(_SC("GetNum"), &SLongInt::GetSNum)
|
||||
.Func(_SC("SetNum"), &SLongInt::SetNum)
|
||||
// Overloads
|
||||
.Overload< void (SLongInt::*)(void) >(_SC("Random"), &SLongInt::Random)
|
||||
.Overload< void (SLongInt::*)(SLongInt::Type) >(_SC("Random"), &SLongInt::Random)
|
||||
.Overload< void (SLongInt::*)(SLongInt::Type, SLongInt::Type) >(_SC("Random"), &SLongInt::Random)
|
||||
);
|
||||
|
||||
RootTable(vm).Bind(TypenameU::Str,
|
||||
Class< ULongInt >(vm, TypenameU::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< ULongInt::Type >()
|
||||
.Ctor< CCStr, SQInteger >()
|
||||
// Properties
|
||||
.Prop(_SC("Str"), &ULongInt::GetCStr, &ULongInt::SetStr)
|
||||
.Prop(_SC("Num"), &ULongInt::GetSNum, &ULongInt::SetNum)
|
||||
// Core Meta-methods
|
||||
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< ULongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, ULongInt, SLongInt >)
|
||||
.SquirrelFunc(_SC("_typename"), &TypenameU::Fn)
|
||||
.Func(_SC("_tostring"), &ULongInt::ToString)
|
||||
// Core Functions
|
||||
.Func(_SC("tointeger"), &ULongInt::ToSqInteger)
|
||||
.Func(_SC("tofloat"), &ULongInt::ToSqFloat)
|
||||
.Func(_SC("tostring"), &ULongInt::ToSqString)
|
||||
.Func(_SC("tobool"), &ULongInt::ToSqBool)
|
||||
.Func(_SC("tochar"), &ULongInt::ToSqChar)
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< ULongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, ULongInt, SLongInt >)
|
||||
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< ULongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, ULongInt, SLongInt >)
|
||||
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< ULongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, ULongInt, SLongInt >)
|
||||
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< ULongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, ULongInt, SLongInt >)
|
||||
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< ULongInt >, SQInteger, SQFloat, bool, std::nullptr_t, CSStr, ULongInt, SLongInt >)
|
||||
.Func< ULongInt (ULongInt::*)(void) const >(_SC("_unm"), &ULongInt::operator -)
|
||||
// Functions
|
||||
.Func(_SC("GetStr"), &ULongInt::GetCStr)
|
||||
.Func(_SC("SetStr"), &ULongInt::SetStr)
|
||||
.Func(_SC("GetNum"), &ULongInt::GetSNum)
|
||||
.Func(_SC("SetNum"), &ULongInt::SetNum)
|
||||
// Overloads
|
||||
.Overload< void (ULongInt::*)(void) >(_SC("Random"), &ULongInt::Random)
|
||||
.Overload< void (ULongInt::*)(ULongInt::Type) >(_SC("Random"), &ULongInt::Random)
|
||||
.Overload< void (ULongInt::*)(ULongInt::Type, ULongInt::Type) >(_SC("Random"), &ULongInt::Random)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
1280
module/Library/Numeric/LongInt.hpp
Normal file
1280
module/Library/Numeric/LongInt.hpp
Normal file
File diff suppressed because it is too large
Load Diff
1231
module/Library/Numeric/Math.cpp
Normal file
1231
module/Library/Numeric/Math.cpp
Normal file
File diff suppressed because it is too large
Load Diff
14
module/Library/Numeric/Math.hpp
Normal file
14
module/Library/Numeric/Math.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef _LIBRARY_NUMERIC_MATH_HPP_
|
||||
#define _LIBRARY_NUMERIC_MATH_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "SqBase.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_NUMERIC_MATH_HPP_
|
444
module/Library/Numeric/Random.cpp
Normal file
444
module/Library/Numeric/Random.cpp
Normal file
@@ -0,0 +1,444 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Numeric/Random.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
#include "Base/Buffer.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <cstdlib>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#ifdef SQMOD_OS_WINDOWS
|
||||
#include <process.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static std::unique_ptr< std::mt19937 > RG32_MT19937 =
|
||||
std::unique_ptr< std::mt19937 >(new std::mt19937(GenerateSeed()));
|
||||
|
||||
static std::unique_ptr< std::mt19937_64 > RG64_MT19937 =
|
||||
std::unique_ptr< std::mt19937_64 >(new std::mt19937_64(GenerateSeed()));
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static std::uniform_int_distribution< Int8 > Int8_Dist(std::numeric_limits< Int8 >::min(),
|
||||
std::numeric_limits< Int8 >::max());
|
||||
static std::uniform_int_distribution< Uint8 > Uint8_Dist(std::numeric_limits< Uint8 >::min(),
|
||||
std::numeric_limits< Uint8 >::max());
|
||||
|
||||
static std::uniform_int_distribution< Int16 > Int16_Dist(std::numeric_limits< Int16 >::min(),
|
||||
std::numeric_limits< Int16 >::max());
|
||||
static std::uniform_int_distribution< Uint16 > Uint16_Dist(std::numeric_limits< Uint16 >::min(),
|
||||
std::numeric_limits< Uint16 >::max());
|
||||
|
||||
static std::uniform_int_distribution< Int32 > Int32_Dist(std::numeric_limits< Int32 >::min(),
|
||||
std::numeric_limits< Int32 >::max());
|
||||
static std::uniform_int_distribution< Uint32 > Uint32_Dist(std::numeric_limits< Uint32 >::min(),
|
||||
std::numeric_limits< Uint32 >::max());
|
||||
|
||||
static std::uniform_int_distribution< Int64 > Int64_Dist(std::numeric_limits< Int64 >::min(),
|
||||
std::numeric_limits< Int64 >::max());
|
||||
static std::uniform_int_distribution< Uint64 > Uint64_Dist(std::numeric_limits< Uint64 >::min(),
|
||||
std::numeric_limits< Uint64 >::max());
|
||||
|
||||
static std::uniform_real_distribution<Float32> Float32_Dist(std::numeric_limits< Float32 >::min(),
|
||||
std::numeric_limits< Float32 >::max());
|
||||
static std::uniform_real_distribution<Float64> Float64_Dist(std::numeric_limits< Float64 >::min(),
|
||||
std::numeric_limits< Float64 >::max());
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static std::uniform_int_distribution< String::value_type >
|
||||
String_Dist(std::numeric_limits< String::value_type >::min(),
|
||||
std::numeric_limits< String::value_type >::max());
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SizeT GenerateSeed()
|
||||
{
|
||||
Ulong a = clock();
|
||||
Ulong b = time(NULL);
|
||||
#ifdef SQMOD_OS_WINDOWS
|
||||
Ulong c = _getpid();
|
||||
#else
|
||||
Ulong c = getpid();
|
||||
#endif
|
||||
// Mangle
|
||||
a=a-b; a=a-c; a=a^(c >> 13);
|
||||
b=b-c; b=b-a; b=b^(a << 8);
|
||||
c=c-a; c=c-b; c=c^(b >> 13);
|
||||
a=a-b; a=a-c; a=a^(c >> 12);
|
||||
b=b-c; b=b-a; b=b^(a << 16);
|
||||
c=c-a; c=c-b; c=c^(b >> 5);
|
||||
a=a-b; a=a-c; a=a^(c >> 3);
|
||||
b=b-c; b=b-a; b=b^(a << 10);
|
||||
c=c-a; c=c-b; c=c^(b >> 15);
|
||||
// Return result
|
||||
return c;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SizeT GenerateSeed2()
|
||||
{
|
||||
struct {
|
||||
std::clock_t c;
|
||||
std::time_t t;
|
||||
#ifdef SQMOD_OS_WINDOWS
|
||||
int p;
|
||||
#else
|
||||
pid_t p;
|
||||
#endif
|
||||
} data;
|
||||
data.c = std::clock();
|
||||
data.t = std::time(nullptr);
|
||||
#ifdef SQMOD_OS_WINDOWS
|
||||
data.p = _getpid();
|
||||
#else
|
||||
data.p = getpid();
|
||||
#endif
|
||||
// Mangle and return result
|
||||
return FnvHash(reinterpret_cast< const uint8_t * >(&data), sizeof(data));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void ReseedRandom()
|
||||
{
|
||||
RG32_MT19937.reset(new std::mt19937(GenerateSeed()));
|
||||
RG64_MT19937.reset(new std::mt19937_64(GenerateSeed()));
|
||||
}
|
||||
|
||||
void ReseedRandom(Uint32 n)
|
||||
{
|
||||
RG32_MT19937.reset(new std::mt19937(n));
|
||||
RG64_MT19937.reset(new std::mt19937_64(n));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void ReseedRandom32()
|
||||
{
|
||||
RG32_MT19937.reset(new std::mt19937(GenerateSeed()));
|
||||
}
|
||||
|
||||
void ReseedRandom32(Uint32 n)
|
||||
{
|
||||
RG32_MT19937.reset(new std::mt19937(n));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void ReseedRandom64()
|
||||
{
|
||||
RG64_MT19937.reset(new std::mt19937_64(GenerateSeed()));
|
||||
}
|
||||
|
||||
void ReseedRandom64(Uint32 n)
|
||||
{
|
||||
RG64_MT19937.reset(new std::mt19937_64(n));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int8 GetRandomInt8()
|
||||
{
|
||||
return Int8_Dist(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Int8 GetRandomInt8(Int8 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int8 >(0, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Int8 GetRandomInt8(Int8 m, Int8 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int8 >(m, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint8 GetRandomUint8()
|
||||
{
|
||||
return Uint8_Dist(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Uint8 GetRandomUint8(Uint8 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Uint8 >(0, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Uint8 GetRandomUint8(Uint8 m, Uint8 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Uint8 >(m, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int16 GetRandomInt16()
|
||||
{
|
||||
return Int16_Dist(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Int16 GetRandomInt16(Int16 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int16 >(0, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Int16 GetRandomInt16(Int16 m, Int16 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int16 >(m, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint16 GetRandomUint16()
|
||||
{
|
||||
return Uint16_Dist(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Uint16 GetRandomUint16(Uint16 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Uint16 >(0, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Uint16 GetRandomUint16(Uint16 m, Uint16 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Uint16 >(m, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 GetRandomInt32()
|
||||
{
|
||||
return Int32_Dist(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Int32 GetRandomInt32(Int32 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int32 >(0, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Int32 GetRandomInt32(Int32 m, Int32 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int32 >(m, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint32 GetRandomUint32()
|
||||
{
|
||||
return Int32_Dist(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Uint32 GetRandomUint32(Uint32 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int32 >(0, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Uint32 GetRandomUint32(Uint32 m, Uint32 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int32 >(m, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int64 GetRandomInt64()
|
||||
{
|
||||
return Int64_Dist(*RG64_MT19937);
|
||||
}
|
||||
|
||||
Int64 GetRandomInt64(Int64 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int64 >(0, n)(*RG64_MT19937);
|
||||
}
|
||||
|
||||
Int64 GetRandomInt64(Int64 m, Int64 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Int64 >(m, n)(*RG64_MT19937);
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint64 GetRandomUint64()
|
||||
{
|
||||
return Uint64_Dist(*RG64_MT19937);
|
||||
}
|
||||
|
||||
Uint64 GetRandomUint64(Uint64 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Uint64 >(0, n)(*RG64_MT19937);
|
||||
}
|
||||
|
||||
Uint64 GetRandomUint64(Uint64 m, Uint64 n)
|
||||
{
|
||||
return std::uniform_int_distribution< Uint64 >(m, n)(*RG64_MT19937);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Float32 GetRandomFloat32()
|
||||
{
|
||||
return Float32_Dist(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Float32 GetRandomFloat32(Float32 n)
|
||||
{
|
||||
return std::uniform_real_distribution< Float32 >(0, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
Float32 GetRandomFloat32(Float32 m, Float32 n)
|
||||
{
|
||||
return std::uniform_real_distribution< Float32 >(m, n)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Float64 GetRandomFloat64()
|
||||
{
|
||||
return Float64_Dist(*RG64_MT19937);
|
||||
}
|
||||
|
||||
Float64 GetRandomFloat64(Float64 n)
|
||||
{
|
||||
return std::uniform_real_distribution< Float64 >(0, n)(*RG64_MT19937);
|
||||
}
|
||||
|
||||
Float64 GetRandomFloat64(Float64 m, Float64 n)
|
||||
{
|
||||
return std::uniform_real_distribution< Float64 >(m, n)(*RG64_MT19937);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void GetRandomString(String & str, String::size_type len)
|
||||
{
|
||||
// Reserve the requested size + the null terminator
|
||||
str.reserve(len+1);
|
||||
// Resize to the requested size and fill with 0
|
||||
str.resize(len);
|
||||
// Generate the requested amount of characters
|
||||
for (auto & c : str)
|
||||
c = String_Dist(*RG32_MT19937);
|
||||
// Append the null terminator
|
||||
str.push_back(0);
|
||||
}
|
||||
|
||||
void GetRandomString(String & str, String::size_type len, String::value_type n)
|
||||
{
|
||||
// Reserve the requested size + the null terminator
|
||||
str.reserve(len+1);
|
||||
// Resize to the requested size and fill with 0
|
||||
str.resize(len);
|
||||
// Create the custom distribution
|
||||
std::uniform_int_distribution< String::value_type > dist(1, n);
|
||||
// Generate the requested amount of characters
|
||||
for (auto & c : str)
|
||||
c = dist(*RG32_MT19937);
|
||||
// Append the null terminator
|
||||
str.push_back(0);
|
||||
}
|
||||
|
||||
void GetRandomString(String & str, String::size_type len, String::value_type m, String::value_type n)
|
||||
{
|
||||
// Reserve the requested size + the null terminator
|
||||
str.reserve(len+1);
|
||||
// Resize to the requested size and fill with 0
|
||||
str.resize(len);
|
||||
// Create the custom distribution
|
||||
std::uniform_int_distribution< String::value_type > dist(m, n);
|
||||
// Generate the requested amount of characters
|
||||
for (auto & c : str)
|
||||
c = dist(*RG32_MT19937);
|
||||
// Append the null terminator
|
||||
str.push_back(0);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool GetRandomBool()
|
||||
{
|
||||
return std::bernoulli_distribution()(*RG32_MT19937);
|
||||
}
|
||||
|
||||
bool GetRandomBool(SQFloat p)
|
||||
{
|
||||
return std::bernoulli_distribution(p)(*RG32_MT19937);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static String RandomString(Int32 len)
|
||||
{
|
||||
// Is there anything to generate?
|
||||
if (len <= 0)
|
||||
return _SC("");
|
||||
// Prepare the string buffer
|
||||
String str;
|
||||
// Request the random fill
|
||||
GetRandomString(str, len);
|
||||
// Return ownership of the string
|
||||
return std::move(str);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static String RandomString(Int32 len, SQChar n)
|
||||
{
|
||||
// Is there anything to generate?
|
||||
if (len <= 0)
|
||||
return _SC("");
|
||||
// Prepare the string buffer
|
||||
String str;
|
||||
// Request the random fill
|
||||
GetRandomString(str, len, n);
|
||||
// Return ownership of the string
|
||||
return std::move(str);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static String RandomString(Int32 len, SQChar m, SQChar n)
|
||||
{
|
||||
// Is there anything to generate?
|
||||
if (len <= 0)
|
||||
return _SC("");
|
||||
// Prepare the string buffer
|
||||
String str;
|
||||
// Request the random fill
|
||||
GetRandomString(str, len, m, n);
|
||||
// Return ownership of the string
|
||||
return std::move(str);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Register_Random(HSQUIRRELVM vm)
|
||||
{
|
||||
RootTable(vm).Bind(_SC("SqRand"), Table(vm)
|
||||
.Func(_SC("GenSeed"), &GenerateSeed)
|
||||
.Func(_SC("GenSeed2"), &GenerateSeed2)
|
||||
.Overload< void (*)(void) >(_SC("Reseed"), &ReseedRandom)
|
||||
.Overload< void (*)(Uint32) >(_SC("Reseed"), &ReseedRandom)
|
||||
.Overload< void (*)(void) >(_SC("Reseed32"), &ReseedRandom32)
|
||||
.Overload< void (*)(Uint32) >(_SC("Reseed32"), &ReseedRandom32)
|
||||
.Overload< void (*)(void) >(_SC("Reseed64"), &ReseedRandom64)
|
||||
.Overload< void (*)(Uint32) >(_SC("Reseed64"), &ReseedRandom64)
|
||||
|
||||
#ifdef _SQ64
|
||||
.Overload< SQInteger (*)(void) >(_SC("Integer"), &GetRandomInt64)
|
||||
.Overload< SQInteger (*)(SQInteger) >(_SC("Integer"), &GetRandomInt64)
|
||||
.Overload< SQInteger (*)(SQInteger, SQInteger) >(_SC("Integer"), &GetRandomInt64)
|
||||
#else
|
||||
.Overload< SQInteger (*)(void) >(_SC("Integer"), &GetRandomInt32)
|
||||
.Overload< SQInteger (*)(SQInteger) >(_SC("Integer"), &GetRandomInt32)
|
||||
.Overload< SQInteger (*)(SQInteger, SQInteger) >(_SC("Integer"), &GetRandomInt32)
|
||||
#endif // _SQ64
|
||||
|
||||
#ifdef SQUSEDOUBLE
|
||||
.Overload< SQFloat (*)(void) >(_SC("Float"), &GetRandomFloat64)
|
||||
.Overload< SQFloat (*)(SQFloat) >(_SC("Float"), &GetRandomFloat64)
|
||||
.Overload< SQFloat (*)(SQFloat, SQFloat) >(_SC("Float"), &GetRandomFloat64)
|
||||
#else
|
||||
.Overload< SQFloat (*)(void) >(_SC("Float"), &GetRandomFloat32)
|
||||
.Overload< SQFloat (*)(SQFloat) >(_SC("Float"), &GetRandomFloat32)
|
||||
.Overload< SQFloat (*)(SQFloat, SQFloat) >(_SC("Float"), &GetRandomFloat32)
|
||||
#endif // SQUSEDOUBLE
|
||||
|
||||
.Overload< String (*)(Int32) >(_SC("String"), &RandomString)
|
||||
.Overload< String (*)(Int32, SQChar) >(_SC("String"), &RandomString)
|
||||
.Overload< String (*)(Int32, SQChar, SQChar) >(_SC("String"), &RandomString)
|
||||
.Overload< bool (*)(void) >(_SC("Bool"), &GetRandomBool)
|
||||
.Overload< bool (*)(SQFloat) >(_SC("Bool"), &GetRandomBool)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
164
module/Library/Numeric/Random.hpp
Normal file
164
module/Library/Numeric/Random.hpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#ifndef _LIBRARY_NUMERIC_RANDOM_HPP_
|
||||
#define _LIBRARY_NUMERIC_RANDOM_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "SqBase.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Attempt to generate a moderately unique number to be used as a seed for random numbers.
|
||||
*/
|
||||
Uint32 GenerateSeed();
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void ReseedRandom();
|
||||
void ReseedRandom(Uint32 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int8 GetRandomInt8();
|
||||
Int8 GetRandomInt8(Int8 n);
|
||||
Int8 GetRandomInt8(Int8 m, Int8 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint8 GetRandomUint8();
|
||||
Uint8 GetRandomUint8(Uint8 n);
|
||||
Uint8 GetRandomUint8(Uint8 m, Uint8 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int16 GetRandomInt16();
|
||||
Int16 GetRandomInt16(Int16 n);
|
||||
Int16 GetRandomInt16(Int16 m, Int16 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint16 GetRandomUint16();
|
||||
Uint16 GetRandomUint16(Uint16 n);
|
||||
Uint16 GetRandomUint16(Uint16 m, Uint16 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 GetRandomInt32();
|
||||
Int32 GetRandomInt32(Int32 n);
|
||||
Int32 GetRandomInt32(Int32 m, Int32 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint32 GetRandomUint32();
|
||||
Uint32 GetRandomUint32(Uint32 n);
|
||||
Uint32 GetRandomUint32(Uint32 m, Uint32 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int64 GetRandomInt64();
|
||||
Int64 GetRandomInt64(Int64 n);
|
||||
Int64 GetRandomInt64(Int64 m, Int64 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint64 GetRandomUint64();
|
||||
Uint64 GetRandomUint64(Uint64 n);
|
||||
Uint64 GetRandomUint64(Uint64 m, Uint64 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Float32 GetRandomFloat32();
|
||||
Float32 GetRandomFloat32(Float32 n);
|
||||
Float32 GetRandomFloat32(Float32 m, Float32 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Float64 GetRandomFloat64();
|
||||
Float64 GetRandomFloat64(Float64 n);
|
||||
Float64 GetRandomFloat64(Float64 m, Float64 n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void GetRandomString(String & str, String::size_type len);
|
||||
void GetRandomString(String & str, String::size_type len, String::value_type n);
|
||||
void GetRandomString(String & str, String::size_type len, String::value_type m, String::value_type n);
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool GetRandomBool();
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template <typename T> struct RandomVal
|
||||
{ /* ... */ };
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template <> struct RandomVal< Int8 >
|
||||
{
|
||||
static inline Int8 Get() { return GetRandomInt8(); }
|
||||
static inline Int8 Get(Int8 n) { return GetRandomInt8(n); }
|
||||
static inline Int8 Get(Int8 m, Int8 n) { return GetRandomInt8(m, n); }
|
||||
};
|
||||
|
||||
template <> struct RandomVal< Uint8 >
|
||||
{
|
||||
static inline Uint8 Get() { return GetRandomUint8(); }
|
||||
static inline Uint8 Get(Uint8 n) { return GetRandomUint8(n); }
|
||||
static inline Uint8 Get(Uint8 m, Uint8 n) { return GetRandomUint8(m, n); }
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template <> struct RandomVal< Int16 >
|
||||
{
|
||||
static inline Int16 Get() { return GetRandomInt16(); }
|
||||
static inline Int16 Get(Int16 n) { return GetRandomInt16(n); }
|
||||
static inline Int16 Get(Int16 m, Int16 n) { return GetRandomInt16(m, n); }
|
||||
};
|
||||
|
||||
template <> struct RandomVal< Uint16 >
|
||||
{
|
||||
static inline Uint16 Get() { return GetRandomUint16(); }
|
||||
static inline Uint16 Get(Uint16 n) { return GetRandomUint16(n); }
|
||||
static inline Uint16 Get(Uint16 m, Uint16 n) { return GetRandomUint16(m, n); }
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template <> struct RandomVal< Int32 >
|
||||
{
|
||||
static inline Int32 Get() { return GetRandomInt32(); }
|
||||
static inline Int32 Get(Int32 n) { return GetRandomInt32(n); }
|
||||
static inline Int32 Get(Int32 m, Int32 n) { return GetRandomInt32(m, n); }
|
||||
};
|
||||
|
||||
template <> struct RandomVal< Uint32 >
|
||||
{
|
||||
static inline Uint32 Get() { return GetRandomUint32(); }
|
||||
static inline Uint32 Get(Uint32 n) { return GetRandomUint32(n); }
|
||||
static inline Uint32 Get(Uint32 m, Uint32 n) { return GetRandomUint32(m, n); }
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template <> struct RandomVal< Int64 >
|
||||
{
|
||||
static inline Int64 Get() { return GetRandomInt64(); }
|
||||
static inline Int64 Get(Int64 n) { return GetRandomInt64(n); }
|
||||
static inline Int64 Get(Int64 m, Int64 n) { return GetRandomInt64(m, n); }
|
||||
};
|
||||
|
||||
template <> struct RandomVal< Uint64 >
|
||||
{
|
||||
static inline Uint64 Get() { return GetRandomUint64(); }
|
||||
static inline Uint64 Get(Uint64 n) { return GetRandomUint64(n); }
|
||||
static inline Uint64 Get(Uint64 m, Uint64 n) { return GetRandomUint64(m, n); }
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template <> struct RandomVal< Float32 >
|
||||
{
|
||||
static inline Float32 Get() { return GetRandomFloat32(); }
|
||||
static inline Float32 Get(Float32 n) { return GetRandomFloat32(n); }
|
||||
static inline Float32 Get(Float32 m, Float32 n) { return GetRandomFloat32(m, n); }
|
||||
};
|
||||
|
||||
template <> struct RandomVal< Float64 >
|
||||
{
|
||||
static inline Float64 Get() { return GetRandomFloat64(); }
|
||||
static inline Float64 Get(Float64 n) { return GetRandomFloat64(n); }
|
||||
static inline Float64 Get(Float64 m, Float64 n) { return GetRandomFloat64(m, n); }
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template <> struct RandomVal< bool >
|
||||
{
|
||||
static inline bool Get() { return GetRandomBool(); }
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_NUMERIC_RANDOM_HPP_
|
Reference in New Issue
Block a user