1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-19 03:57:14 +01: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

@ -505,12 +505,14 @@
<Unit filename="../source/Library/IO/File.hpp" />
<Unit filename="../source/Library/IO/INI.cpp" />
<Unit filename="../source/Library/IO/INI.hpp" />
<Unit filename="../source/Library/Math.cpp" />
<Unit filename="../source/Library/Math.hpp" />
<Unit filename="../source/Library/Numeric.cpp" />
<Unit filename="../source/Library/Numeric.hpp" />
<Unit filename="../source/Library/Random.cpp" />
<Unit filename="../source/Library/Random.hpp" />
<Unit filename="../source/Library/Numeric/LongInt.cpp" />
<Unit filename="../source/Library/Numeric/LongInt.hpp" />
<Unit filename="../source/Library/Numeric/Math.cpp" />
<Unit filename="../source/Library/Numeric/Math.hpp" />
<Unit filename="../source/Library/Numeric/Random.cpp" />
<Unit filename="../source/Library/Numeric/Random.hpp" />
<Unit filename="../source/Library/String.cpp" />
<Unit filename="../source/Library/String.hpp" />
<Unit filename="../source/Library/System.cpp" />

View File

@ -17,7 +17,7 @@
// ------------------------------------------------------------------------------------------------
#ifndef SQMOD_PLUGIN_API
#include "Library/Numeric.hpp"
#include "Library/Numeric/LongInt.hpp"
#include <sqstdstring.h>
#endif // SQMOD_PLUGIN_API

View File

@ -2,9 +2,9 @@
#include "Base/Shared.hpp"
#include "Base/Buffer.hpp"
#include "Base/Color3.hpp"
#include "Library/Random.hpp"
#include "Library/Numeric/Random.hpp"
#include "Library/Numeric/LongInt.hpp"
#include "Library/String.hpp"
#include "Library/Numeric.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstring>

View File

@ -2,7 +2,7 @@
#include "Core.hpp"
// ------------------------------------------------------------------------------------------------
#include "Library/Numeric.hpp"
#include "Library/Numeric/LongInt.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Datetime.hpp"

View File

@ -5,7 +5,7 @@
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Timer.hpp"
#include "Library/Chrono/Timestamp.hpp"
#include "Library/Numeric.hpp"
#include "Library/Numeric/LongInt.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------

View File

@ -4,7 +4,7 @@
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Numeric.hpp"
#include "Library/Numeric/LongInt.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {

View File

@ -1,159 +1,20 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Numeric.hpp"
#include "Library/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <cerrno>
#include <cstdlib>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
LongInt< Int64 >::LongInt(CSStr text) : m_Data(0), m_Text()
{ m_Data = strtoll(text, NULL, 10); }
LongInt< Int64 >::LongInt(CSStr text, SQInteger fall) : m_Data(0), m_Text()
{
m_Data = strtoll(text, NULL, 10);
if (errno == ERANGE)
{
m_Data = ConvTo< Type >::From(fall);
}
}
// ------------------------------------------------------------------------------------------------
LongInt< Int64 > & LongInt< Int64 >::operator = (CSStr text)
{
m_Data = strtoll(text, NULL, 10);
return *this;
}
// ------------------------------------------------------------------------------------------------
CSStr LongInt< Int64 >::ToString()
{
if (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 = strtoll(text, NULL, 10); }
LongInt< Uint64 >::LongInt(CSStr text, SQInteger fall) : m_Data(0), m_Text()
{
m_Data = strtoull(text, NULL, 10);
if (errno == ERANGE)
{
m_Data = ConvTo< Type >::From(fall);
}
}
// ------------------------------------------------------------------------------------------------
LongInt< Uint64 > & LongInt< Uint64 >::operator = (CSStr text)
{
m_Data = strtoull(text, NULL, 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); }
extern void Register_LongInt(HSQUIRRELVM vm);
extern void Register_Math(HSQUIRRELVM vm);
extern void Register_Random(HSQUIRRELVM vm);
// ================================================================================================
void Register_Numeric(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)
);
Register_LongInt(vm);
Register_Math(vm);
Register_Random(vm);
}
} // Namespace:: SqMod

View File

@ -2,535 +2,12 @@
#define _LIBRARY_NUMERIC_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
template < typename T > class LongInt;
// ------------------------------------------------------------------------------------------------
template <> class LongInt< Int64 >
{
public:
// --------------------------------------------------------------------------------------------
typedef Int64 Type;
/* --------------------------------------------------------------------------------------------
*
*/
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);
}
private:
// --------------------------------------------------------------------------------------------
Type m_Data;
SQChar m_Text[32];
};
// ------------------------------------------------------------------------------------------------
template <> class LongInt< Uint64 >
{
public:
// --------------------------------------------------------------------------------------------
typedef Uint64 Type;
/* --------------------------------------------------------------------------------------------
*
*/
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);
}
private:
// --------------------------------------------------------------------------------------------
Type m_Data;
SQChar m_Text[32];
};
// ------------------------------------------------------------------------------------------------
typedef LongInt< Int64 > SLongInt;
typedef LongInt< Uint64 > ULongInt;
} // Namespace:: SqMod

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_

View File

@ -1,6 +1,7 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Math.hpp"
#include "Library/Numeric.hpp"
#include "Library/Numeric/Math.hpp"
#include "Library/Numeric/LongInt.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#include <cmath>

View File

@ -1,12 +1,14 @@
#ifndef _LIBRARY_MATH_HPP_
#define _LIBRARY_MATH_HPP_
#ifndef _LIBRARY_NUMERIC_MATH_HPP_
#define _LIBRARY_NUMERIC_MATH_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
} // Namespace:: SqMod
#endif // _LIBRARY_MATH_HPP_
#endif // _LIBRARY_NUMERIC_MATH_HPP_

View File

@ -1,5 +1,5 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Random.hpp"
#include "Library/Numeric/Random.hpp"
#include "Base/Shared.hpp"
#include "Base/Buffer.hpp"

View File

@ -1,5 +1,5 @@
#ifndef _LIBRARY_RANDOM_HPP_
#define _LIBRARY_RANDOM_HPP_
#ifndef _LIBRARY_NUMERIC_RANDOM_HPP_
#define _LIBRARY_NUMERIC_RANDOM_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
@ -161,4 +161,4 @@ template <> struct RandomVal< bool >
} // Namespace:: SqMod
#endif // _LIBRARY_RANDOM_HPP_
#endif // _LIBRARY_NUMERIC_RANDOM_HPP_

View File

@ -5,7 +5,7 @@
#include "Base/Vector2.hpp"
#include "Base/Vector3.hpp"
#include "Entity/Player.hpp"
#include "Library/Numeric.hpp"
#include "Library/Numeric/LongInt.hpp"
// ------------------------------------------------------------------------------------------------
#include "Misc/Functions.hpp"

View File

@ -38,8 +38,6 @@ extern void Register_Chrono(HSQUIRRELVM vm);
extern void Register_Crypt(HSQUIRRELVM vm);
extern void Register_IO(HSQUIRRELVM vm);
extern void Register_Numeric(HSQUIRRELVM vm);
extern void Register_Math(HSQUIRRELVM vm);
extern void Register_Random(HSQUIRRELVM vm);
extern void Register_String(HSQUIRRELVM vm);
extern void Register_System(HSQUIRRELVM vm);
@ -79,9 +77,7 @@ bool RegisterAPI(HSQUIRRELVM vm)
Register_Chrono(vm);
Register_Crypt(vm);
Register_IO(vm);
Register_Random(vm);
Register_Numeric(vm);
Register_Math(vm);
Register_String(vm);
Register_System(vm);