counting_predicates.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef TEST_SUPPORT_COUNTING_PREDICATES_H
  9. #define TEST_SUPPORT_COUNTING_PREDICATES_H
  10. #include <cstddef>
  11. template <typename Predicate, typename Arg>
  12. struct unary_counting_predicate {
  13. public:
  14. typedef Arg argument_type;
  15. typedef bool result_type;
  16. unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
  17. ~unary_counting_predicate() {}
  18. bool operator () (const Arg &a) const { ++count_; return p_(a); }
  19. size_t count() const { return count_; }
  20. void reset() { count_ = 0; }
  21. private:
  22. Predicate p_;
  23. mutable size_t count_;
  24. };
  25. template <typename Predicate, typename Arg1, typename Arg2=Arg1>
  26. struct binary_counting_predicate {
  27. public:
  28. typedef Arg1 first_argument_type;
  29. typedef Arg2 second_argument_type;
  30. typedef bool result_type;
  31. binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {}
  32. ~binary_counting_predicate() {}
  33. bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); }
  34. size_t count() const { return count_; }
  35. void reset() { count_ = 0; }
  36. private:
  37. Predicate p_;
  38. mutable size_t count_;
  39. };
  40. #endif // TEST_SUPPORT_COUNTING_PREDICATES_H