| 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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 194 |
| 195 template<typename T, class P> | 195 template<typename T, class P> |
| 196 void List<T, P>::Initialize(int capacity) { | 196 void List<T, P>::Initialize(int capacity) { |
| 197 ASSERT(capacity >= 0); | 197 ASSERT(capacity >= 0); |
| 198 data_ = (capacity > 0) ? NewData(capacity) : NULL; | 198 data_ = (capacity > 0) ? NewData(capacity) : NULL; |
| 199 capacity_ = capacity; | 199 capacity_ = capacity; |
| 200 length_ = 0; | 200 length_ = 0; |
| 201 } | 201 } |
| 202 | 202 |
| 203 | 203 |
| 204 template<typename T, class P> |
| 205 void SearchableList<T, P>::Add(const T& element) { |
| 206 is_sorted_ = false; |
| 207 List<T, P>::Add(element); |
| 208 } |
| 209 |
| 210 |
| 211 template<typename T, class P> |
| 212 void SearchableList<T, P>::AddAll(const List<T, P>& other) { |
| 213 is_sorted_ = false; |
| 214 List<T, P>::AddAll(other); |
| 215 } |
| 216 |
| 217 |
| 218 template<typename T, class P> |
| 219 Vector<T> SearchableList<T, P>::AddBlock(T value, int count) { |
| 220 is_sorted_ = false; |
| 221 return List<T, P>::AddBlock(value, count); |
| 222 } |
| 223 |
| 224 |
| 225 template<typename T, class P> |
| 226 bool SearchableList<T, P>::Contains(const T& elm) { |
| 227 if (is_sorted_) { |
| 228 return ContainsSorted(elm); |
| 229 } |
| 230 return List<T, P>::Contains(elm); |
| 231 } |
| 232 |
| 233 |
| 234 template<typename T, class P> |
| 235 bool SearchableList<T, P>::Contains(const T& elm, bool ensure_sorted) { |
| 236 if (ensure_sorted) { |
| 237 Sort(); // Will only sort if not already sorted. |
| 238 } |
| 239 return List<T, P>::Contains(elm); |
| 240 } |
| 241 |
| 242 |
| 243 template<typename T, class P> |
| 244 void SearchableList<T, P>::Sort() { |
| 245 if (!is_sorted_) { |
| 246 List<T, P>::Sort(comparator_); |
| 247 is_sorted_ = true; |
| 248 } |
| 249 } |
| 250 |
| 251 |
| 252 template<typename T, class P> |
| 253 bool SearchableList<T, P>::ContainsSorted(const T& elm) { |
| 254 typedef int (*RawComparer)(const void*, const void*); |
| 255 return (NULL != bsearch(&elm, |
| 256 List<T, P>::data(), |
| 257 List<T, P>::length(), |
| 258 sizeof(T), |
| 259 reinterpret_cast<RawComparer>(comparator_))); |
| 260 } |
| 261 |
| 262 |
| 204 } } // namespace v8::internal | 263 } } // namespace v8::internal |
| 205 | 264 |
| 206 #endif // V8_LIST_INL_H_ | 265 #endif // V8_LIST_INL_H_ |
| OLD | NEW |