Chromium Code Reviews| Index: src/list-inl.h |
| =================================================================== |
| --- src/list-inl.h (revision 11245) |
| +++ src/list-inl.h (working copy) |
| @@ -207,20 +207,19 @@ |
| } |
| -template <typename T> |
| -int SortedListBSearch( |
| - const List<T>& list, T elem, int (*cmp)(const T* x, const T* y)) { |
| +template <typename T, typename P> |
| +int SortedListBSearch(const List<T>& list, P cmp) { |
| int low = 0; |
| int high = list.length() - 1; |
| while (low <= high) { |
| int mid = (low + high) / 2; |
| T mid_elem = list[mid]; |
| - if (cmp(&mid_elem, &elem) > 0) { |
| + if (cmp(&mid_elem) > 0) { |
| high = mid - 1; |
| continue; |
| } |
| - if (cmp(&mid_elem, &elem) < 0) { |
| + if (cmp(&mid_elem) < 0) { |
| low = mid + 1; |
| continue; |
| } |
| @@ -231,9 +230,21 @@ |
| } |
| +template<typename T> |
| +class ElementCmp { |
| + public: |
| + explicit ElementCmp(T e) : elem_(e) {} |
| + int operator()(const T* elem) { |
|
mnaganov (inactive)
2012/04/10 09:30:55
Please rename the argument, otherwise it looks lik
yurys
2012/04/10 09:42:34
Done.
|
| + return PointerValueCompare(elem, &elem_); |
| + } |
| + private: |
| + T elem_; |
| +}; |
| + |
| + |
| template <typename T> |
| int SortedListBSearch(const List<T>& list, T elem) { |
| - return SortedListBSearch<T>(list, elem, PointerValueCompare<T>); |
| + return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem)); |
| } |