1*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===// 2*0a6a1f1dSLionel Sambuc // 3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure 4*0a6a1f1dSLionel Sambuc // 5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open 6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details. 7*0a6a1f1dSLionel Sambuc // 8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===// 9*0a6a1f1dSLionel Sambuc 10*0a6a1f1dSLionel Sambuc #ifndef __COUNTING_PREDICATES_H 11*0a6a1f1dSLionel Sambuc #define __COUNTING_PREDICATES_H 12*0a6a1f1dSLionel Sambuc 13*0a6a1f1dSLionel Sambuc 14*0a6a1f1dSLionel Sambuc template <typename Predicate, typename Arg> 15*0a6a1f1dSLionel Sambuc struct unary_counting_predicate : public std::unary_function<Arg, bool> { 16*0a6a1f1dSLionel Sambuc public: unary_counting_predicateunary_counting_predicate17*0a6a1f1dSLionel Sambuc unary_counting_predicate(Predicate p) : p_(p), count_(0) {} ~unary_counting_predicateunary_counting_predicate18*0a6a1f1dSLionel Sambuc ~unary_counting_predicate() {} 19*0a6a1f1dSLionel Sambuc operator ()unary_counting_predicate20*0a6a1f1dSLionel Sambuc bool operator () (const Arg &a) const { ++count_; return p_(a); } countunary_counting_predicate21*0a6a1f1dSLionel Sambuc size_t count() const { return count_; } resetunary_counting_predicate22*0a6a1f1dSLionel Sambuc void reset() { count_ = 0; } 23*0a6a1f1dSLionel Sambuc 24*0a6a1f1dSLionel Sambuc private: 25*0a6a1f1dSLionel Sambuc Predicate p_; 26*0a6a1f1dSLionel Sambuc mutable size_t count_; 27*0a6a1f1dSLionel Sambuc }; 28*0a6a1f1dSLionel Sambuc 29*0a6a1f1dSLionel Sambuc 30*0a6a1f1dSLionel Sambuc template <typename Predicate, typename Arg1, typename Arg2=Arg1> 31*0a6a1f1dSLionel Sambuc struct binary_counting_predicate : public std::binary_function<Arg1, Arg2, bool> { 32*0a6a1f1dSLionel Sambuc public: 33*0a6a1f1dSLionel Sambuc binary_counting_predicatebinary_counting_predicate34*0a6a1f1dSLionel Sambuc binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {} ~binary_counting_predicatebinary_counting_predicate35*0a6a1f1dSLionel Sambuc ~binary_counting_predicate() {} 36*0a6a1f1dSLionel Sambuc operator ()binary_counting_predicate37*0a6a1f1dSLionel Sambuc bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); } countbinary_counting_predicate38*0a6a1f1dSLionel Sambuc size_t count() const { return count_; } resetbinary_counting_predicate39*0a6a1f1dSLionel Sambuc void reset() { count_ = 0; } 40*0a6a1f1dSLionel Sambuc 41*0a6a1f1dSLionel Sambuc private: 42*0a6a1f1dSLionel Sambuc Predicate p_; 43*0a6a1f1dSLionel Sambuc mutable size_t count_; 44*0a6a1f1dSLionel Sambuc }; 45*0a6a1f1dSLionel Sambuc 46*0a6a1f1dSLionel Sambuc #endif // __COUNTING_PREDICATES_H 47