OLD | NEW |
---|---|
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 | 200 |
201 template<typename T, class P> | 201 template<typename T, class P> |
202 void List<T, P>::Initialize(int capacity) { | 202 void List<T, P>::Initialize(int capacity) { |
203 ASSERT(capacity >= 0); | 203 ASSERT(capacity >= 0); |
204 data_ = (capacity > 0) ? NewData(capacity) : NULL; | 204 data_ = (capacity > 0) ? NewData(capacity) : NULL; |
205 capacity_ = capacity; | 205 capacity_ = capacity; |
206 length_ = 0; | 206 length_ = 0; |
207 } | 207 } |
208 | 208 |
209 | 209 |
210 template <typename T> | 210 template <typename T, typename P> |
211 int SortedListBSearch( | 211 int SortedListBSearch(const List<T>& list, P cmp) { |
212 const List<T>& list, T elem, int (*cmp)(const T* x, const T* y)) { | |
213 int low = 0; | 212 int low = 0; |
214 int high = list.length() - 1; | 213 int high = list.length() - 1; |
215 while (low <= high) { | 214 while (low <= high) { |
216 int mid = (low + high) / 2; | 215 int mid = (low + high) / 2; |
217 T mid_elem = list[mid]; | 216 T mid_elem = list[mid]; |
218 | 217 |
219 if (cmp(&mid_elem, &elem) > 0) { | 218 if (cmp(&mid_elem) > 0) { |
220 high = mid - 1; | 219 high = mid - 1; |
221 continue; | 220 continue; |
222 } | 221 } |
223 if (cmp(&mid_elem, &elem) < 0) { | 222 if (cmp(&mid_elem) < 0) { |
224 low = mid + 1; | 223 low = mid + 1; |
225 continue; | 224 continue; |
226 } | 225 } |
227 // Found the elememt. | 226 // Found the elememt. |
228 return mid; | 227 return mid; |
229 } | 228 } |
230 return -1; | 229 return -1; |
231 } | 230 } |
232 | 231 |
233 | 232 |
233 template<typename T> | |
234 class ElementCmp { | |
235 public: | |
236 explicit ElementCmp(T e) : elem_(e) {} | |
237 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.
| |
238 return PointerValueCompare(elem, &elem_); | |
239 } | |
240 private: | |
241 T elem_; | |
242 }; | |
243 | |
244 | |
234 template <typename T> | 245 template <typename T> |
235 int SortedListBSearch(const List<T>& list, T elem) { | 246 int SortedListBSearch(const List<T>& list, T elem) { |
236 return SortedListBSearch<T>(list, elem, PointerValueCompare<T>); | 247 return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem)); |
237 } | 248 } |
238 | 249 |
239 | 250 |
240 } } // namespace v8::internal | 251 } } // namespace v8::internal |
241 | 252 |
242 #endif // V8_LIST_INL_H_ | 253 #endif // V8_LIST_INL_H_ |
OLD | NEW |