From 458d81cede493a8e3b2f2d7a64223d206e935ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Lu=C3=ADs=20Vaz=20Silva?= Date: Sun, 17 Mar 2024 17:49:16 -0300 Subject: [PATCH] Make TArray take functions by forward-reference and call them by invoke (1. avoids copying for lambdas, and 2. allows passing methods) --- src/common/utility/tarray.h | 41 ++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/common/utility/tarray.h b/src/common/utility/tarray.h index b642f3c87ef..bce3ba3ea1c 100644 --- a/src/common/utility/tarray.h +++ b/src/common/utility/tarray.h @@ -55,6 +55,7 @@ #include #include #include +#include #if !defined(_WIN32) #include // for intptr_t @@ -366,11 +367,11 @@ class TArray } template - bool IsSorted(Func lt) + bool IsSorted(Func &<) { 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; } @@ -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 - unsigned int SortedFind(const T& item, Func lt, bool exact = true) const + unsigned int SortedFind(const T& item, Func &<, bool exact = true) const { if(Count == 0) return 0; if(Count == 1) return lt(item, Array[0]) ? 0 : 1; @@ -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; @@ -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; } } @@ -466,12 +467,24 @@ class TArray } template - 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 + 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; @@ -569,9 +582,9 @@ class TArray } template - unsigned SortedAddUnique(const T& obj, Func lt) + unsigned SortedAddUnique(const T& obj, Func &<) { - auto f = SortedFind(obj, lt, true); + auto f = SortedFind(obj, std::forward(lt), true); if (f == Size()) Push(obj); return f; } @@ -591,9 +604,9 @@ class TArray } template - bool SortedDelete(const T& obj, Func lt) + bool SortedDelete(const T& obj, Func &<) { - auto f = SortedFind(obj, lt, true); + auto f = SortedFind(obj, std::forward(lt), true); if (f == Size()) { Delete(f); @@ -691,9 +704,9 @@ class TArray } template - void SortedInsert (const T &item, Func lt) + void SortedInsert (const T &item, Func &<) { - Insert (SortedFind (item, lt, false), item); + Insert (SortedFind (item, std::forward(lt), false), item); } void ShrinkToFit ()