blob: a0b3ca518eccfb0ec26af050ee56f319b7b47c92 [file] [log] [blame]
Marshall Clow57e06df2014-06-06 22:33:40 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eric Fiselier74fb1632018-11-21 20:39:24 +000010#ifndef TEST_SUPPORT_COUNTING_PREDICATES_H
11#define TEST_SUPPORT_COUNTING_PREDICATES_H
Marshall Clow57e06df2014-06-06 22:33:40 +000012
Eric Fiselier74fb1632018-11-21 20:39:24 +000013#include <cstddef>
Marshall Clow57e06df2014-06-06 22:33:40 +000014
Marshall Clow90ba05332014-08-04 17:32:25 +000015template <typename Predicate, typename Arg>
Stephan T. Lavavejbc933762017-08-24 21:24:08 +000016struct unary_counting_predicate {
Marshall Clow57e06df2014-06-06 22:33:40 +000017public:
Stephan T. Lavavejbc933762017-08-24 21:24:08 +000018 typedef Arg argument_type;
19 typedef bool result_type;
20
Marshall Clowc8528b52014-10-18 11:03:33 +000021 unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
22 ~unary_counting_predicate() {}
Eric Fiselierd04c6852016-06-01 21:35:39 +000023
Marshall Clowc8528b52014-10-18 11:03:33 +000024 bool operator () (const Arg &a) const { ++count_; return p_(a); }
25 size_t count() const { return count_; }
26 void reset() { count_ = 0; }
Eric Fiselierd04c6852016-06-01 21:35:39 +000027
Marshall Clow57e06df2014-06-06 22:33:40 +000028private:
Marshall Clowc8528b52014-10-18 11:03:33 +000029 Predicate p_;
30 mutable size_t count_;
Eric Fiselier74fb1632018-11-21 20:39:24 +000031};
Marshall Clow57e06df2014-06-06 22:33:40 +000032
33
34template <typename Predicate, typename Arg1, typename Arg2=Arg1>
Stephan T. Lavavejbc933762017-08-24 21:24:08 +000035struct binary_counting_predicate {
Marshall Clow57e06df2014-06-06 22:33:40 +000036public:
Stephan T. Lavavejbc933762017-08-24 21:24:08 +000037 typedef Arg1 first_argument_type;
38 typedef Arg2 second_argument_type;
39 typedef bool result_type;
Marshall Clow57e06df2014-06-06 22:33:40 +000040
Marshall Clowc8528b52014-10-18 11:03:33 +000041 binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
42 ~binary_counting_predicate() {}
Eric Fiselierd04c6852016-06-01 21:35:39 +000043
Marshall Clowc8528b52014-10-18 11:03:33 +000044 bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
45 size_t count() const { return count_; }
46 void reset() { count_ = 0; }
Marshall Clow79d4ffb2014-07-09 21:04:22 +000047
Marshall Clow57e06df2014-06-06 22:33:40 +000048private:
Marshall Clowc8528b52014-10-18 11:03:33 +000049 Predicate p_;
50 mutable size_t count_;
Eric Fiselier74fb1632018-11-21 20:39:24 +000051};
Marshall Clow57e06df2014-06-06 22:33:40 +000052
Eric Fiselier74fb1632018-11-21 20:39:24 +000053#endif // TEST_SUPPORT_COUNTING_PREDICATES_H