blob: aab6a9785772c1619dfce488ddf711fb25086596 [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>
15struct unary_counting_predicate : public std::unary_function<Arg, bool> {
Marshall Clow57e06df2014-06-06 22:33:40 +000016public:
Marshall Clowc8528b52014-10-18 11:03:33 +000017 unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
18 ~unary_counting_predicate() {}
19
20 bool operator () (const Arg &a) const { ++count_; return p_(a); }
21 size_t count() const { return count_; }
22 void reset() { count_ = 0; }
23
Marshall Clow57e06df2014-06-06 22:33:40 +000024private:
Marshall Clowc8528b52014-10-18 11:03:33 +000025 Predicate p_;
26 mutable size_t count_;
27 };
Marshall Clow57e06df2014-06-06 22:33:40 +000028
29
30template <typename Predicate, typename Arg1, typename Arg2=Arg1>
31struct binary_counting_predicate : public std::binary_function<Arg1, Arg2, bool> {
32public:
33
Marshall Clowc8528b52014-10-18 11:03:33 +000034 binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
35 ~binary_counting_predicate() {}
36
37 bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
38 size_t count() const { return count_; }
39 void reset() { count_ = 0; }
Marshall Clow79d4ffb2014-07-09 21:04:22 +000040
Marshall Clow57e06df2014-06-06 22:33:40 +000041private:
Marshall Clowc8528b52014-10-18 11:03:33 +000042 Predicate p_;
43 mutable size_t count_;
44 };
Marshall Clow57e06df2014-06-06 22:33:40 +000045
46#endif // __COUNTING_PREDICATES_H