1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-15 22:57:12 +02:00

Merge the Random and Math libraries into the Numeric library and organize code a bit.

This commit is contained in:
Sandu Liviu Catalin
2016-06-05 00:00:59 +03:00
parent cce6beb4d5
commit d42040c9c0
16 changed files with 780 additions and 694 deletions

View File

@ -0,0 +1,196 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Numeric/LongInt.hpp"
#include "Library/Numeric/Random.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <cerrno>
#include <cstdlib>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
LongInt< Int64 >::LongInt(CSStr text) : m_Data(0), m_Text()
{
m_Data = std::strtoll(text, nullptr, 10);
}
// ------------------------------------------------------------------------------------------------
LongInt< Int64 >::LongInt(CSStr text, SQInteger fall) : m_Data(0), m_Text()
{
m_Data = std::strtoll(text, nullptr, 10);
// Simple, check for conversion errors
if (errno == ERANGE)
{
m_Data = ConvTo< Type >::From(fall);
}
}
// ------------------------------------------------------------------------------------------------
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::strtoll(text, nullptr, 10);
}
// ------------------------------------------------------------------------------------------------
LongInt< Uint64 >::LongInt(CSStr text, SQInteger fall) : m_Data(0), m_Text()
{
m_Data = std::strtoull(text, nullptr, 10);
// Simple, check for conversion errors
if (errno == ERANGE)
{
m_Data = ConvTo< Type >::From(fall);
}
}
// ------------------------------------------------------------------------------------------------
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(_SC("SLongInt"), Class< SLongInt >(vm, _SC("SLongInt"))
/* 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 */
.Func(_SC("_tostring"), &SLongInt::ToString)
.Func(_SC("_typename"), &SLongInt::Typename)
.Func(_SC("_cmp"), &SLongInt::Cmp)
/* 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 */
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_add"), &SLongInt::operator +)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_sub"), &SLongInt::operator -)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_mul"), &SLongInt::operator *)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_div"), &SLongInt::operator /)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_modulo"), &SLongInt::operator %)
.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(_SC("ULongInt"), Class< ULongInt >(vm, _SC("ULongInt"))
/* 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 */
.Func(_SC("_tostring"), &ULongInt::ToString)
.Func(_SC("_typename"), &ULongInt::Typename)
.Func(_SC("_cmp"), &ULongInt::Cmp)
/* 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 */
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_add"), &ULongInt::operator +)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_sub"), &ULongInt::operator -)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_mul"), &ULongInt::operator *)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_div"), &ULongInt::operator /)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_modulo"), &ULongInt::operator %)
.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

View File

@ -0,0 +1,551 @@
#ifndef _LIBRARY_NUMERIC_LONGINT_HPP_
#define _LIBRARY_NUMERIC_LONGINT_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
template < typename T > class LongInt;
/* ------------------------------------------------------------------------------------------------
*
*/
template <> class LongInt< Int64 >
{
public:
// --------------------------------------------------------------------------------------------
typedef Int64 Type;
private:
// --------------------------------------------------------------------------------------------
Type m_Data;
SQChar m_Text[32];
public:
/* --------------------------------------------------------------------------------------------
*
*/
LongInt()
: m_Data(0), m_Text()
{
/* ... */
}
LongInt(Type n)
: m_Data(n), m_Text()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt(CSStr text);
LongInt(CSStr text, SQInteger fall);
/* --------------------------------------------------------------------------------------------
*
*/
LongInt(const LongInt< Type > & o)
: m_Data(o.m_Data), m_Text()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
*
*/
~LongInt()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt & operator = (const LongInt< Type > & o)
{
m_Data = o.m_Data;
return *this;
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt< Type > & operator = (Type data)
{
m_Data = data;
return *this;
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt< Type > & operator = (CSStr text);
/* --------------------------------------------------------------------------------------------
*
*/
bool operator == (const LongInt< Type > & o) const
{
return (m_Data == o.m_Data);
}
bool operator != (const LongInt< Type > & o) const
{
return (m_Data != o.m_Data);
}
bool operator < (const LongInt< Type > & o) const
{
return (m_Data < o.m_Data);
}
bool operator > (const LongInt< Type > & o) const
{
return (m_Data > o.m_Data);
}
bool operator <= (const LongInt< Type > & o) const
{
return (m_Data <= o.m_Data);
}
bool operator >= (const LongInt< Type > & o) const
{
return (m_Data >= o.m_Data);
}
/* --------------------------------------------------------------------------------------------
*
*/
operator Type () const { return m_Data; }
/* --------------------------------------------------------------------------------------------
*
*/
LongInt< Type > operator + (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data + o.m_Data);
}
LongInt< Type > operator - (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data - o.m_Data);
}
LongInt< Type > operator * (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data * o.m_Data);
}
LongInt< Type > operator / (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data / o.m_Data);
}
LongInt< Type > operator % (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data % o.m_Data);
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt< Type > operator - () const
{
return LongInt< Type >(-m_Data);
}
/* --------------------------------------------------------------------------------------------
*
*/
Int32 Cmp(const LongInt< Type > & o) const
{
if (m_Data == o.m_Data)
return 0;
else if (m_Data > o.m_Data)
return 1;
else
return -1;
}
/* --------------------------------------------------------------------------------------------
*
*/
CSStr ToString();
/* --------------------------------------------------------------------------------------------
*
*/
CSStr Typename() const
{
return _SC("SLongInt");
}
/* --------------------------------------------------------------------------------------------
*
*/
void SetNum(Type data)
{
m_Data = data;
}
Type GetNum() const
{
return m_Data;
}
/* --------------------------------------------------------------------------------------------
*
*/
SQInteger GetSNum() const
{
return ClampL< Type, SQInteger >(m_Data);
}
/* --------------------------------------------------------------------------------------------
*
*/
void SetStr(CSStr text)
{
*this = text;
}
CSStr GetCStr()
{
return ToString();
}
/* --------------------------------------------------------------------------------------------
*
*/
void Random();
void Random(Type n);
void Random(Type m, Type n);
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel integer.
*/
SQInteger ToSqInteger() const
{
return ClampL< Type, SQInteger >(m_Data);
}
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel float.
*/
SQFloat ToSqFloat() const
{
return ClampL< Float64, SQFloat >(static_cast< Float64 >(m_Data));
}
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel string.
*/
CSStr ToSqString()
{
return ToString();
}
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel boolean.
*/
bool ToSqBool() const
{
return (m_Data > 0);
}
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel character.
*/
SQChar ToSqChar() const
{
return ClampL< Type, SQChar >(m_Data);
}
};
/* ------------------------------------------------------------------------------------------------
*
*/
template <> class LongInt< Uint64 >
{
public:
// --------------------------------------------------------------------------------------------
typedef Uint64 Type;
private:
// --------------------------------------------------------------------------------------------
Type m_Data;
SQChar m_Text[32];
public:
/* --------------------------------------------------------------------------------------------
*
*/
LongInt()
: m_Data(0), m_Text()
{
/* ... */
}
LongInt(Type n)
: m_Data(n), m_Text()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt(CSStr text);
LongInt(CSStr text, SQInteger fall);
/* --------------------------------------------------------------------------------------------
*
*/
LongInt(const LongInt< Type > & o)
: m_Data(o.m_Data), m_Text()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
*
*/
~LongInt()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt & operator = (const LongInt< Type > & o)
{
m_Data = o.m_Data;
return *this;
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt< Type > & operator = (Type data)
{
m_Data = data;
return *this;
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt< Type > & operator = (CSStr text);
/* --------------------------------------------------------------------------------------------
*
*/
bool operator == (const LongInt< Type > & o) const
{
return (m_Data == o.m_Data);
}
bool operator != (const LongInt< Type > & o) const
{
return (m_Data != o.m_Data);
}
bool operator < (const LongInt< Type > & o) const
{
return (m_Data < o.m_Data);
}
bool operator > (const LongInt< Type > & o) const
{
return (m_Data > o.m_Data);
}
bool operator <= (const LongInt< Type > & o) const
{
return (m_Data <= o.m_Data);
}
bool operator >= (const LongInt< Type > & o) const
{
return (m_Data >= o.m_Data);
}
/* --------------------------------------------------------------------------------------------
*
*/
operator Type () const { return m_Data; }
/* --------------------------------------------------------------------------------------------
*
*/
LongInt< Type > operator + (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data + o.m_Data);
}
LongInt< Type > operator - (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data - o.m_Data);
}
LongInt< Type > operator * (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data * o.m_Data);
}
LongInt< Type > operator / (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data / o.m_Data);
}
LongInt< Type > operator % (const LongInt< Type > & o) const
{
return LongInt< Type >(m_Data % o.m_Data);
}
/* --------------------------------------------------------------------------------------------
*
*/
LongInt< Type > operator - () const
{
return LongInt< Type >(-m_Data);
}
/* --------------------------------------------------------------------------------------------
*
*/
Int32 Cmp(const LongInt< Type > & o) const
{
if (m_Data == o.m_Data)
{
return 0;
}
else if (m_Data > o.m_Data)
{
return 1;
}
else
{
return -1;
}
}
/* --------------------------------------------------------------------------------------------
*
*/
CSStr ToString();
/* --------------------------------------------------------------------------------------------
*
*/
CSStr Typename() const
{
return _SC("ULongInt");
}
/* --------------------------------------------------------------------------------------------
*
*/
void SetNum(Type data)
{
m_Data = data;
}
Type GetNum() const
{
return m_Data;
}
/* --------------------------------------------------------------------------------------------
*
*/
SQInteger GetSNum() const
{
return (SQInteger)(m_Data);
}
/* --------------------------------------------------------------------------------------------
*
*/
void SetStr(CSStr text)
{
*this = text;
}
CSStr GetCStr()
{
return ToString();
}
/* --------------------------------------------------------------------------------------------
*
*/
void Random();
void Random(Type n);
void Random(Type m, Type n);
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel integer.
*/
SQInteger ToSqInteger() const
{
return ClampL< Type, SQInteger >(m_Data);
}
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel float.
*/
SQFloat ToSqFloat() const
{
return ClampL< Float64, SQFloat >(static_cast< Float64 >(m_Data));
}
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel string.
*/
CSStr ToSqString()
{
return ToString();
}
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel boolean.
*/
bool ToSqBool() const
{
return (m_Data > 0);
}
/* --------------------------------------------------------------------------------------------
* Attempt to convert the long integer to a squirrel character.
*/
SQChar ToSqChar() const
{
return ClampL< Type, SQChar >(m_Data);
}
};
// ------------------------------------------------------------------------------------------------
typedef LongInt< Int64 > SLongInt;
typedef LongInt< Uint64 > ULongInt;
} // Namespace:: SqMod
#endif // _LIBRARY_NUMERIC_LONGINT_HPP_

File diff suppressed because it is too large Load Diff

View 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_

View File

@ -0,0 +1,420 @@
// ------------------------------------------------------------------------------------------------
#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;
}
// ------------------------------------------------------------------------------------------------
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)
.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

View 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_