Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 } | 76 } |
| 77 | 77 |
| 78 | 78 |
| 79 // Return the smallest multiple of m which is >= x. | 79 // Return the smallest multiple of m which is >= x. |
| 80 template <typename T> | 80 template <typename T> |
| 81 static inline T RoundUp(T x, int m) { | 81 static inline T RoundUp(T x, int m) { |
| 82 return RoundDown(x + m - 1, m); | 82 return RoundDown(x + m - 1, m); |
| 83 } | 83 } |
| 84 | 84 |
| 85 | 85 |
| 86 template <typename T> | |
| 87 static int Spaceship(const T& a, const T& b) { | |
|
Erik Corry
2008/11/13 11:45:43
The reasoning behind this name is a little opaque
Christian Plesner Hansen
2008/11/13 11:58:37
http://en.wikipedia.org/wiki/Spaceship_operator
W
| |
| 88 if (a == b) | |
| 89 return 0; | |
| 90 else if (a < b) | |
| 91 return -1; | |
| 92 else | |
| 93 return 1; | |
| 94 } | |
| 95 | |
| 96 | |
| 97 template <typename T> | |
| 98 static int PointerSpaceship(const T* a, const T* b) { | |
| 99 return Spaceship<T>(*a, *b); | |
| 100 } | |
| 101 | |
| 102 | |
| 86 // Returns the smallest power of two which is >= x. If you pass in a | 103 // Returns the smallest power of two which is >= x. If you pass in a |
| 87 // number that is already a power of two, it is returned as is. | 104 // number that is already a power of two, it is returned as is. |
| 88 uint32_t RoundUpToPowerOf2(uint32_t x); | 105 uint32_t RoundUpToPowerOf2(uint32_t x); |
| 89 | 106 |
| 90 | 107 |
| 91 template <typename T> | 108 template <typename T> |
| 92 static inline bool IsAligned(T value, T alignment) { | 109 static inline bool IsAligned(T value, T alignment) { |
| 93 ASSERT(IsPowerOf2(alignment)); | 110 ASSERT(IsPowerOf2(alignment)); |
| 94 return (value & (alignment - 1)) == 0; | 111 return (value & (alignment - 1)) == 0; |
| 95 } | 112 } |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 | 328 |
| 312 T& last() { return start_[length_ - 1]; } | 329 T& last() { return start_[length_ - 1]; } |
| 313 | 330 |
| 314 // Returns a clone of this vector with a new backing store. | 331 // Returns a clone of this vector with a new backing store. |
| 315 Vector<T> Clone() const { | 332 Vector<T> Clone() const { |
| 316 T* result = NewArray<T>(length_); | 333 T* result = NewArray<T>(length_); |
| 317 for (int i = 0; i < length_; i++) result[i] = start_[i]; | 334 for (int i = 0; i < length_; i++) result[i] = start_[i]; |
| 318 return Vector<T>(result, length_); | 335 return Vector<T>(result, length_); |
| 319 } | 336 } |
| 320 | 337 |
| 338 void Sort(int (*cmp)(const T*, const T*)) { | |
| 339 typedef int (*RawComparer)(const void*, const void*); | |
| 340 qsort(start(), | |
| 341 length(), | |
| 342 sizeof(T), | |
| 343 reinterpret_cast<RawComparer>(cmp)); | |
| 344 } | |
| 345 | |
| 346 void Sort() { | |
| 347 Sort(PointerSpaceship<T>); | |
| 348 } | |
| 349 | |
| 321 // Releases the array underlying this vector. Once disposed the | 350 // Releases the array underlying this vector. Once disposed the |
| 322 // vector is empty. | 351 // vector is empty. |
| 323 void Dispose() { | 352 void Dispose() { |
| 324 if (is_empty()) return; | 353 if (is_empty()) return; |
| 325 DeleteArray(start_); | 354 DeleteArray(start_); |
| 326 start_ = NULL; | 355 start_ = NULL; |
| 327 length_ = 0; | 356 length_ = 0; |
| 328 } | 357 } |
| 329 | 358 |
| 330 inline Vector<T> operator+(int offset) { | 359 inline Vector<T> operator+(int offset) { |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 529 #endif | 558 #endif |
| 530 } | 559 } |
| 531 | 560 |
| 532 | 561 |
| 533 | 562 |
| 534 | 563 |
| 535 | 564 |
| 536 } } // namespace v8::internal | 565 } } // namespace v8::internal |
| 537 | 566 |
| 538 #endif // V8_UTILS_H_ | 567 #endif // V8_UTILS_H_ |
| OLD | NEW |