OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_VECTOR_H_ | 5 #ifndef V8_VECTOR_H_ |
6 #define V8_VECTOR_H_ | 6 #define V8_VECTOR_H_ |
7 | 7 |
8 #include <string.h> | 8 #include <string.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "src/allocation.h" | 11 #include "src/allocation.h" |
12 #include "src/checks.h" | 12 #include "src/checks.h" |
13 #include "src/globals.h" | 13 #include "src/globals.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 | 18 |
19 template <typename T> | 19 template <typename T> |
20 class Vector { | 20 class Vector { |
21 public: | 21 public: |
22 Vector() : start_(NULL), length_(0) {} | 22 Vector() : start_(NULL), length_(0) {} |
23 Vector(T* data, int length) : start_(data), length_(length) { | 23 Vector(T* data, int length) : start_(data), length_(length) { |
24 ASSERT(length == 0 || (length > 0 && data != NULL)); | 24 DCHECK(length == 0 || (length > 0 && data != NULL)); |
25 } | 25 } |
26 | 26 |
27 static Vector<T> New(int length) { | 27 static Vector<T> New(int length) { |
28 return Vector<T>(NewArray<T>(length), length); | 28 return Vector<T>(NewArray<T>(length), length); |
29 } | 29 } |
30 | 30 |
31 // Returns a vector using the same backing storage as this one, | 31 // Returns a vector using the same backing storage as this one, |
32 // spanning from and including 'from', to but not including 'to'. | 32 // spanning from and including 'from', to but not including 'to'. |
33 Vector<T> SubVector(int from, int to) { | 33 Vector<T> SubVector(int from, int to) { |
34 SLOW_ASSERT(to <= length_); | 34 SLOW_DCHECK(to <= length_); |
35 SLOW_ASSERT(from < to); | 35 SLOW_DCHECK(from < to); |
36 ASSERT(0 <= from); | 36 DCHECK(0 <= from); |
37 return Vector<T>(start() + from, to - from); | 37 return Vector<T>(start() + from, to - from); |
38 } | 38 } |
39 | 39 |
40 // Returns the length of the vector. | 40 // Returns the length of the vector. |
41 int length() const { return length_; } | 41 int length() const { return length_; } |
42 | 42 |
43 // Returns whether or not the vector is empty. | 43 // Returns whether or not the vector is empty. |
44 bool is_empty() const { return length_ == 0; } | 44 bool is_empty() const { return length_ == 0; } |
45 | 45 |
46 // Returns the pointer to the start of the data in the vector. | 46 // Returns the pointer to the start of the data in the vector. |
47 T* start() const { return start_; } | 47 T* start() const { return start_; } |
48 | 48 |
49 // Access individual vector elements - checks bounds in debug mode. | 49 // Access individual vector elements - checks bounds in debug mode. |
50 T& operator[](int index) const { | 50 T& operator[](int index) const { |
51 ASSERT(0 <= index && index < length_); | 51 DCHECK(0 <= index && index < length_); |
52 return start_[index]; | 52 return start_[index]; |
53 } | 53 } |
54 | 54 |
55 const T& at(int index) const { return operator[](index); } | 55 const T& at(int index) const { return operator[](index); } |
56 | 56 |
57 T& first() { return start_[0]; } | 57 T& first() { return start_[0]; } |
58 | 58 |
59 T& last() { return start_[length_ - 1]; } | 59 T& last() { return start_[length_ - 1]; } |
60 | 60 |
61 // Returns a clone of this vector with a new backing store. | 61 // Returns a clone of this vector with a new backing store. |
62 Vector<T> Clone() const { | 62 Vector<T> Clone() const { |
63 T* result = NewArray<T>(length_); | 63 T* result = NewArray<T>(length_); |
64 for (int i = 0; i < length_; i++) result[i] = start_[i]; | 64 for (int i = 0; i < length_; i++) result[i] = start_[i]; |
65 return Vector<T>(result, length_); | 65 return Vector<T>(result, length_); |
66 } | 66 } |
67 | 67 |
68 void Sort(int (*cmp)(const T*, const T*)) { | 68 void Sort(int (*cmp)(const T*, const T*)) { |
69 std::sort(start(), start() + length(), RawComparer(cmp)); | 69 std::sort(start(), start() + length(), RawComparer(cmp)); |
70 } | 70 } |
71 | 71 |
72 void Sort() { | 72 void Sort() { |
73 std::sort(start(), start() + length()); | 73 std::sort(start(), start() + length()); |
74 } | 74 } |
75 | 75 |
76 void Truncate(int length) { | 76 void Truncate(int length) { |
77 ASSERT(length <= length_); | 77 DCHECK(length <= length_); |
78 length_ = length; | 78 length_ = length; |
79 } | 79 } |
80 | 80 |
81 // Releases the array underlying this vector. Once disposed the | 81 // Releases the array underlying this vector. Once disposed the |
82 // vector is empty. | 82 // vector is empty. |
83 void Dispose() { | 83 void Dispose() { |
84 DeleteArray(start_); | 84 DeleteArray(start_); |
85 start_ = NULL; | 85 start_ = NULL; |
86 length_ = 0; | 86 length_ = 0; |
87 } | 87 } |
88 | 88 |
89 inline Vector<T> operator+(int offset) { | 89 inline Vector<T> operator+(int offset) { |
90 ASSERT(offset < length_); | 90 DCHECK(offset < length_); |
91 return Vector<T>(start_ + offset, length_ - offset); | 91 return Vector<T>(start_ + offset, length_ - offset); |
92 } | 92 } |
93 | 93 |
94 // Factory method for creating empty vectors. | 94 // Factory method for creating empty vectors. |
95 static Vector<T> empty() { return Vector<T>(NULL, 0); } | 95 static Vector<T> empty() { return Vector<T>(NULL, 0); } |
96 | 96 |
97 template<typename S> | 97 template<typename S> |
98 static Vector<T> cast(Vector<S> input) { | 98 static Vector<T> cast(Vector<S> input) { |
99 return Vector<T>(reinterpret_cast<T*>(input.start()), | 99 return Vector<T>(reinterpret_cast<T*>(input.start()), |
100 input.length() * sizeof(S) / sizeof(T)); | 100 input.length() * sizeof(S) / sizeof(T)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 DeleteArray(this->start()); | 139 DeleteArray(this->start()); |
140 } | 140 } |
141 | 141 |
142 private: | 142 private: |
143 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); | 143 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); |
144 }; | 144 }; |
145 | 145 |
146 | 146 |
147 inline int StrLength(const char* string) { | 147 inline int StrLength(const char* string) { |
148 size_t length = strlen(string); | 148 size_t length = strlen(string); |
149 ASSERT(length == static_cast<size_t>(static_cast<int>(length))); | 149 DCHECK(length == static_cast<size_t>(static_cast<int>(length))); |
150 return static_cast<int>(length); | 150 return static_cast<int>(length); |
151 } | 151 } |
152 | 152 |
153 | 153 |
154 #define STATIC_ASCII_VECTOR(x) \ | 154 #define STATIC_ASCII_VECTOR(x) \ |
155 v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \ | 155 v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \ |
156 ARRAY_SIZE(x)-1) | 156 ARRAY_SIZE(x)-1) |
157 | 157 |
158 inline Vector<const char> CStrVector(const char* data) { | 158 inline Vector<const char> CStrVector(const char* data) { |
159 return Vector<const char>(data, StrLength(data)); | 159 return Vector<const char>(data, StrLength(data)); |
(...skipping 13 matching lines...) Expand all Loading... |
173 | 173 |
174 inline Vector<char> MutableCStrVector(char* data, int max) { | 174 inline Vector<char> MutableCStrVector(char* data, int max) { |
175 int length = StrLength(data); | 175 int length = StrLength(data); |
176 return Vector<char>(data, (length < max) ? length : max); | 176 return Vector<char>(data, (length < max) ? length : max); |
177 } | 177 } |
178 | 178 |
179 | 179 |
180 } } // namespace v8::internal | 180 } } // namespace v8::internal |
181 | 181 |
182 #endif // V8_VECTOR_H_ | 182 #endif // V8_VECTOR_H_ |
OLD | NEW |