1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 16:57:16 +01:00
SqMod/module/Library/Utils/Vector.hpp

697 lines
24 KiB
C++
Raw Normal View History

2021-01-30 23:16:10 +01:00
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Core/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include "Poco/SharedPtr.h"
2021-01-30 23:16:10 +01:00
// ------------------------------------------------------------------------------------------------
#include <vector>
#include <random>
2021-01-30 23:16:10 +01:00
#include <iterator>
#include <algorithm>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Utility used to transform optimal argument type to stored type.
*/
template < class T > struct SqVectorOpt
{
/* --------------------------------------------------------------------------------------------
* Optimal argument type.
*/
using Type = T;
/* --------------------------------------------------------------------------------------------
* Container type.
*/
using Container = std::vector< T >;
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* 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(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;
}
2021-01-30 23:16:10 +01:00
};
/* ------------------------------------------------------------------------------------------------
* Specialization of SqVectorOpt for String type.
*/
template < > struct SqVectorOpt< String >
{
/* --------------------------------------------------------------------------------------------
* Optimal argument type.
*/
using Type = StackStrF;
/* --------------------------------------------------------------------------------------------
* Container type.
*/
using Container = std::vector< String >;
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* 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(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());
}
2021-01-30 23:16:10 +01:00
};
/* ------------------------------------------------------------------------------------------------
* Wrapper around a std::vector of values. Space efficient array.
2021-01-30 23:16:10 +01:00
*/
template < class T > struct SqVector
{
/* --------------------------------------------------------------------------------------------
* Type given via the template parameter.
*/
using Type = T;
/* --------------------------------------------------------------------------------------------
* The typeof container that will be used.
*/
using Container = std::vector< T >;
/* --------------------------------------------------------------------------------------------
* Reference to the container.
2021-01-30 23:16:10 +01:00
*/
using Reference = Poco::SharedPtr< Container >;
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Type given used to interact with specialized value cases.
2021-01-30 23:16:10 +01:00
*/
using Opt = SqVectorOpt< T >;
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Optimal type to receive a value of this type as function argument. Mainly for strings.
*/
using OptimalType = typename Opt::Type;
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* 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;
/* --------------------------------------------------------------------------------------------
* A vector of bool needs special treatment from the other types.
*/
using ReturnType = typename std::conditional< std::is_same< T, bool >::value, T, T & >::type;
/* --------------------------------------------------------------------------------------------
* Reference to the container instance.
*/
Reference mC;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
SqVector()
: mC(Poco::makeShared< Container >())
2021-01-30 23:16:10 +01:00
{
}
/* --------------------------------------------------------------------------------------------
* Construct with initial capacity. No element is created.
*/
explicit SqVector(SQInteger n)
: SqVector()
{
mC->reserve(static_cast< size_t >(n));
}
/* --------------------------------------------------------------------------------------------
* Construct with initial size. Filled with copies of specified element.
*/
SqVector(SQInteger n, OptimalArg v)
: 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))
{
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
SqVector(SqVector &&) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Destroys the Statement.
*/
~SqVector() = default;
/* --------------------------------------------------------------------------------------------
* Assignment operator.
*/
SqVector & operator = (const SqVector &) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment.
*/
SqVector & operator = (SqVector &&) noexcept = default;
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced.
*/
void Validate() const
{
if (!mC)
{
STHROWF("Invalid vector container instance");
2021-01-30 23:16:10 +01:00
}
}
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced and return it.
*/
Container & Valid() { Validate(); return *mC; }
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced and return it.
*/
const Container & Valid() const { Validate(); return *mC; }
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced and return its reference.
*/
Reference & ValidRef() { Validate(); return mC; }
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced and return its reference.
*/
const Reference & ValidRef() const { Validate(); return mC; }
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
2021-03-28 21:36:08 +02:00
* Make sure an index is within range and return the container. Container must exist.
2021-01-30 23:16:10 +01:00
*/
Container & ValidIdx(SQInteger i)
2021-01-30 23:16:10 +01:00
{
if (static_cast< size_t >(i) >= mC->size())
{
2021-02-03 16:50:39 +01:00
STHROWF("Invalid vector container index({})", i);
2021-01-30 23:16:10 +01:00
}
return *mC;
}
/* --------------------------------------------------------------------------------------------
2021-03-28 21:36:08 +02:00
* Make sure an index is within range and return the container. Container must exist.
*/
const Container & ValidIdx(SQInteger i) const
{
if (static_cast< size_t >(i) >= mC->size())
{
2021-02-03 16:50:39 +01:00
STHROWF("Invalid vector container index({})", i);
}
return *mC;
}
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced and is populated, then return it.
*/
Container & ValidPop()
{
Validate();
if (mC->empty())
{
STHROWF("Vector container is empty");
}
return *mC;
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Make sure a container instance is referenced and is populated, then return it.
*/
const Container & ValidPop() const
2021-01-30 23:16:10 +01:00
{
Validate();
if (mC->empty())
{
STHROWF("Vector container is empty");
2021-01-30 23:16:10 +01:00
}
return *mC;
}
/* --------------------------------------------------------------------------------------------
* Check if a container instance is referenced.
*/
SQMOD_NODISCARD bool IsNull() const
{
2021-02-03 22:16:59 +01:00
return !mC;
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve a value from the container.
*/
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Get(SQInteger i)
{
return ValidIdx(i).at(ClampL< SQInteger, size_t >(i));
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Modify a value from the container.
*/
void Set(SQInteger i, OptimalArg v)
{
Opt::Put(ValidIdx(i), i, v);
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve the first element in the container.
*/
2021-03-28 21:36:08 +02:00
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Front()
{
return ValidPop().front();
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the last element in the container.
*/
2021-03-28 21:36:08 +02:00
SQMOD_NODISCARD typename std::add_const< ReturnType >::type Back()
{
return ValidPop().back();
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Check if the container has no elements.
*/
SQMOD_NODISCARD bool Empty() const
{
return Valid().empty();
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the number of elements in the container.
*/
SQMOD_NODISCARD SQInteger Size() const
{
return static_cast< SQInteger >(Valid().size());
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the number of elements that the container has currently allocated space for.
*/
SQMOD_NODISCARD SQInteger Capacity() const
{
return static_cast< SQInteger >(Valid().capacity());
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Resize the container to contain a specific amount of elements.
*/
void Resize(SQInteger n)
{
2021-01-31 13:13:11 +01:00
Valid().resize(ClampL< SQInteger, size_t >(n), T());
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Resize the container to contain a specific amount of elements.
*/
2021-01-31 13:13:11 +01:00
SqVector & ResizeEx(SQInteger n, OptimalArg v)
2021-01-30 23:16:10 +01:00
{
2021-01-31 13:13:11 +01:00
Valid().resize(ClampL< SQInteger, size_t >(n), Opt::Get(v));
return *this;
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Increase the capacity of the container to a value that's greater or equal to the one specified.
2021-01-30 23:16:10 +01:00
*/
2021-01-31 13:13:11 +01:00
SqVector & Reserve(SQInteger n)
{
2021-01-31 13:13:11 +01:00
Valid().reserve(ClampL< SQInteger, size_t >(n));
return *this;
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Request the removal of unused capacity.
*/
void Compact()
{
2021-01-31 13:13:11 +01:00
Valid().shrink_to_fit();
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Erase all elements from the container.
*/
void Clear()
{
2021-01-31 13:13:11 +01:00
Valid().clear();
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Push a value at the back of the container.
*/
void Push(OptimalArg v)
{
2021-01-31 13:13:11 +01:00
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());
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Pop the last element in the container.
*/
void Pop()
{
ValidPop().pop_back();
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Erase the element at a certain position.
*/
void EraseAt(SQInteger i)
{
Validate();
mC->erase(ValidIdx(i).begin() + static_cast< size_t >(i));
}
/* --------------------------------------------------------------------------------------------
* Erase a certain amount of elements starting from a specific position.
*/
void EraseFrom(SQInteger i, SQInteger n)
{
Validate();
mC->erase(ValidIdx(i).begin() + static_cast< size_t >(i),
ValidIdx(i + n).begin() + static_cast< size_t >(i + n));
}
/* --------------------------------------------------------------------------------------------
* Erase all occurrences of value from the container.
*/
void EraseValue(OptimalArg v)
{
Validate();
mC->erase(std::remove(mC->begin(), mC->end(), Opt::Get(v)), mC->end());
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Insert a specific value starting from a certain position.
*/
void InsertAt(SQInteger i, OptimalArg v)
{
Validate();
mC->insert(ValidIdx(i).begin() + static_cast< size_t >(i), Opt::Get(v));
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Insert a certain amount of copies of a value starting from a specific position.
*/
void Insert(SQInteger i, SQInteger n, OptimalArg v)
{
Validate();
mC->insert(ValidIdx(i).begin() + static_cast< size_t >(i), ClampL< SQInteger, size_t >(n), Opt::Get(v));
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Locate the position of a value.
*/
SQMOD_NODISCARD SQInteger Locate(OptimalArg v)
2021-01-30 23:16:10 +01:00
{
auto itr = std::find(Valid().begin(), Valid().end(), Opt::Get(v));
2021-01-30 23:16:10 +01:00
return itr == mC->end() ? -1 : static_cast< SQInteger >(std::distance(mC->begin(), itr));
}
/* --------------------------------------------------------------------------------------------
* Locate the position of a value starting from an offset.
*/
SQMOD_NODISCARD SQInteger LocateFrom(SQInteger p, OptimalArg v)
2021-01-30 23:16:10 +01:00
{
Validate();
auto itr = std::find(ValidIdx(p).begin() + static_cast< size_t >(p),
Valid().end(), Opt::Get(v));
2021-01-30 23:16:10 +01:00
return itr == mC->end() ? -1 : static_cast< SQInteger >(std::distance(mC->begin(), itr));
}
/* --------------------------------------------------------------------------------------------
* Count the occurrences of a value in the container.
*/
SQMOD_NODISCARD SQInteger Count(OptimalArg v) const
{
return static_cast< SQInteger >(std::count(Valid().begin(), Valid().end(), Opt::Get(v)));
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* See if values are the same as another container.
*/
SQMOD_NODISCARD bool Equal(SqVector & o) const { return Valid() == o.Valid(); }
/* --------------------------------------------------------------------------------------------
* Retrieve a portion of this container.
*/
2021-02-03 23:48:34 +01:00
LightObj Slice(SQInteger p, SQInteger n) const
{
2021-02-03 23:48:34 +01:00
return LightObj(SqTypeIdentity< SqVector >{}, SqVM(), Poco::makeShared< 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.
2021-01-30 23:16:10 +01:00
*/
void Each(Function & fn) const
{
Validate();
for (const auto & e : *mC)
{
fn.Execute(e);
}
}
/* --------------------------------------------------------------------------------------------
* Iterate values in range through a functor.
2021-01-30 23:16:10 +01:00
*/
void EachRange(SQInteger p, SQInteger n, Function & fn) const
{
Validate();
std::for_each(ValidIdx(p).begin() + static_cast< size_t >(p),
ValidIdx(p + n).begin() + static_cast< size_t >(p + n),
[&](const T & e) {
fn.Execute(e);
});
}
/* --------------------------------------------------------------------------------------------
* Iterate all values through a functor until stopped (i.e false is returned).
2021-01-30 23:16:10 +01:00
*/
void While(Function & fn) const
{
Validate();
for (const auto & e : *mC)
{
auto ret = fn.Eval(e);
// (null || true) == continue & false == break
if (!ret.IsNull() || !ret.template Cast< bool >())
{
break;
}
}
}
/* --------------------------------------------------------------------------------------------
* Iterate values in range through a functor until stopped (i.e false is returned).
2021-01-30 23:16:10 +01:00
*/
void WhileRange(SQInteger p, SQInteger n, Function & fn) const
{
Validate();
auto itr = ValidIdx(p).begin() + static_cast< size_t >(p);
auto end = ValidIdx(p + n).begin() + static_cast< size_t >(p + n);
for (; itr != end; ++itr)
{
auto ret = fn.Eval(*itr);
// (null || true) == continue & false == break
if (!ret.IsNull() || !ret.template Cast< bool >())
{
break;
}
}
}
/* --------------------------------------------------------------------------------------------
* Create a script array with the elements from the container.
*/
SQMOD_NODISCARD Array AsArray() const
{
Validate();
// Create a script array
Array arr(SqVM());
// Reserve space in advance
arr.Reserve(static_cast< SQInteger >(mC->size()));
// Make sure to preserve the stack
StackGuard sg(SqVM());
// Populate the array with container elements
arr.AppendFromCounted([](HSQUIRRELVM vm, SQInteger i, const Container & v) -> bool {
2021-01-30 23:16:10 +01:00
if (static_cast< size_t >(i) < v.size())
{
Var< T >::push(vm, v[static_cast< size_t >(i)]);
// There's a value on the stack
return true;
}
// No more elements
return false;
}, *mC);
// Return the array
return arr;
}
/* --------------------------------------------------------------------------------------------
* Reverse the order of elements in the container.
*/
SqVector & Reverse()
2021-01-30 23:16:10 +01:00
{
2021-03-28 21:36:08 +02:00
std::reverse(Valid().begin(), Valid().end());
return *this;
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Generate new elements at the back of the container.
*/
SqVector & Generate(LightObj & ctx, Function & fn)
2021-01-30 23:16:10 +01:00
{
for (;;)
{
auto ret = fn.Eval(ctx);
// null == break
if (ret.IsNull())
{
break;
}
// Extract the value from object
mC->push_back(ret.Cast< T >());
}
return *this;
2021-01-30 23:16:10 +01:00
}
/* --------------------------------------------------------------------------------------------
* Generate new elements at the back of the container.
*/
SqVector & GenerateSome(SQInteger n, LightObj & ctx, Function & fn)
2021-01-30 23:16:10 +01:00
{
while (n--)
{
auto ret = fn.Eval(ctx);
// Extract the value from object
mC->push_back(ret.Cast< T >());
}
return *this;
2021-01-30 23:16:10 +01:00
}
2021-01-30 23:16:10 +01:00
/* --------------------------------------------------------------------------------------------
* Generate new elements at specified position.
*/
SqVector & GenerateFrom(SQInteger p, SQInteger n, LightObj & ctx, Function & fn)
2021-01-30 23:16:10 +01:00
{
Validate();
if (static_cast< size_t >(p) >= mC->size())
{
2021-03-28 21:36:08 +02:00
STHROWF("Invalid container index ({} >= {})", p, mC->size());
2021-01-30 23:16:10 +01:00
}
for (auto i = static_cast< size_t >(p); n--; ++i)
{
auto ret = fn.Eval(ctx);
// Extract the value from object and insert it
mC->insert(mC->begin() + i, ret.Cast< T >());
}
return *this;
}
2021-01-31 13:55:49 +01:00
/* --------------------------------------------------------------------------------------------
* Generate new elements at specified position.
*/
SqVector & GenerateBetween(SQInteger p, SQInteger n, LightObj & ctx, Function & fn)
{
Validate();
if (static_cast< size_t >(p) >= mC->size())
{
2021-03-28 21:36:08 +02:00
STHROWF("Invalid container index ({} >= {})", p, mC->size());
2021-01-31 13:55:49 +01:00
}
else if (static_cast< size_t >(p + n) >= mC->size())
{
2021-03-28 21:36:08 +02:00
STHROWF("Invalid container index ({} >= {})", p + n, mC->size());
2021-01-31 13:55:49 +01:00
}
for (n = (p + n); p <= n; ++p)
{
auto ret = fn.Eval(ctx);
// Extract the value from object and assign it
mC->at(static_cast< size_t >(p)) = 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.
*/
2021-02-03 22:08:18 +01:00
bool IsSorted()
{
Validate();
2021-02-03 22:11:29 +01:00
if (std::is_same< T, bool >::value)
{
STHROWF("This functionality is not implemented for booleans");
}
2021-02-03 22:08:18 +01:00
return 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;
2021-01-30 23:16:10 +01:00
}
};
} // Namespace:: SqMod