algorithm.h 949 B

123456789101112131415161718192021222324252627
  1. // Copyright 2019 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <functional>
  7. // Algorithms that operate on iterators, much like the <algorithm> header.
  8. //
  9. // Note: If the algorithm is not general-purpose and/or doesn't operate on iterators,
  10. // it should probably not be placed within this header.
  11. namespace Common {
  12. template <class ForwardIt, class T, class Compare = std::less<>>
  13. ForwardIt BinaryFind(ForwardIt first, ForwardIt last, const T& value, Compare comp = {}) {
  14. // Note: BOTH type T and the type after ForwardIt is dereferenced
  15. // must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
  16. // This is stricter than lower_bound requirement (see above)
  17. first = std::lower_bound(first, last, value, comp);
  18. return first != last && !comp(value, *first) ? first : last;
  19. }
  20. } // namespace Common