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:
Vector()22 Vector() : start_(NULL), length_(0) {}
Vector(T * data,int length)23 Vector(T* data, int length) : start_(data), length_(length) {
24 DCHECK(length == 0 || (length > 0 && data != NULL));
25 }
26
27 template <int N>
Vector(T (& arr)[N])28 explicit Vector(T (&arr)[N]) : start_(arr), length_(N) {}
29
New(int length)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'.
SubVector(int from,int to)36 Vector<T> SubVector(int from, int to) {
37 DCHECK(0 <= from);
38 SLOW_DCHECK(from < to);
39 SLOW_DCHECK(static_cast<unsigned>(to) <= static_cast<unsigned>(length_));
40 return Vector<T>(start() + from, to - from);
41 }
42
43 // Returns the length of the vector.
length()44 int length() const { return length_; }
45
46 // Returns whether or not the vector is empty.
is_empty()47 bool is_empty() const { return length_ == 0; }
48
49 // Returns the pointer to the start of the data in the vector.
start()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
at(int index)58 const T& at(int index) const { return operator[](index); }
59
first()60 T& first() { return start_[0]; }
61
last()62 T& last() { return start_[length_ - 1]; }
63
64 typedef T* iterator;
begin()65 inline iterator begin() const { return &start_[0]; }
end()66 inline iterator end() const { return &start_[length_]; }
67
68 // Returns a clone of this vector with a new backing store.
Clone()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
75 template <typename CompareFunction>
Sort(CompareFunction cmp,size_t s,size_t l)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>
Sort(CompareFunction cmp)81 void Sort(CompareFunction cmp) {
82 std::sort(start(), start() + length(), RawComparer<CompareFunction>(cmp));
83 }
84
Sort()85 void Sort() {
86 std::sort(start(), start() + length());
87 }
88
89 template <typename CompareFunction>
StableSort(CompareFunction cmp,size_t s,size_t l)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>
StableSort(CompareFunction cmp)96 void StableSort(CompareFunction cmp) {
97 std::stable_sort(start(), start() + length(),
98 RawComparer<CompareFunction>(cmp));
99 }
100
StableSort()101 void StableSort() { std::stable_sort(start(), start() + length()); }
102
Truncate(int length)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.
Dispose()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.
empty()122 static Vector<T> empty() { return Vector<T>(NULL, 0); }
123
124 template<typename S>
cast(Vector<S> input)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:
set_start(T * start)142 void set_start(T* start) { start_ = start; }
143
144 private:
145 T* start_;
146 int length_;
147
148 template <typename CookedComparer>
149 class RawComparer {
150 public:
RawComparer(CookedComparer cmp)151 explicit RawComparer(CookedComparer cmp) : cmp_(cmp) {}
operator()152 bool operator()(const T& a, const T& b) {
153 return cmp_(&a, &b) < 0;
154 }
155
156 private:
157 CookedComparer cmp_;
158 };
159 };
160
161
162 template <typename T>
163 class ScopedVector : public Vector<T> {
164 public:
ScopedVector(int length)165 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
~ScopedVector()166 ~ScopedVector() {
167 DeleteArray(this->start());
168 }
169
170 private:
171 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
172 };
173
174
StrLength(const char * string)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
CStrVector(const char * data)186 inline Vector<const char> CStrVector(const char* data) {
187 return Vector<const char>(data, StrLength(data));
188 }
189
OneByteVector(const char * data,int length)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
OneByteVector(const char * data)194 inline Vector<const uint8_t> OneByteVector(const char* data) {
195 return OneByteVector(data, StrLength(data));
196 }
197
MutableCStrVector(char * data)198 inline Vector<char> MutableCStrVector(char* data) {
199 return Vector<char>(data, StrLength(data));
200 }
201
MutableCStrVector(char * data,int max)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
207 template <typename T, int N>
ArrayVector(T (& arr)[N])208 inline Vector<T> ArrayVector(T (&arr)[N]) {
209 return Vector<T>(arr);
210 }
211
212 } // namespace internal
213 } // namespace v8
214
215 #endif // V8_VECTOR_H_
216