From 3b9b111b2fd73aff0b02efd317b84a865fbfac12 Mon Sep 17 00:00:00 2001 From: Martok Date: Wed, 26 Apr 2023 17:32:55 +0200 Subject: [PATCH] Issue #2142 - Add predicate functions count_if and any_of to ListNode iterator --- js/src/frontend/ParseNode.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/js/src/frontend/ParseNode.h b/js/src/frontend/ParseNode.h index 54d2bef108..2f6311eace 100644 --- a/js/src/frontend/ParseNode.h +++ b/js/src/frontend/ParseNode.h @@ -6,6 +6,8 @@ #ifndef frontend_ParseNode_h #define frontend_ParseNode_h +#include + #include "mozilla/Attributes.h" #include "builtin/ModuleObject.h" @@ -1420,6 +1422,8 @@ class ListNode : public ParseNode } }; + typedef std::function predicate_fun; + #ifdef DEBUG MOZ_MUST_USE bool contains(ParseNode* target) const { MOZ_ASSERT(target); @@ -1460,6 +1464,24 @@ class ListNode : public ParseNode MOZ_ASSERT_IF(end, contains(end)); return range(head(), end); } + + // Predicate functions, like their counterparts in C++17 + size_t count_if(predicate_fun predicate) const { + size_t count = 0; + for (ParseNode* node = head(); node; node = node->pn_next) { + if (predicate(node)) + count++; + } + return count; + } + + bool any_of(predicate_fun predicate) const { + for (ParseNode* node = head(); node; node = node->pn_next) { + if (predicate(node)) + return true; + } + return false; + } }; inline bool