blob: 050b51b75bb1f0693b8bf52aa2e8196f60568495 [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
10#ifndef __COUNTING_PREDICATES_H
11#define __COUNTING_PREDICATES_H
12
13
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_;
30 };
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_;
50 };
Marshall Clow57e06df2014-06-06 22:33:40 +000051
52#endif // __COUNTING_PREDICATES_H