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 | |
Marshall Clow | 90ba0533 | 2014-08-04 17:32:25 +0000 | [diff] [blame] | 14 | template <typename Predicate, typename Arg> |
Stephan T. Lavavej | bc93376 | 2017-08-24 21:24:08 +0000 | [diff] [blame] | 15 | struct unary_counting_predicate { |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 16 | public: |
Stephan T. Lavavej | bc93376 | 2017-08-24 21:24:08 +0000 | [diff] [blame] | 17 | typedef Arg argument_type; |
| 18 | typedef bool result_type; |
| 19 | |
Marshall Clow | c8528b5 | 2014-10-18 11:03:33 +0000 | [diff] [blame] | 20 | unary_counting_predicate(Predicate p) : p_(p), count_(0) {} |
| 21 | ~unary_counting_predicate() {} |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 22 | |
Marshall Clow | c8528b5 | 2014-10-18 11:03:33 +0000 | [diff] [blame] | 23 | bool operator () (const Arg &a) const { ++count_; return p_(a); } |
| 24 | size_t count() const { return count_; } |
| 25 | void reset() { count_ = 0; } |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 26 | |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 27 | private: |
Marshall Clow | c8528b5 | 2014-10-18 11:03:33 +0000 | [diff] [blame] | 28 | Predicate p_; |
| 29 | mutable size_t count_; |
| 30 | }; |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | template <typename Predicate, typename Arg1, typename Arg2=Arg1> |
Stephan T. Lavavej | bc93376 | 2017-08-24 21:24:08 +0000 | [diff] [blame] | 34 | struct binary_counting_predicate { |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 35 | public: |
Stephan T. Lavavej | bc93376 | 2017-08-24 21:24:08 +0000 | [diff] [blame] | 36 | typedef Arg1 first_argument_type; |
| 37 | typedef Arg2 second_argument_type; |
| 38 | typedef bool result_type; |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 39 | |
Marshall Clow | c8528b5 | 2014-10-18 11:03:33 +0000 | [diff] [blame] | 40 | binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {} |
| 41 | ~binary_counting_predicate() {} |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 42 | |
Marshall Clow | c8528b5 | 2014-10-18 11:03:33 +0000 | [diff] [blame] | 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; } |
Marshall Clow | 79d4ffb | 2014-07-09 21:04:22 +0000 | [diff] [blame] | 46 | |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 47 | private: |
Marshall Clow | c8528b5 | 2014-10-18 11:03:33 +0000 | [diff] [blame] | 48 | Predicate p_; |
| 49 | mutable size_t count_; |
| 50 | }; |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 51 | |
| 52 | #endif // __COUNTING_PREDICATES_H |