blob: 4e0f8fb7133a5020659dd022ba405a76afe5b4a4 [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
14template <typename Predicate>
15struct unary_counting_predicate {
16public:
17 unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
18 ~unary_counting_predicate() {}
19
20 typedef typename Predicate::argument_type argument_type;
21 typedef bool result_type;
22
23 bool operator () (const argument_type &a) const { ++count_; return p_(a); }
24 size_t count() const { return count_; }
Marshall Clow79d4ffb2014-07-09 21:04:22 +000025 void reset() { count_ = 0; }
Marshall Clow57e06df2014-06-06 22:33:40 +000026
27private:
28 Predicate p_;
29 mutable size_t count_;
30 };
31
32
33template <typename Predicate, typename Arg1, typename Arg2=Arg1>
34struct binary_counting_predicate : public std::binary_function<Arg1, Arg2, bool> {
35public:
36
37 binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
38 ~binary_counting_predicate() {}
39
40 bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
41 size_t count() const { return count_; }
Marshall Clow79d4ffb2014-07-09 21:04:22 +000042 void reset() { count_ = 0; }
43
Marshall Clow57e06df2014-06-06 22:33:40 +000044private:
45 Predicate p_;
46 mutable size_t count_;
47 };
48
49#endif // __COUNTING_PREDICATES_H