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 : public std::unary_function<Arg, bool> { 16 public: 17 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 24 private: 25 Predicate p_; 26 mutable size_t count_; 27 }; 28 29 30 template <typename Predicate, typename Arg1, typename Arg2=Arg1> 31 struct binary_counting_predicate : public std::binary_function<Arg1, Arg2, bool> { 32 public: 33 34 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; } 40 41 private: 42 Predicate p_; 43 mutable size_t count_; 44 }; 45 46 #endif // __COUNTING_PREDICATES_H 47