blob: eef7daf310d0f27e5f7fc028e9327b405e9282be [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_; }
25
26private:
27 Predicate p_;
28 mutable size_t count_;
29 };
30
31
32template <typename Predicate, typename Arg1, typename Arg2=Arg1>
33struct binary_counting_predicate : public std::binary_function<Arg1, Arg2, bool> {
34public:
35
36 binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
37 ~binary_counting_predicate() {}
38
39 bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
40 size_t count() const { return count_; }
41
42private:
43 Predicate p_;
44 mutable size_t count_;
45 };
46
47#endif // __COUNTING_PREDICATES_H