From 5bee99cb930de1ff22a7d9e2d47bfe8e2f11439c Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 28 Mar 2025 15:36:03 +0100 Subject: [PATCH] Add `ArrayExtensions::First` and fix usage for object pointers --- .../Engine/Core/Collections/ArrayExtensions.h | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Source/Engine/Core/Collections/ArrayExtensions.h b/Source/Engine/Core/Collections/ArrayExtensions.h index 4327e3c15..e8bd9ce45 100644 --- a/Source/Engine/Core/Collections/ArrayExtensions.h +++ b/Source/Engine/Core/Collections/ArrayExtensions.h @@ -60,6 +60,40 @@ public: return INVALID_INDEX; } + /// + /// Searches for the specified object using a custom query and returns it or default value. + /// + /// The target collection. + /// The prediction function. Should return true for the target element to find. + /// The first found item or default value if nothing found. + template + static T First(const Array& obj, const Function predicate) + { + for (int32 i = 0; i < obj.Count(); i++) + { + if (predicate(obj[i])) + return obj[i]; + } + return T(); + } + + /// + /// Searches for the specified object using a custom query and returns it or default value. + /// + /// The target collection. + /// The prediction function. Should return true for the target element to find. + /// The first found item or default value if nothing found. + template + static T* First(const Array& obj, const Function predicate) + { + for (int32 i = 0; i < obj.Count(); i++) + { + if (predicate(obj[i])) + return obj[i]; + } + return nullptr; + } + /// /// The Any operator checks, if there are any elements in the collection matching the predicate. It does not select the element, but returns true if at least one element is matched. /// @@ -94,6 +128,23 @@ public: return true; } + /// + /// All Any operator returns true if all elements match the predicate. It does not select the element, but returns true if all elements are matching. + /// + /// The target collection. + /// The prediction function. + /// True if all elements in the collection matches the prediction, otherwise false. + template + static int32 All(const Array& obj, const Function& predicate) + { + 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. ///