Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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> |
| 15 | struct unary_counting_predicate { |
| 16 | public: |
| 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_; } |
Marshall Clow | 79d4ffb | 2014-07-09 21:04:22 +0000 | [diff] [blame^] | 25 | void reset() { count_ = 0; } |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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 : public std::binary_function<Arg1, Arg2, bool> { |
| 35 | public: |
| 36 | |
| 37 | binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {} |
| 38 | ~binary_counting_predicate() {} |
| 39 | |
| 40 | bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); } |
| 41 | size_t count() const { return count_; } |
Marshall Clow | 79d4ffb | 2014-07-09 21:04:22 +0000 | [diff] [blame^] | 42 | void reset() { count_ = 0; } |
| 43 | |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 44 | private: |
| 45 | Predicate p_; |
| 46 | mutable size_t count_; |
| 47 | }; |
| 48 | |
| 49 | #endif // __COUNTING_PREDICATES_H |