Browse Source

add some util functions to arrays

Kolja Strohm 8 months ago
parent
commit
4c2563af1e
1 changed files with 58 additions and 0 deletions
  1. 58 0
      Array.h

+ 58 - 0
Array.h

@@ -529,6 +529,35 @@ namespace Framework
             return -1;
         }
 
+        bool anyMatch(std::function<bool(const TYP element)> predicate) const
+        {
+            for (TYP t : *this)
+            {
+                if (predicate(t)) return 1;
+            }
+            return 0;
+        }
+
+        bool allMatch(std::function<bool(const TYP element)> predicate) const
+        {
+            for (TYP t : *this)
+            {
+                if (!predicate(t)) return 0;
+            }
+            return 1;
+        }
+
+        int findIndex(std::function<bool(const TYP element)> predicate) const
+        {
+            int index = 0;
+            for (TYP t : *this)
+            {
+                if (predicate(t)) return index;
+                index++;
+            }
+            return -1;
+        }
+
         Array& operator=(const Array& arr)
         {
             leeren();
@@ -845,6 +874,35 @@ namespace Framework
 			return -1;
         }
 
+        bool anyMatch(std::function<bool(const TYP* zElement)> predicate) const
+        {
+            for (TYP* t : *this)
+            {
+                if (predicate(t)) return 1;
+            }
+            return 0;
+        }
+
+        bool allMatch(std::function<bool(const TYP* zElement)> predicate) const
+        {
+            for (TYP* t : *this)
+            {
+                if (!predicate(t)) return 0;
+            }
+            return 1;
+        }
+
+        int findIndex(std::function<bool(const TYP* zElement)> predicate) const
+        {
+            int index = 0;
+            for (TYP* t : *this)
+            {
+                if (predicate(t)) return index;
+                index++;
+            }
+            return -1;
+        }
+
         RCArray& operator=(const RCArray& arr)
         {
             leeren();