OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 12 matching lines...) Expand all Loading... |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_UTILS_H_ | 28 #ifndef V8_UTILS_H_ |
29 #define V8_UTILS_H_ | 29 #define V8_UTILS_H_ |
30 | 30 |
31 #include <stdlib.h> | 31 #include <stdlib.h> |
32 #include <string.h> | 32 #include <string.h> |
| 33 #include <algorithm> |
33 #include <climits> | 34 #include <climits> |
34 | 35 |
35 #include "allocation.h" | 36 #include "allocation.h" |
36 #include "checks.h" | 37 #include "checks.h" |
37 #include "globals.h" | 38 #include "globals.h" |
38 | 39 |
39 namespace v8 { | 40 namespace v8 { |
40 namespace internal { | 41 namespace internal { |
41 | 42 |
42 // ---------------------------------------------------------------------------- | 43 // ---------------------------------------------------------------------------- |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 T& last() { return start_[length_ - 1]; } | 404 T& last() { return start_[length_ - 1]; } |
404 | 405 |
405 // Returns a clone of this vector with a new backing store. | 406 // Returns a clone of this vector with a new backing store. |
406 Vector<T> Clone() const { | 407 Vector<T> Clone() const { |
407 T* result = NewArray<T>(length_); | 408 T* result = NewArray<T>(length_); |
408 for (int i = 0; i < length_; i++) result[i] = start_[i]; | 409 for (int i = 0; i < length_; i++) result[i] = start_[i]; |
409 return Vector<T>(result, length_); | 410 return Vector<T>(result, length_); |
410 } | 411 } |
411 | 412 |
412 void Sort(int (*cmp)(const T*, const T*)) { | 413 void Sort(int (*cmp)(const T*, const T*)) { |
413 typedef int (*RawComparer)(const void*, const void*); | 414 std::sort(start(), start() + length(), RawComparer(cmp)); |
414 qsort(start(), | |
415 length(), | |
416 sizeof(T), | |
417 reinterpret_cast<RawComparer>(cmp)); | |
418 } | 415 } |
419 | 416 |
420 void Sort() { | 417 void Sort() { |
421 Sort(PointerValueCompare<T>); | 418 std::sort(start(), start() + length()); |
422 } | 419 } |
423 | 420 |
424 void Truncate(int length) { | 421 void Truncate(int length) { |
425 ASSERT(length <= length_); | 422 ASSERT(length <= length_); |
426 length_ = length; | 423 length_ = length; |
427 } | 424 } |
428 | 425 |
429 // Releases the array underlying this vector. Once disposed the | 426 // Releases the array underlying this vector. Once disposed the |
430 // vector is empty. | 427 // vector is empty. |
431 void Dispose() { | 428 void Dispose() { |
(...skipping 15 matching lines...) Expand all Loading... |
447 return Vector<T>(reinterpret_cast<T*>(input.start()), | 444 return Vector<T>(reinterpret_cast<T*>(input.start()), |
448 input.length() * sizeof(S) / sizeof(T)); | 445 input.length() * sizeof(S) / sizeof(T)); |
449 } | 446 } |
450 | 447 |
451 protected: | 448 protected: |
452 void set_start(T* start) { start_ = start; } | 449 void set_start(T* start) { start_ = start; } |
453 | 450 |
454 private: | 451 private: |
455 T* start_; | 452 T* start_; |
456 int length_; | 453 int length_; |
| 454 |
| 455 class RawComparer { |
| 456 public: |
| 457 explicit RawComparer(int (*cmp)(const T*, const T*)) : cmp_(cmp) {} |
| 458 bool operator()(const T& a, const T& b) { |
| 459 return cmp_(&a, &b) < 0; |
| 460 } |
| 461 |
| 462 private: |
| 463 int (*cmp_)(const T*, const T*); |
| 464 }; |
457 }; | 465 }; |
458 | 466 |
459 | 467 |
460 // A pointer that can only be set once and doesn't allow NULL values. | 468 // A pointer that can only be set once and doesn't allow NULL values. |
461 template<typename T> | 469 template<typename T> |
462 class SetOncePointer { | 470 class SetOncePointer { |
463 public: | 471 public: |
464 SetOncePointer() : pointer_(NULL) { } | 472 SetOncePointer() : pointer_(NULL) { } |
465 | 473 |
466 bool is_set() const { return pointer_ != NULL; } | 474 bool is_set() const { return pointer_ != NULL; } |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 | 1091 |
1084 // Every compiled stub starts with this id. | 1092 // Every compiled stub starts with this id. |
1085 static const int kStubEntryId = 5; | 1093 static const int kStubEntryId = 5; |
1086 | 1094 |
1087 int id_; | 1095 int id_; |
1088 }; | 1096 }; |
1089 | 1097 |
1090 } } // namespace v8::internal | 1098 } } // namespace v8::internal |
1091 | 1099 |
1092 #endif // V8_UTILS_H_ | 1100 #endif // V8_UTILS_H_ |
OLD | NEW |