| #pragma once |
| |
| #ifndef TCG_ALGORITHM_H |
| #define TCG_ALGORITHM_H |
| |
| |
| #include "traits.h" |
| |
| |
| #include <functional> |
| |
| |
| |
| |
| |
| |
| |
| |
| namespace tcg |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename RanIt, typename T> |
| RanIt binary_find(RanIt begin, |
| RanIt end, |
| const T &value) |
| { |
| RanIt it = std::lower_bound(begin, end, value); |
| return (it != end && !(value < *it)) ? it : end; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename RanIt, typename T, typename Compare> |
| RanIt binary_find(RanIt begin, |
| RanIt end, |
| const T &value, |
| Compare comp) |
| { |
| RanIt it = std::lower_bound(begin, end, value, comp); |
| return (it != end && !comp(value, *it)) ? it : end; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename ForIt, typename Func, typename Comp> |
| ForIt min_transform(ForIt begin, |
| ForIt end, |
| Func func, |
| Comp comp) |
| { |
| typedef typename tcg::function_traits<Func>::ret_type ret_type; |
| typedef typename tcg::remove_cref<ret_type>::type value_type; |
| |
| if (begin == end) |
| return end; |
| |
| ForIt minPos = begin; |
| value_type minimum = func(*begin); |
| |
| for (; begin != end; ++begin) { |
| const value_type &candidate = func(*begin); |
| |
| if (comp(candidate, minimum)) |
| minPos = begin, minimum = candidate; |
| } |
| |
| return minPos; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename ForIt, typename Func> |
| ForIt min_transform(ForIt begin, |
| ForIt end, |
| Func func) |
| { |
| typedef typename tcg::function_traits<Func>::ret_type ret_type; |
| typedef typename tcg::remove_cref<ret_type>::type value_type; |
| |
| return min_transform(begin, end, func, std::less<value_type>()); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename ForIt, typename Func, typename Comp> |
| ForIt max_transform(ForIt begin, |
| ForIt end, |
| Func func, |
| Comp comp) |
| { |
| typedef typename tcg::function_traits<Func>::ret_type ret_type; |
| typedef typename tcg::remove_cref<ret_type>::type value_type; |
| |
| if (begin == end) |
| return end; |
| |
| ForIt maxPos = begin; |
| value_type maximum = func(*begin); |
| |
| for (; begin != end; ++begin) { |
| const value_type &candidate = func(*begin); |
| |
| if (comp(maximum, candidate)) |
| maxPos = begin, maximum = candidate; |
| } |
| |
| return maxPos; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename ForIt, typename Func> |
| ForIt max_transform(ForIt begin, |
| ForIt end, |
| Func func) |
| { |
| typedef typename tcg::function_traits<Func>::ret_type ret_type; |
| typedef typename tcg::remove_cref<ret_type>::type value_type; |
| |
| return max_transform(begin, end, func, std::less<value_type>()); |
| } |
| |
| } |
| |
| #endif |