algorithm.h 998 B

12345678910111213141516171819202122232425262728
  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. [[nodiscard]] ForwardIt BinaryFind(ForwardIt first, ForwardIt last, const T& value,
  14. Compare comp = {}) {
  15. // Note: BOTH type T and the type after ForwardIt is dereferenced
  16. // must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
  17. // This is stricter than lower_bound requirement (see above)
  18. first = std::lower_bound(first, last, value, comp);
  19. return first != last && !comp(value, *first) ? first : last;
  20. }
  21. } // namespace Common