On 25/08/2026 13:49, Richard Biener wrote:
On Tue, Aug 25, 2026 at 1:27 AM feedable via Sourceware Forge
<[email protected]> wrote:
From: feedable <[email protected]>
gcc/ChangeLog:
* vec.h (array_slice::contains): New function.
(array_slice::find): New function.
---
gcc/vec.h | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/gcc/vec.h b/gcc/vec.h
index a2ff7a9b77694..9d25678029832 100644
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -2422,6 +2422,15 @@ public:
const value_type &back () const;
const value_type &operator[] (unsigned int i) const;
+ /* Check if there is an element equal to V exists in the array. */
+ template<class U>
+ bool contains (const U &v);
+
+ /* Check if there is an element in the array that satisfies the predicate
+ PRED. */
+ template<class F>
+ bool find (const F &pred);
Both should have a big fat warning that these are O(n) operations and use
is discouraged. So ... you need those? Absolutely?
Yes, these functions are basically just me extracting loops from RA into
the functions here. Those uses want to be linear in complexity, and that
is logical and should be expected of an unsorted vector.
+
unsigned size () const { return m_size; }
size_t size_bytes () const { return m_size * sizeof (T); }
bool empty () const { return m_size == 0; }
@@ -2486,6 +2495,28 @@ array_slice<T>::operator[] (unsigned int i) const
return m_base[i];
}
+template<typename T>
+template<typename U>
+bool
+array_slice<T>::contains (const U &v)
+{
+ for (const auto &x : *this)
+ if (v == x)
+ return true;
+ return false;
+}
+
+template<typename T>
+template<typename F>
+bool
+array_slice<T>::find (const F &pred)
+{
+ for (const auto &x : *this)
+ if (pred (x))
+ return true;
+ return false;
+}
+
template<typename T>
array_slice<T>
make_array_slice (T *base, unsigned int size)
--
2.54.0