Issue #2142 - Add predicate functions count_if and any_of to ListNode iterator

This commit is contained in:
Martok 2023-04-26 17:32:55 +02:00 committed by roytam1
commit 3b9b111b2f

View file

@ -6,6 +6,8 @@
#ifndef frontend_ParseNode_h
#define frontend_ParseNode_h
#include <functional>
#include "mozilla/Attributes.h"
#include "builtin/ModuleObject.h"
@ -1420,6 +1422,8 @@ class ListNode : public ParseNode
}
};
typedef std::function<bool(ParseNode*)> 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