Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_VECTOR_H_ |
| 6 | #define V8_VECTOR_H_ |
| 7 | |
| 8 | #include <string.h> |
| 9 | #include <algorithm> |
| 10 | |
| 11 | #include "src/allocation.h" |
| 12 | #include "src/checks.h" |
| 13 | #include "src/globals.h" |
| 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | |
| 18 | |
| 19 | template <typename T> |
| 20 | class Vector { |
| 21 | public: |
| 22 | Vector() : start_(NULL), length_(0) {} |
| 23 | Vector(T* data, int length) : start_(data), length_(length) { |
| 24 | DCHECK(length == 0 || (length > 0 && data != NULL)); |
| 25 | } |
| 26 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 27 | template <int N> |
| 28 | explicit Vector(T (&arr)[N]) : start_(arr), length_(N) {} |
| 29 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | static Vector<T> New(int length) { |
| 31 | return Vector<T>(NewArray<T>(length), length); |
| 32 | } |
| 33 | |
| 34 | // Returns a vector using the same backing storage as this one, |
| 35 | // spanning from and including 'from', to but not including 'to'. |
| 36 | Vector<T> SubVector(int from, int to) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | DCHECK(0 <= from); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 38 | SLOW_DCHECK(from < to); |
| 39 | SLOW_DCHECK(static_cast<unsigned>(to) <= static_cast<unsigned>(length_)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | return Vector<T>(start() + from, to - from); |
| 41 | } |
| 42 | |
| 43 | // Returns the length of the vector. |
| 44 | int length() const { return length_; } |
| 45 | |
| 46 | // Returns whether or not the vector is empty. |
| 47 | bool is_empty() const { return length_ == 0; } |
| 48 | |
| 49 | // Returns the pointer to the start of the data in the vector. |
| 50 | T* start() const { return start_; } |
| 51 | |
| 52 | // Access individual vector elements - checks bounds in debug mode. |
| 53 | T& operator[](int index) const { |
| 54 | DCHECK(0 <= index && index < length_); |
| 55 | return start_[index]; |
| 56 | } |
| 57 | |
| 58 | const T& at(int index) const { return operator[](index); } |
| 59 | |
| 60 | T& first() { return start_[0]; } |
| 61 | |
| 62 | T& last() { return start_[length_ - 1]; } |
| 63 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 64 | typedef T* iterator; |
| 65 | inline iterator begin() const { return &start_[0]; } |
| 66 | inline iterator end() const { return &start_[length_]; } |
| 67 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | // Returns a clone of this vector with a new backing store. |
| 69 | Vector<T> Clone() const { |
| 70 | T* result = NewArray<T>(length_); |
| 71 | for (int i = 0; i < length_; i++) result[i] = start_[i]; |
| 72 | return Vector<T>(result, length_); |
| 73 | } |
| 74 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 75 | template <typename CompareFunction> |
| 76 | void Sort(CompareFunction cmp, size_t s, size_t l) { |
| 77 | std::sort(start() + s, start() + s + l, RawComparer<CompareFunction>(cmp)); |
| 78 | } |
| 79 | |
| 80 | template <typename CompareFunction> |
| 81 | void Sort(CompareFunction cmp) { |
| 82 | std::sort(start(), start() + length(), RawComparer<CompareFunction>(cmp)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Sort() { |
| 86 | std::sort(start(), start() + length()); |
| 87 | } |
| 88 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | template <typename CompareFunction> |
| 90 | void StableSort(CompareFunction cmp, size_t s, size_t l) { |
| 91 | std::stable_sort(start() + s, start() + s + l, |
| 92 | RawComparer<CompareFunction>(cmp)); |
| 93 | } |
| 94 | |
| 95 | template <typename CompareFunction> |
| 96 | void StableSort(CompareFunction cmp) { |
| 97 | std::stable_sort(start(), start() + length(), |
| 98 | RawComparer<CompareFunction>(cmp)); |
| 99 | } |
| 100 | |
| 101 | void StableSort() { std::stable_sort(start(), start() + length()); } |
| 102 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | void Truncate(int length) { |
| 104 | DCHECK(length <= length_); |
| 105 | length_ = length; |
| 106 | } |
| 107 | |
| 108 | // Releases the array underlying this vector. Once disposed the |
| 109 | // vector is empty. |
| 110 | void Dispose() { |
| 111 | DeleteArray(start_); |
| 112 | start_ = NULL; |
| 113 | length_ = 0; |
| 114 | } |
| 115 | |
| 116 | inline Vector<T> operator+(int offset) { |
| 117 | DCHECK(offset < length_); |
| 118 | return Vector<T>(start_ + offset, length_ - offset); |
| 119 | } |
| 120 | |
| 121 | // Factory method for creating empty vectors. |
| 122 | static Vector<T> empty() { return Vector<T>(NULL, 0); } |
| 123 | |
| 124 | template<typename S> |
| 125 | static Vector<T> cast(Vector<S> input) { |
| 126 | return Vector<T>(reinterpret_cast<T*>(input.start()), |
| 127 | input.length() * sizeof(S) / sizeof(T)); |
| 128 | } |
| 129 | |
| 130 | bool operator==(const Vector<T>& other) const { |
| 131 | if (length_ != other.length_) return false; |
| 132 | if (start_ == other.start_) return true; |
| 133 | for (int i = 0; i < length_; ++i) { |
| 134 | if (start_[i] != other.start_[i]) { |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | protected: |
| 142 | void set_start(T* start) { start_ = start; } |
| 143 | |
| 144 | private: |
| 145 | T* start_; |
| 146 | int length_; |
| 147 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 148 | template <typename CookedComparer> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | class RawComparer { |
| 150 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 151 | explicit RawComparer(CookedComparer cmp) : cmp_(cmp) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 152 | bool operator()(const T& a, const T& b) { |
| 153 | return cmp_(&a, &b) < 0; |
| 154 | } |
| 155 | |
| 156 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 157 | CookedComparer cmp_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | }; |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | template <typename T> |
| 163 | class ScopedVector : public Vector<T> { |
| 164 | public: |
| 165 | explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { } |
| 166 | ~ScopedVector() { |
| 167 | DeleteArray(this->start()); |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | inline int StrLength(const char* string) { |
| 176 | size_t length = strlen(string); |
| 177 | DCHECK(length == static_cast<size_t>(static_cast<int>(length))); |
| 178 | return static_cast<int>(length); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | #define STATIC_CHAR_VECTOR(x) \ |
| 183 | v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \ |
| 184 | arraysize(x) - 1) |
| 185 | |
| 186 | inline Vector<const char> CStrVector(const char* data) { |
| 187 | return Vector<const char>(data, StrLength(data)); |
| 188 | } |
| 189 | |
| 190 | inline Vector<const uint8_t> OneByteVector(const char* data, int length) { |
| 191 | return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length); |
| 192 | } |
| 193 | |
| 194 | inline Vector<const uint8_t> OneByteVector(const char* data) { |
| 195 | return OneByteVector(data, StrLength(data)); |
| 196 | } |
| 197 | |
| 198 | inline Vector<char> MutableCStrVector(char* data) { |
| 199 | return Vector<char>(data, StrLength(data)); |
| 200 | } |
| 201 | |
| 202 | inline Vector<char> MutableCStrVector(char* data, int max) { |
| 203 | int length = StrLength(data); |
| 204 | return Vector<char>(data, (length < max) ? length : max); |
| 205 | } |
| 206 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 207 | template <typename T, int N> |
| 208 | inline Vector<T> ArrayVector(T (&arr)[N]) { |
| 209 | return Vector<T>(arr); |
| 210 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 211 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 212 | } // namespace internal |
| 213 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | |
| 215 | #endif // V8_VECTOR_H_ |