libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

counting_predicates.hpp (1514B)


      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 TEST_SUPPORT_COUNTING_PREDICATES_H
     11 #define TEST_SUPPORT_COUNTING_PREDICATES_H
     12 
     13 #include <cstddef>
     14 
     15 template <typename Predicate, typename Arg>
     16 struct unary_counting_predicate {
     17 public:
     18     typedef Arg argument_type;
     19     typedef bool result_type;
     20 
     21     unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
     22     ~unary_counting_predicate() {}
     23 
     24     bool operator () (const Arg &a) const { ++count_; return p_(a); }
     25     size_t count() const { return count_; }
     26     void reset() { count_ = 0; }
     27 
     28 private:
     29     Predicate p_;
     30     mutable size_t count_;
     31 };
     32 
     33 
     34 template <typename Predicate, typename Arg1, typename Arg2=Arg1>
     35 struct binary_counting_predicate {
     36 public:
     37     typedef Arg1 first_argument_type;
     38     typedef Arg2 second_argument_type;
     39     typedef bool result_type;
     40 
     41     binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
     42     ~binary_counting_predicate() {}
     43 
     44     bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
     45     size_t count() const { return count_; }
     46     void reset() { count_ = 0; }
     47 
     48 private:
     49     Predicate p_;
     50     mutable size_t count_;
     51 };
     52 
     53 #endif // TEST_SUPPORT_COUNTING_PREDICATES_H