mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 00:37:15 +01:00
Improve vector container.
Add basic map container.
This commit is contained in:
parent
1829668015
commit
0670a8dadf
@ -74,6 +74,7 @@ add_library(SqModule MODULE SqBase.hpp Main.cpp
|
||||
Library/System/Env.cpp Library/System/Env.hpp
|
||||
Library/System/Path.cpp Library/System/Path.hpp
|
||||
Library/Utils.cpp Library/Utils.hpp
|
||||
Library/Utils/Map.cpp Library/Utils/Map.hpp
|
||||
Library/Utils/Vector.cpp Library/Utils/Vector.hpp
|
||||
# Misc
|
||||
Misc/Broadcast.cpp
|
||||
|
@ -82,6 +82,7 @@ static SQInteger SqExtractIPv4(HSQUIRRELVM vm)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
extern void Register_Map(HSQUIRRELVM vm, Table & ns);
|
||||
extern void Register_Vector(HSQUIRRELVM vm, Table & ns);
|
||||
|
||||
// ================================================================================================
|
||||
@ -89,6 +90,7 @@ void Register_Utils(HSQUIRRELVM vm)
|
||||
{
|
||||
Table ns(vm);
|
||||
|
||||
Register_Map(vm, ns);
|
||||
Register_Vector(vm, ns);
|
||||
|
||||
ns.SquirrelFunc(_SC("ExtractIPv4"), &SqExtractIPv4);
|
||||
|
42
module/Library/Utils/Map.cpp
Normal file
42
module/Library/Utils/Map.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Utils/Map.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQMOD_DECL_TYPENAME(SqMapInteger, _SC("SqMapInteger"))
|
||||
SQMOD_DECL_TYPENAME(SqMapString, _SC("SqMapString"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
template < class T, class U >
|
||||
static void Register_Map(HSQUIRRELVM vm, Table & ns, const SQChar * name)
|
||||
{
|
||||
using Container = SqMap< T >;
|
||||
// --------------------------------------------------------------------------------------------
|
||||
ns.Bind(name,
|
||||
Class< Container, NoCopy< Container > >(vm, U::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &U::Fn)
|
||||
// Properties
|
||||
.Prop(_SC("Null"), &Container::IsNull)
|
||||
.Prop(_SC("Empty"), &Container::Empty)
|
||||
.Prop(_SC("Size"), &Container::Size)
|
||||
// Member Methods
|
||||
.Func(_SC("Get"), &Container::Get)
|
||||
.Func(_SC("Set"), &Container::Set)
|
||||
.Func(_SC("Clear"), &Container::Clear)
|
||||
.Func(_SC("Erase"), &Container::Erase)
|
||||
);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_Map(HSQUIRRELVM vm, Table & ns)
|
||||
{
|
||||
Register_Map< SQInteger, SqMapInteger >(vm, ns, _SC("IntMap"));
|
||||
Register_Map< String, SqMapString >(vm, ns, _SC("StrMap"));
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
270
module/Library/Utils/Map.hpp
Normal file
270
module/Library/Utils/Map.hpp
Normal file
@ -0,0 +1,270 @@
|
||||
#pragma once
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Core/Utility.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <map>
|
||||
#include <random>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Utility used to transform optimal argument type to stored type.
|
||||
*/
|
||||
template < class T > struct SqMapOpt
|
||||
{
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Optimal argument type.
|
||||
*/
|
||||
using Type = T;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Container type.
|
||||
*/
|
||||
using Container = std::map< T, LightObj >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Convert the optimal type to the stored type. Does nothing special in this case.
|
||||
*/
|
||||
inline static Type & Get(Type & k) { return k; }
|
||||
inline static const Type & Get(const Type & k) { return k; }
|
||||
// --------------------------------------------------------------------------------------------
|
||||
inline static void Put(Container & c, LightObj & v, Type & k)
|
||||
{
|
||||
c[k] = v;
|
||||
}
|
||||
inline static void Put(Container & c, LightObj & v, const Type & k)
|
||||
{
|
||||
c[k] = v;
|
||||
}
|
||||
// --------------------------------------------------------------------------------------------
|
||||
inline static void Put(Container & c, LightObj && v, Type & k)
|
||||
{
|
||||
c[k] = std::move(v);
|
||||
}
|
||||
inline static void Put(Container & c, LightObj && v, const Type & k)
|
||||
{
|
||||
c[k] = std::move(v);
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Specialization of SqMapOpt for String type.
|
||||
*/
|
||||
template < > struct SqMapOpt< String >
|
||||
{
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Optimal argument type.
|
||||
*/
|
||||
using Type = StackStrF;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Container type.
|
||||
*/
|
||||
using Container = std::map< String, LightObj >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Convert the optimal type to the stored type.
|
||||
*/
|
||||
inline static String Get(Type & k) { return k.ToStr(); }
|
||||
inline static String Get(const Type & k) { return k.ToStr(); }
|
||||
// --------------------------------------------------------------------------------------------
|
||||
inline static void Put(Container & c, LightObj & v, Type & k)
|
||||
{
|
||||
c[k.ToStr()] = v;
|
||||
}
|
||||
inline static void Put(Container & c, LightObj & v, const Type & k)
|
||||
{
|
||||
c[k.ToStr()] = v;
|
||||
}
|
||||
// --------------------------------------------------------------------------------------------
|
||||
inline static void Put(Container & c, LightObj && v, Type & k)
|
||||
{
|
||||
c[k.ToStr()] = std::move(v);
|
||||
}
|
||||
inline static void Put(Container & c, LightObj && v, const Type & k)
|
||||
{
|
||||
c[k.ToStr()] = std::move(v);
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Wrapper around a std::map of values. Space efficient table.
|
||||
*/
|
||||
template < class T > struct SqMap
|
||||
{
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Type given via the template parameter.
|
||||
*/
|
||||
using Type = T;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The typeof container that will be used.
|
||||
*/
|
||||
using Container = std::map< T, LightObj >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Type stored in the container.
|
||||
*/
|
||||
using Element = typename Container::value_type;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reference to the container.
|
||||
*/
|
||||
using Reference = std::shared_ptr< Container >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Type given used to interact with specialized value cases.
|
||||
*/
|
||||
using Opt = SqMapOpt< T >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Optimal type to receive a value of this type as function argument. Mainly for strings.
|
||||
*/
|
||||
using OptimalType = typename Opt::Type;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Same as OptimalType but preferably with a reference qualifier to avoid copies.
|
||||
*/
|
||||
using OptimalArg = typename std::conditional< std::is_same< T, OptimalType >::value, T, OptimalType & >::type;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reference to the container instance.
|
||||
*/
|
||||
Reference mC;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
SqMap()
|
||||
: mC(std::make_shared< Container >())
|
||||
{
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor from reference.
|
||||
*/
|
||||
explicit SqMap(const Reference & v)
|
||||
: mC(v)
|
||||
{
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor from reference.
|
||||
*/
|
||||
explicit SqMap(Reference && v) noexcept
|
||||
: mC(std::move(v))
|
||||
{
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
SqMap(SqMap &&) noexcept = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destroys the Statement.
|
||||
*/
|
||||
~SqMap() = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Assignment operator.
|
||||
*/
|
||||
SqMap & operator = (const SqMap &) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment.
|
||||
*/
|
||||
SqMap & operator = (SqMap &&) noexcept = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Make sure a container instance is referenced.
|
||||
*/
|
||||
void Validate() const
|
||||
{
|
||||
if (!mC)
|
||||
{
|
||||
STHROWF("Invalid map container instance");
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Make sure a container instance is referenced and return it.
|
||||
*/
|
||||
Container & Valid() const { Validate(); return *mC; }
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Make sure a container instance is referenced and is populated, then return it.
|
||||
*/
|
||||
Container & ValidPop() const
|
||||
{
|
||||
Validate();
|
||||
if (mC->empty())
|
||||
{
|
||||
STHROWF("Vector container is empty");
|
||||
}
|
||||
return *mC;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Check if a container instance is referenced.
|
||||
*/
|
||||
SQMOD_NODISCARD bool IsNull() const
|
||||
{
|
||||
return static_cast< bool >(mC);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve a value from the container.
|
||||
*/
|
||||
SQMOD_NODISCARD const LightObj & Get(OptimalArg k) const
|
||||
{
|
||||
return Valid()[Opt::Get(k)];
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Modify a value from the container.
|
||||
*/
|
||||
void Set(LightObj & v, OptimalArg k)
|
||||
{
|
||||
Opt::Put(Valid(), std::move(v), k);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Check if the container has no elements.
|
||||
*/
|
||||
SQMOD_NODISCARD bool Empty() const
|
||||
{
|
||||
return Valid().empty();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of elements in the container.
|
||||
*/
|
||||
SQMOD_NODISCARD SQInteger Size() const
|
||||
{
|
||||
return static_cast< SQInteger >(Valid().size());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Erase all elements from the container.
|
||||
*/
|
||||
void Clear()
|
||||
{
|
||||
return Valid().clear();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Erase the element with a certain key from the container.
|
||||
*/
|
||||
void Erase(OptimalArg k)
|
||||
{
|
||||
Valid().erase(Opt::Get(k));
|
||||
}
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
@ -15,53 +15,57 @@ SQMOD_DECL_TYPENAME(SqVectorBool, _SC("SqVectorBool"))
|
||||
template < class T, class U >
|
||||
static void Register_Vector(HSQUIRRELVM vm, Table & ns, const SQChar * name)
|
||||
{
|
||||
using Vector = SqVector< T >;
|
||||
using Container = SqVector< T >;
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Class< Vector, NoCopy< Vector > > cls(vm, U::Str);
|
||||
// Constructors
|
||||
cls.Ctor()
|
||||
.template Ctor< SQInteger >()
|
||||
.template Ctor< SQInteger, typename Vector::OptimalArg >()
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &U::Fn)
|
||||
// Properties
|
||||
.Prop(_SC("Null"), &Vector::IsNull)
|
||||
.Prop(_SC("Front"), &Vector::Front)
|
||||
.Prop(_SC("Back"), &Vector::Back)
|
||||
.Prop(_SC("Empty"), &Vector::Empty)
|
||||
.Prop(_SC("Size"), &Vector::Size, &Vector::Resize)
|
||||
.Prop(_SC("Capacity"), &Vector::Capacity, &Vector::Reserve)
|
||||
// Member Methods
|
||||
.Func(_SC("Get"), &Vector::Get)
|
||||
.Func(_SC("Set"), &Vector::Set)
|
||||
.Func(_SC("Resize"), &Vector::ResizeEx)
|
||||
.Func(_SC("Reserve"), &Vector::Reserve)
|
||||
.Func(_SC("Compact"), &Vector::Compact)
|
||||
.Func(_SC("Clear"), &Vector::Clear)
|
||||
.Func(_SC("Push"), &Vector::Push)
|
||||
.Func(_SC("Pop"), &Vector::Pop)
|
||||
.Func(_SC("EraseAt"), &Vector::EraseAt)
|
||||
.Func(_SC("EraseFrom"), &Vector::EraseFrom)
|
||||
.Func(_SC("EraseValue"), &Vector::EraseValue)
|
||||
.Func(_SC("InsertAt"), &Vector::InsertAt)
|
||||
.Func(_SC("Insert"), &Vector::Insert)
|
||||
.Func(_SC("Locate"), &Vector::Locate)
|
||||
.Func(_SC("LocateFrom"), &Vector::LocateFrom)
|
||||
.Func(_SC("Count"), &Vector::Count)
|
||||
.Func(_SC("Equal"), &Vector::Equal)
|
||||
.Func(_SC("Each"), &Vector::Each)
|
||||
.Func(_SC("EachRange"), &Vector::EachRange)
|
||||
.Func(_SC("While"), &Vector::While)
|
||||
.Func(_SC("WhileRange"), &Vector::WhileRange)
|
||||
.Func(_SC("AsArray"), &Vector::AsArray)
|
||||
.Func(_SC("Reverse"), &Vector::Reverse)
|
||||
.Func(_SC("Generate"), &Vector::Generate)
|
||||
.Func(_SC("GenerateSome"), &Vector::GenerateSome)
|
||||
.Func(_SC("GenerateFrom"), &Vector::GenerateFrom);
|
||||
// --------------------------------------------------------------------------------------------
|
||||
ns.Bind(name, cls);
|
||||
// Bind it to the root table with a `Sq` prefix
|
||||
RootTable(vm).Bind(fmt::format("Sq{}", name).c_str(), cls);
|
||||
ns.Bind(name,
|
||||
Class< Container, NoCopy< Container > >(vm, U::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.template Ctor< SQInteger >()
|
||||
.template Ctor< SQInteger, typename Container::OptimalArg >()
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &U::Fn)
|
||||
// Properties
|
||||
.Prop(_SC("Null"), &Container::IsNull)
|
||||
.Prop(_SC("Front"), &Container::Front)
|
||||
.Prop(_SC("Back"), &Container::Back)
|
||||
.Prop(_SC("Empty"), &Container::Empty)
|
||||
.Prop(_SC("Size"), &Container::Size, &Container::Resize)
|
||||
.Prop(_SC("Capacity"), &Container::Capacity, &Container::Reserve)
|
||||
.Prop(_SC("Sorted"), &Container::IsSorted)
|
||||
// Member Methods
|
||||
.Func(_SC("Get"), &Container::Get)
|
||||
.Func(_SC("Set"), &Container::Set)
|
||||
.Func(_SC("Resize"), &Container::ResizeEx)
|
||||
.Func(_SC("Reserve"), &Container::Reserve)
|
||||
.Func(_SC("Compact"), &Container::Compact)
|
||||
.Func(_SC("Clear"), &Container::Clear)
|
||||
.Func(_SC("Push"), &Container::Push)
|
||||
.Func(_SC("Append"), &Container::Push)
|
||||
.Func(_SC("Extend"), &Container::Extend)
|
||||
.Func(_SC("Pop"), &Container::Pop)
|
||||
.Func(_SC("EraseAt"), &Container::EraseAt)
|
||||
.Func(_SC("EraseFrom"), &Container::EraseFrom)
|
||||
.Func(_SC("EraseValue"), &Container::EraseValue)
|
||||
.Func(_SC("InsertAt"), &Container::InsertAt)
|
||||
.Func(_SC("Insert"), &Container::Insert)
|
||||
.Func(_SC("Locate"), &Container::Locate)
|
||||
.Func(_SC("LocateFrom"), &Container::LocateFrom)
|
||||
.Func(_SC("Count"), &Container::Count)
|
||||
.Func(_SC("Equal"), &Container::Equal)
|
||||
.Func(_SC("Slice"), &Container::Slice)
|
||||
.Func(_SC("Each"), &Container::Each)
|
||||
.Func(_SC("EachRange"), &Container::EachRange)
|
||||
.Func(_SC("While"), &Container::While)
|
||||
.Func(_SC("WhileRange"), &Container::WhileRange)
|
||||
.Func(_SC("AsArray"), &Container::AsArray)
|
||||
.Func(_SC("Reverse"), &Container::Reverse)
|
||||
.Func(_SC("Generate"), &Container::Generate)
|
||||
.Func(_SC("GenerateSome"), &Container::GenerateSome)
|
||||
.Func(_SC("GenerateFrom"), &Container::GenerateFrom)
|
||||
.Func(_SC("Sort"), &Container::Sort)
|
||||
.Func(_SC("Shuffle"), &Container::Shuffle)
|
||||
);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include "Core/Utility.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
@ -20,16 +22,25 @@ template < class T > struct SqVectorOpt
|
||||
*/
|
||||
using Type = T;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Container type.
|
||||
*/
|
||||
using Container = std::vector< T >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Convert the optimal type to the stored type. Does nothing special in this case.
|
||||
*/
|
||||
inline static Type & Get(Type & v) { return v; }
|
||||
inline static const Type & Get(const Type & v) { return v; }
|
||||
// --------------------------------------------------------------------------------------------
|
||||
inline static void Put(std::vector< T > & c, SQInteger i, Type & v)
|
||||
{ c[ConvTo< size_t >::From(i)] = v; }
|
||||
inline static void Put(std::vector< T > & c, SQInteger i, const Type & v)
|
||||
{ c[ConvTo< size_t >::From(i)] = v; }
|
||||
inline static void Put(Container & c, SQInteger i, Type & v)
|
||||
{
|
||||
c[ClampL< SQInteger, size_t >(i)] = v;
|
||||
}
|
||||
inline static void Put(Container & c, SQInteger i, const Type & v)
|
||||
{
|
||||
c[ClampL< SQInteger, size_t >(i)] = v;
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
@ -42,20 +53,29 @@ template < > struct SqVectorOpt< String >
|
||||
*/
|
||||
using Type = StackStrF;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Container type.
|
||||
*/
|
||||
using Container = std::vector< String >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Convert the optimal type to the stored type.
|
||||
*/
|
||||
inline static String Get(Type & v) { return v.ToStr(); }
|
||||
inline static String Get(const Type & v) { return v.ToStr(); }
|
||||
// --------------------------------------------------------------------------------------------
|
||||
inline static void Put(std::vector< String > & c, SQInteger i, Type & v)
|
||||
{ c[ConvTo< size_t >::From(i)].assign(v.mPtr, v.GetSize()); }
|
||||
inline static void Put(std::vector< String > & c, SQInteger i, const Type & v)
|
||||
{ c[ConvTo< size_t >::From(i)].assign(v.mPtr, v.GetSize()); }
|
||||
inline static void Put(Container & c, SQInteger i, Type & v)
|
||||
{
|
||||
c[ClampL< SQInteger, size_t >(i)].assign(v.mPtr, v.GetSize());
|
||||
}
|
||||
inline static void Put(Container & c, SQInteger i, const Type & v)
|
||||
{
|
||||
c[ClampL< SQInteger, size_t >(i)].assign(v.mPtr, v.GetSize());
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Wrapper around a vector of values. Space efficient array.
|
||||
* Wrapper around a std::vector of values. Space efficient array.
|
||||
*/
|
||||
template < class T > struct SqVector
|
||||
{
|
||||
@ -65,19 +85,24 @@ template < class T > struct SqVector
|
||||
using Type = T;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* The typeof vector that will be used.
|
||||
* The typeof container that will be used.
|
||||
*/
|
||||
using Vector = std::vector< T >;
|
||||
using Container = std::vector< T >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reference to the vector.
|
||||
* Reference to the container.
|
||||
*/
|
||||
using Reference = std::shared_ptr< Vector >;
|
||||
using Reference = std::shared_ptr< Container >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Type given used to interact with specialized value cases.
|
||||
*/
|
||||
using Opt = SqVectorOpt< T >;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Optimal type to receive a value of this type as function argument. Mainly for strings.
|
||||
*/
|
||||
using OptimalType = typename SqVectorOpt< T >::Type;
|
||||
using OptimalType = typename Opt::Type;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Same as OptimalType but preferably with a reference qualifier to avoid copies.
|
||||
@ -98,7 +123,7 @@ template < class T > struct SqVector
|
||||
* Default constructor.
|
||||
*/
|
||||
SqVector()
|
||||
: mC(std::make_shared< Vector >())
|
||||
: mC(std::make_shared< Container >())
|
||||
{
|
||||
}
|
||||
|
||||
@ -120,6 +145,22 @@ template < class T > struct SqVector
|
||||
ResizeEx(n, v);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor from reference.
|
||||
*/
|
||||
explicit SqVector(const Reference & v)
|
||||
: mC(v)
|
||||
{
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor from reference.
|
||||
*/
|
||||
explicit SqVector(Reference && v) noexcept
|
||||
: mC(std::move(v))
|
||||
{
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
@ -147,23 +188,23 @@ template < class T > struct SqVector
|
||||
{
|
||||
if (!mC)
|
||||
{
|
||||
STHROWF("Invalid container instance");
|
||||
STHROWF("Invalid vector container instance");
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Make sure a container instance is referenced and return it.
|
||||
*/
|
||||
Vector & Valid() const { Validate(); return *mC; }
|
||||
Container & Valid() const { Validate(); return *mC; }
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Make sure an index is within rance and return the container it. Container must exist.
|
||||
*/
|
||||
Vector & ValidIdx(SQInteger i) const
|
||||
Container & ValidIdx(SQInteger i) const
|
||||
{
|
||||
if (static_cast< size_t >(i) >= mC->size())
|
||||
{
|
||||
STHROWF("Invalid container index");
|
||||
STHROWF("Invalid vector container index");
|
||||
}
|
||||
return *mC;
|
||||
}
|
||||
@ -171,12 +212,12 @@ template < class T > struct SqVector
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Make sure a container instance is referenced and is populated, then return it.
|
||||
*/
|
||||
Vector & ValidPop() const
|
||||
Container & ValidPop() const
|
||||
{
|
||||
Validate();
|
||||
if (mC->empty())
|
||||
{
|
||||
STHROWF("Container is empty");
|
||||
STHROWF("Vector container is empty");
|
||||
}
|
||||
return *mC;
|
||||
}
|
||||
@ -192,47 +233,65 @@ template < class T > struct SqVector
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve a value from the container.
|
||||
*/
|
||||
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Get(SQInteger i) const { return ValidIdx(i).at(ConvTo< size_t >::From(i)); }
|
||||
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Get(SQInteger i) const
|
||||
{
|
||||
return ValidIdx(i).at(ClampL< SQInteger, size_t >(i));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Modify a value from the container.
|
||||
*/
|
||||
void Set(SQInteger i, OptimalArg v)
|
||||
{
|
||||
SqVectorOpt< T >::Put(ValidIdx(i), i, v);
|
||||
Opt::Put(ValidIdx(i), i, v);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the first element in the container.
|
||||
*/
|
||||
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Front() const { return ValidPop().front(); }
|
||||
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Front() const
|
||||
{
|
||||
return ValidPop().front();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the last element in the container.
|
||||
*/
|
||||
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Back() const { return ValidPop().back(); }
|
||||
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Back() const
|
||||
{
|
||||
return ValidPop().back();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Check if the container has no elements.
|
||||
*/
|
||||
SQMOD_NODISCARD bool Empty() const { return Valid().empty(); }
|
||||
SQMOD_NODISCARD bool Empty() const
|
||||
{
|
||||
return Valid().empty();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of elements in the container.
|
||||
*/
|
||||
SQMOD_NODISCARD SQInteger Size() const { return static_cast< SQInteger >(Valid().size()); }
|
||||
SQMOD_NODISCARD SQInteger Size() const
|
||||
{
|
||||
return static_cast< SQInteger >(Valid().size());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of elements that the container has currently allocated space for.
|
||||
*/
|
||||
SQMOD_NODISCARD SQInteger Capacity() const { return static_cast< SQInteger >(Valid().capacity()); }
|
||||
SQMOD_NODISCARD SQInteger Capacity() const
|
||||
{
|
||||
return static_cast< SQInteger >(Valid().capacity());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Resize the container to contain a specific amount of elements.
|
||||
*/
|
||||
void Resize(SQInteger n)
|
||||
{
|
||||
return Valid().resize(ConvTo< size_t >::From(n), T());
|
||||
return Valid().resize(ClampL< SQInteger, size_t >(n), T());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -240,33 +299,58 @@ template < class T > struct SqVector
|
||||
*/
|
||||
void ResizeEx(SQInteger n, OptimalArg v)
|
||||
{
|
||||
return Valid().resize(ConvTo< size_t >::From(n), SqVectorOpt< T >::Get(v));
|
||||
return Valid().resize(ClampL< SQInteger, size_t >(n), Opt::Get(v));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Increase the capacity of the vector to a value that's greater or equal to the one specified.
|
||||
* Increase the capacity of the container to a value that's greater or equal to the one specified.
|
||||
*/
|
||||
void Reserve(SQInteger n) { return Valid().reserve(ConvTo< size_t >::From(n)); }
|
||||
void Reserve(SQInteger n)
|
||||
{
|
||||
return Valid().reserve(ClampL< SQInteger, size_t >(n));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Request the removal of unused capacity.
|
||||
*/
|
||||
void Compact() { return Valid().shrink_to_fit(); }
|
||||
void Compact()
|
||||
{
|
||||
return Valid().shrink_to_fit();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Erase all elements from the container.
|
||||
*/
|
||||
void Clear() { return Valid().clear(); }
|
||||
void Clear()
|
||||
{
|
||||
return Valid().clear();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Push a value at the back of the container.
|
||||
*/
|
||||
void Push(OptimalArg v) { return Valid().push_back(SqVectorOpt< T >::Get(v)); }
|
||||
void Push(OptimalArg v)
|
||||
{
|
||||
return Valid().push_back(Opt::Get(v));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Extends the Container by appending all the items in the given container.
|
||||
*/
|
||||
void Extend(SqVector & v)
|
||||
{
|
||||
Validate();
|
||||
v.Validate();
|
||||
mC->insert(mC->end(), v.mC->begin(), v.mC->end());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Pop the last element in the container.
|
||||
*/
|
||||
void Pop() { ValidPop().pop_back(); }
|
||||
void Pop()
|
||||
{
|
||||
ValidPop().pop_back();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Erase the element at a certain position.
|
||||
@ -293,7 +377,7 @@ template < class T > struct SqVector
|
||||
void EraseValue(OptimalArg v)
|
||||
{
|
||||
Validate();
|
||||
mC->erase(std::remove(mC->begin(), mC->end(), SqVectorOpt< T >::Get(v)), mC->end());
|
||||
mC->erase(std::remove(mC->begin(), mC->end(), Opt::Get(v)), mC->end());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -302,7 +386,7 @@ template < class T > struct SqVector
|
||||
void InsertAt(SQInteger i, OptimalArg v)
|
||||
{
|
||||
Validate();
|
||||
mC->insert(ValidIdx(i).begin() + static_cast< size_t >(i), SqVectorOpt< T >::Get(v));
|
||||
mC->insert(ValidIdx(i).begin() + static_cast< size_t >(i), Opt::Get(v));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -311,7 +395,7 @@ template < class T > struct SqVector
|
||||
void Insert(SQInteger i, SQInteger n, OptimalArg v)
|
||||
{
|
||||
Validate();
|
||||
mC->insert(ValidIdx(i).begin() + static_cast< size_t >(i), ConvTo< size_t >::From(n), SqVectorOpt< T >::Get(v));
|
||||
mC->insert(ValidIdx(i).begin() + static_cast< size_t >(i), ClampL< SQInteger, size_t >(n), Opt::Get(v));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -319,7 +403,7 @@ template < class T > struct SqVector
|
||||
*/
|
||||
SQMOD_NODISCARD SQInteger Locate(OptimalArg v) const
|
||||
{
|
||||
auto itr = std::find(Valid().begin(), Valid().end(), SqVectorOpt< T >::Get(v));
|
||||
auto itr = std::find(Valid().begin(), Valid().end(), Opt::Get(v));
|
||||
return itr == mC->end() ? -1 : static_cast< SQInteger >(std::distance(mC->begin(), itr));
|
||||
}
|
||||
|
||||
@ -330,7 +414,7 @@ template < class T > struct SqVector
|
||||
{
|
||||
Validate();
|
||||
auto itr = std::find(ValidIdx(p).begin() + static_cast< size_t >(p),
|
||||
Valid().end(), SqVectorOpt< T >::Get(v));
|
||||
Valid().end(), Opt::Get(v));
|
||||
return itr == mC->end() ? -1 : static_cast< SQInteger >(std::distance(mC->begin(), itr));
|
||||
}
|
||||
|
||||
@ -339,7 +423,7 @@ template < class T > struct SqVector
|
||||
*/
|
||||
SQMOD_NODISCARD SQInteger Count(OptimalArg v) const
|
||||
{
|
||||
return static_cast< SQInteger >(std::count(Valid().begin(), Valid().end(), SqVectorOpt< T >::Get(v)));
|
||||
return static_cast< SQInteger >(std::count(Valid().begin(), Valid().end(), Opt::Get(v)));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -348,7 +432,16 @@ template < class T > struct SqVector
|
||||
SQMOD_NODISCARD bool Equal(SqVector & o) const { return Valid() == o.Valid(); }
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Iterate all values.
|
||||
* Retrieve a portion of this container.
|
||||
*/
|
||||
SqVector Slice(SQInteger p, SQInteger n) const
|
||||
{
|
||||
return SqVector(std::make_shared< Container >(ValidIdx(p).begin() + static_cast< size_t >(p),
|
||||
ValidIdx(p + n).begin() + static_cast< size_t >(p + n)));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Iterate all values through a functor.
|
||||
*/
|
||||
void Each(Function & fn) const
|
||||
{
|
||||
@ -360,7 +453,7 @@ template < class T > struct SqVector
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Iterate values in range.
|
||||
* Iterate values in range through a functor.
|
||||
*/
|
||||
void EachRange(SQInteger p, SQInteger n, Function & fn) const
|
||||
{
|
||||
@ -373,7 +466,7 @@ template < class T > struct SqVector
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Iterate all values until stopped.
|
||||
* Iterate all values through a functor until stopped (i.e false is returned).
|
||||
*/
|
||||
void While(Function & fn) const
|
||||
{
|
||||
@ -390,7 +483,7 @@ template < class T > struct SqVector
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Iterate values in range until stopped.
|
||||
* Iterate values in range through a functor until stopped (i.e false is returned).
|
||||
*/
|
||||
void WhileRange(SQInteger p, SQInteger n, Function & fn) const
|
||||
{
|
||||
@ -420,8 +513,8 @@ template < class T > struct SqVector
|
||||
arr.Reserve(static_cast< SQInteger >(mC->size()));
|
||||
// Make sure to preserve the stack
|
||||
StackGuard sg(SqVM());
|
||||
// Populate the array with vector elements
|
||||
arr.AppendFromCounted([](HSQUIRRELVM vm, SQInteger i, const Vector & v) -> bool {
|
||||
// Populate the array with container elements
|
||||
arr.AppendFromCounted([](HSQUIRRELVM vm, SQInteger i, const Container & v) -> bool {
|
||||
if (static_cast< size_t >(i) < v.size())
|
||||
{
|
||||
Var< T >::push(vm, v[static_cast< size_t >(i)]);
|
||||
@ -438,15 +531,16 @@ template < class T > struct SqVector
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Reverse the order of elements in the container.
|
||||
*/
|
||||
void Reverse()
|
||||
SqVector & Reverse()
|
||||
{
|
||||
std::reverse(Valid().begin(), Valid().end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Generate new elements at the back of the container.
|
||||
*/
|
||||
void Generate(LightObj & ctx, Function & fn)
|
||||
SqVector & Generate(LightObj & ctx, Function & fn)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
@ -459,12 +553,13 @@ template < class T > struct SqVector
|
||||
// Extract the value from object
|
||||
mC->push_back(ret.Cast< T >());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Generate new elements at the back of the container.
|
||||
*/
|
||||
void GenerateSome(SQInteger n, LightObj & ctx, Function & fn)
|
||||
SqVector & GenerateSome(SQInteger n, LightObj & ctx, Function & fn)
|
||||
{
|
||||
while (n--)
|
||||
{
|
||||
@ -472,12 +567,13 @@ template < class T > struct SqVector
|
||||
// Extract the value from object
|
||||
mC->push_back(ret.Cast< T >());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Generate new elements at specified position.
|
||||
*/
|
||||
void GenerateFrom(SQInteger p, SQInteger n, LightObj & ctx, Function & fn)
|
||||
SqVector & GenerateFrom(SQInteger p, SQInteger n, LightObj & ctx, Function & fn)
|
||||
{
|
||||
Validate();
|
||||
if (static_cast< size_t >(p) >= mC->size())
|
||||
@ -490,6 +586,38 @@ template < class T > struct SqVector
|
||||
// Extract the value from object and insert it
|
||||
mC->insert(mC->begin() + i, ret.Cast< T >());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Sort the elements from the container.
|
||||
*/
|
||||
SqVector & Sort()
|
||||
{
|
||||
Validate();
|
||||
std::sort(mC->begin(), mC->end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Check if the elements from the container are sorted.
|
||||
*/
|
||||
void IsSorted()
|
||||
{
|
||||
Validate();
|
||||
std::is_sorted(mC->begin(), mC->end());
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Shuffle the elements from the container.
|
||||
*/
|
||||
SqVector & Shuffle()
|
||||
{
|
||||
Validate();
|
||||
std::random_device rd;
|
||||
std::mt19937 g(rd());
|
||||
std::shuffle(mC->begin(), mC->end(), g);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user