• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
unary_counting_predicateunary_counting_predicate17     unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
~unary_counting_predicateunary_counting_predicate18     ~unary_counting_predicate() {}
19 
operator ()unary_counting_predicate20     bool operator () (const Arg &a) const { ++count_; return p_(a); }
countunary_counting_predicate21     size_t count() const { return count_; }
resetunary_counting_predicate22     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 
binary_counting_predicatebinary_counting_predicate34     binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
~binary_counting_predicatebinary_counting_predicate35     ~binary_counting_predicate() {}
36 
operator ()binary_counting_predicate37     bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
countbinary_counting_predicate38     size_t count() const { return count_; }
resetbinary_counting_predicate39     void reset() { count_ = 0; }
40 
41 private:
42     Predicate p_;
43     mutable size_t count_;
44     };
45 
46 #endif // __COUNTING_PREDICATES_H
47