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 20 unary_counting_predicate(Predicate p) : p_(p), count_(0) {} 21 ~unary_counting_predicate() {} 22 23 bool operator () (const Arg &a) const { ++count_; return p_(a); } 24 size_t count() const { return count_; } 25 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 40 binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {} 41 ~binary_counting_predicate() {} 42 43 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; } 46 47 private: 48 Predicate p_; 49 mutable size_t count_; 50 }; 51 52 #endif // __COUNTING_PREDICATES_H 53