Skip to content

Commit

Permalink
Make TArray take functions by forward-reference and call them by invoke
Browse files Browse the repository at this point in the history
(1. avoids copying for lambdas, and 2. allows passing methods)
  • Loading branch information
RicardoLuis0 committed Mar 17, 2024
1 parent ce479e0 commit 458d81c
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/common/utility/tarray.h
Expand Up @@ -55,6 +55,7 @@
#include <utility>
#include <iterator>
#include <algorithm>
#include <functional>

#if !defined(_WIN32)
#include <inttypes.h> // for intptr_t
Expand Down Expand Up @@ -366,11 +367,11 @@ class TArray
}

template<typename Func>
bool IsSorted(Func lt)
bool IsSorted(Func &&lt)
{
for(unsigned i = 1; i < Count; i++)
{
if(lt(Array[i], Array[i-1])) return false;
if(std::invoke(lt, Array[i], Array[i-1])) return false;
}
return true;
}
Expand Down Expand Up @@ -418,7 +419,7 @@ class TArray
//
// exact = false returns the closest match, to be used for, ex., insertions, exact = true returns Size() when no match, like Find does
template<typename Func>
unsigned int SortedFind(const T& item, Func lt, bool exact = true) const
unsigned int SortedFind(const T& item, Func &&lt, bool exact = true) const
{
if(Count == 0) return 0;
if(Count == 1) return lt(item, Array[0]) ? 0 : 1;
Expand All @@ -430,11 +431,11 @@ class TArray
{
int mid = lo + ((hi - lo) / 2);

if(lt(Array[mid], item))
if(std::invoke(lt, Array[mid], item))
{
lo = mid + 1;
}
else if(lt(item, Array[mid]))
else if(std::invoke(lt, item, Array[mid]))
{
if(mid == 0) break; // prevent negative overflow due to unsigned numbers
hi = mid - 1;
Expand All @@ -450,7 +451,7 @@ class TArray
}
else
{
return (lo == Count || lt(item, Array[lo])) ? lo : lo + 1;
return (lo == Count || std::invoke(lt, item, Array[lo])) ? lo : lo + 1;
}
}

Expand All @@ -466,12 +467,24 @@ class TArray
}

template<class Func>
unsigned int FindEx(Func compare) const
bool Contains(const T& item, Func &&compare) const
{
unsigned int i;
for(i = 0;i < Count;++i)
{
if(std::invoke(compare, Array[i], item))
return true;
}
return false;
}

template<class Func>
unsigned int FindEx(Func &&compare) const
{
unsigned int i;
for (i = 0; i < Count; ++i)
{
if (compare(Array[i]))
if (std::invoke(compare, Array[i]))
break;
}
return i;
Expand Down Expand Up @@ -569,9 +582,9 @@ class TArray
}

template<typename Func>
unsigned SortedAddUnique(const T& obj, Func lt)
unsigned SortedAddUnique(const T& obj, Func &&lt)
{
auto f = SortedFind(obj, lt, true);
auto f = SortedFind(obj, std::forward<Func>(lt), true);
if (f == Size()) Push(obj);
return f;
}
Expand All @@ -591,9 +604,9 @@ class TArray
}

template<typename Func>
bool SortedDelete(const T& obj, Func lt)
bool SortedDelete(const T& obj, Func &&lt)
{
auto f = SortedFind(obj, lt, true);
auto f = SortedFind(obj, std::forward<Func>(lt), true);
if (f == Size())
{
Delete(f);
Expand Down Expand Up @@ -691,9 +704,9 @@ class TArray
}

template<typename Func>
void SortedInsert (const T &item, Func lt)
void SortedInsert (const T &item, Func &&lt)
{
Insert (SortedFind (item, lt, false), item);
Insert (SortedFind (item, std::forward<Func>(lt), false), item);
}

void ShrinkToFit ()
Expand Down

0 comments on commit 458d81c

Please sign in to comment.