1 //===----------------------------------------------------------------------===//
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 
14 template <typename Predicate, typename Arg>
15 struct unary_counting_predicate {
16 public:
17     typedef Arg argument_type;
18     typedef bool result_type;
19 
unary_counting_predicateunary_counting_predicate20     unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
~unary_counting_predicateunary_counting_predicate21     ~unary_counting_predicate() {}
22 
operator ()unary_counting_predicate23     bool operator () (const Arg &a) const { ++count_; return p_(a); }
countunary_counting_predicate24     size_t count() const { return count_; }
resetunary_counting_predicate25     void reset() { count_ = 0; }
26 
27 private:
28     Predicate p_;
29     mutable size_t count_;
30     };
31 
32 
33 template <typename Predicate, typename Arg1, typename Arg2=Arg1>
34 struct binary_counting_predicate {
35 public:
36     typedef Arg1 first_argument_type;
37     typedef Arg2 second_argument_type;
38     typedef bool result_type;
39 
binary_counting_predicatebinary_counting_predicate40     binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
~binary_counting_predicatebinary_counting_predicate41     ~binary_counting_predicate() {}
42 
operator ()binary_counting_predicate43     bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
countbinary_counting_predicate44     size_t count() const { return count_; }
resetbinary_counting_predicate45     void reset() { count_ = 0; }
46 
47 private:
48     Predicate p_;
49     mutable size_t count_;
50     };
51 
52 #endif // __COUNTING_PREDICATES_H
53