blob: 1868d30b471e9384f83d80c085b3b53ff3020a59 [file] [log] [blame]
Marshall Clow57e06df2014-06-06 22:33:40 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Marshall Clow57e06df2014-06-06 22:33:40 +00006//
7//===----------------------------------------------------------------------===//
8
Eric Fiselier74fb1632018-11-21 20:39:24 +00009#ifndef TEST_SUPPORT_COUNTING_PREDICATES_H
10#define TEST_SUPPORT_COUNTING_PREDICATES_H
Marshall Clow57e06df2014-06-06 22:33:40 +000011
Eric Fiselier74fb1632018-11-21 20:39:24 +000012#include <cstddef>
Marshall Clow57e06df2014-06-06 22:33:40 +000013
Marshall Clow90ba05332014-08-04 17:32:25 +000014template <typename Predicate, typename Arg>
Stephan T. Lavavejbc933762017-08-24 21:24:08 +000015struct unary_counting_predicate {
Marshall Clow57e06df2014-06-06 22:33:40 +000016public:
Stephan T. Lavavejbc933762017-08-24 21:24:08 +000017 typedef Arg argument_type;
18 typedef bool result_type;
19
Marshall Clowc8528b52014-10-18 11:03:33 +000020 unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
21 ~unary_counting_predicate() {}
Eric Fiselierd04c6852016-06-01 21:35:39 +000022
Marshall Clowc8528b52014-10-18 11:03:33 +000023 bool operator () (const Arg &a) const { ++count_; return p_(a); }
24 size_t count() const { return count_; }
25 void reset() { count_ = 0; }
Eric Fiselierd04c6852016-06-01 21:35:39 +000026
Marshall Clow57e06df2014-06-06 22:33:40 +000027private:
Marshall Clowc8528b52014-10-18 11:03:33 +000028 Predicate p_;
29 mutable size_t count_;
Eric Fiselier74fb1632018-11-21 20:39:24 +000030};
Marshall Clow57e06df2014-06-06 22:33:40 +000031
32
33template <typename Predicate, typename Arg1, typename Arg2=Arg1>
Stephan T. Lavavejbc933762017-08-24 21:24:08 +000034struct binary_counting_predicate {
Marshall Clow57e06df2014-06-06 22:33:40 +000035public:
Stephan T. Lavavejbc933762017-08-24 21:24:08 +000036 typedef Arg1 first_argument_type;
37 typedef Arg2 second_argument_type;
38 typedef bool result_type;
Marshall Clow57e06df2014-06-06 22:33:40 +000039
Marshall Clowc8528b52014-10-18 11:03:33 +000040 binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
41 ~binary_counting_predicate() {}
Eric Fiselierd04c6852016-06-01 21:35:39 +000042
Marshall Clowc8528b52014-10-18 11:03:33 +000043 bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
44 size_t count() const { return count_; }
45 void reset() { count_ = 0; }
Marshall Clow79d4ffb2014-07-09 21:04:22 +000046
Marshall Clow57e06df2014-06-06 22:33:40 +000047private:
Marshall Clowc8528b52014-10-18 11:03:33 +000048 Predicate p_;
49 mutable size_t count_;
Eric Fiselier74fb1632018-11-21 20:39:24 +000050};
Marshall Clow57e06df2014-06-06 22:33:40 +000051
Eric Fiselier74fb1632018-11-21 20:39:24 +000052#endif // TEST_SUPPORT_COUNTING_PREDICATES_H