diff --git a/regamedll/public/utlarray.h b/regamedll/public/utlarray.h index 6bdb4136..a65fd9b8 100644 --- a/regamedll/public/utlarray.h +++ b/regamedll/public/utlarray.h @@ -20,6 +20,8 @@ #include "tier0/platform.h" #include "tier0/dbg.h" +#include + #define FOR_EACH_ARRAY(vecName, iteratorName)\ for (int iteratorName = 0; (vecName).IsUtlArray && iteratorName < (vecName).Count(); iteratorName++) @@ -71,6 +73,15 @@ public: bool HasElement(const T &src) const; + // sort using std:: and expecting a "<" function to be defined for the type + void Sort(); + void Sort(bool (*pfnLessFunc)(const T &src1, const T &src2)); + void Sort(int (__cdecl *pfnCompare)(const T *, const T *)); + + // sort using std:: with a predicate. e.g. [] -> bool (const T &a, const T &b) const { return a < b; } + template + void SortPredicate(F &&predicate); + protected: T m_Memory[MAX_SIZE]; }; @@ -180,6 +191,43 @@ inline int CUtlArray::InvalidIndex() return -1; } +// Sort methods +template +void CUtlArray::Sort() +{ + std::sort(Base(), Base() + Count()); +} + +template +void CUtlArray::Sort(bool (*pfnLessFunc)(const T &src1, const T &src2)) +{ + std::sort(Base(), Base() + Count(), + [pfnLessFunc](const T &a, const T &b) -> bool + { + if (&a == &b) + return false; + + return (*pfnLessFunc)(a, b); + }); +} + +template +void CUtlArray::Sort(int (__cdecl *pfnCompare)(const T *, const T *)) +{ + typedef int (__cdecl *QSortCompareFunc_t)(const void *, const void *); + if (Count() <= 1) + return; + + qsort(Base(), Count(), sizeof(T), (QSortCompareFunc_t)(pfnCompare)); +} + +template +template +void CUtlArray::SortPredicate(F &&predicate) +{ + std::sort(Base(), Base() + Count(), predicate); +} + template void CUtlArray::CopyArray(const T *pArray, size_t count) {