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* other) { |
+ return PointerValueCompare(other, &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)); |
} |