From f654d507e5b030190a0982406af75397df219d01 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 3 Dec 2023 14:09:58 +0100 Subject: [PATCH] Add `Where`, `Select` and `RemoveAll` to `ArrayExtensions` --- .../Engine/Core/Collections/ArrayExtensions.h | 98 +++++++++++++++++-- 1 file changed, 91 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Core/Collections/ArrayExtensions.h b/Source/Engine/Core/Collections/ArrayExtensions.h index 2ae6da9c8..99d454906 100644 --- a/Source/Engine/Core/Collections/ArrayExtensions.h +++ b/Source/Engine/Core/Collections/ArrayExtensions.h @@ -55,9 +55,7 @@ public: for (int32 i = 0; i < obj.Count(); i++) { if (predicate(obj[i])) - { return i; - } } return INVALID_INDEX; } @@ -74,9 +72,7 @@ public: for (int32 i = 0; i < obj.Count(); i++) { if (predicate(obj[i])) - { return true; - } } return false; } @@ -93,13 +89,101 @@ public: for (int32 i = 0; i < obj.Count(); i++) { if (!predicate(obj[i])) - { return false; - } } return true; } + /// + /// Filters a sequence of values based on a predicate. + /// + /// The target collection. + /// The prediction function. Return true for elements that should be included in result list. + /// The result list with items that passed the predicate. + template + static void Where(const Array& obj, const Function& predicate, Array& result) + { + for (const T& i : obj) + { + if (predicate(i)) + result.Add(i); + } + } + + /// + /// Filters a sequence of values based on a predicate. + /// + /// The target collection. + /// The prediction function. Return true for elements that should be included in result list. + /// The result list with items that passed the predicate. + template + static Array Where(const Array& obj, const Function& predicate) + { + Array result; + Where(obj, predicate, result); + return result; + } + + /// + /// Projects each element of a sequence into a new form. + /// + /// The target collection. + /// A transform function to apply to each source element; the second parameter of the function represents the index of the source element. + /// The result list whose elements are the result of invoking the transform function on each element of source. + template + static void Select(const Array& obj, const Function& selector, Array& result) + { + for (const TSource& i : obj) + result.Add(MoveTemp(selector(i))); + } + + /// + /// Projects each element of a sequence into a new form. + /// + /// The target collection. + /// A transform function to apply to each source element; the second parameter of the function represents the index of the source element. + /// The result list whose elements are the result of invoking the transform function on each element of source. + template + static Array Select(const Array& obj, const Function& selector) + { + Array result; + Select(obj, selector, result); + return result; + } + + /// + /// Removes all the elements that match the conditions defined by the specified predicate. + /// + /// The target collection to modify. + /// A transform function that defines the conditions of the elements to remove. + template + static void RemoveAll(Array& obj, const Function& predicate) + { + for (int32 i = obj.Count() - 1; i >= 0; i--) + { + if (predicate(obj[i])) + obj.RemoveAtKeepOrder(i); + } + } + + /// + /// Removes all the elements that match the conditions defined by the specified predicate. + /// + /// The target collection to process. + /// A transform function that defines the conditions of the elements to remove. + /// The result list whose elements are the result of invoking the transform function on each element of source. + template + static Array RemoveAll(const Array& obj, const Function& predicate) + { + Array result; + for (const T& i : obj) + { + if (!predicate(i)) + result.Ass(i); + } + return result; + } + /// /// Groups the elements of a sequence according to a specified key selector function. /// @@ -109,7 +193,7 @@ public: template static void GroupBy(const Array& obj, const Function& keySelector, Array, AllocationType>& result) { - Dictionary> data(static_cast(obj.Count() * 3.0f)); + Dictionary> data; for (int32 i = 0; i < obj.Count(); i++) { const TKey key = keySelector(obj[i]);