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_LE(0, index);
55 DCHECK_LT(index, length_);
56 return start_[index];
57 }
58
at(int index)59 const T& at(int index) const { return operator[](index); }
60
first()61 T& first() { return start_[0]; }
62
last()63 T& last() { return start_[length_ - 1]; }
64
65 typedef T* iterator;
begin()66 inline iterator begin() const { return &start_[0]; }
end()67 inline iterator end() const { return &start_[length_]; }
68
69 // Returns a clone of this vector with a new backing store.
Clone()70 Vector<T> Clone() const {
71 T* result = NewArray<T>(length_);
72 for (int i = 0; i < length_; i++) result[i] = start_[i];
73 return Vector<T>(result, length_);
74 }
75
76 template <typename CompareFunction>
Sort(CompareFunction cmp,size_t s,size_t l)77 void Sort(CompareFunction cmp, size_t s, size_t l) {
78 std::sort(start() + s, start() + s + l, RawComparer<CompareFunction>(cmp));
79 }
80
81 template <typename CompareFunction>
Sort(CompareFunction cmp)82 void Sort(CompareFunction cmp) {
83 std::sort(start(), start() + length(), RawComparer<CompareFunction>(cmp));
84 }
85
Sort()86 void Sort() {
87 std::sort(start(), start() + length());
88 }
89
90 template <typename CompareFunction>
StableSort(CompareFunction cmp,size_t s,size_t l)91 void StableSort(CompareFunction cmp, size_t s, size_t l) {
92 std::stable_sort(start() + s, start() + s + l,
93 RawComparer<CompareFunction>(cmp));
94 }
95
96 template <typename CompareFunction>
StableSort(CompareFunction cmp)97 void StableSort(CompareFunction cmp) {
98 std::stable_sort(start(), start() + length(),
99 RawComparer<CompareFunction>(cmp));
100 }
101
StableSort()102 void StableSort() { std::stable_sort(start(), start() + length()); }
103
Truncate(int length)104 void Truncate(int length) {
105 DCHECK(length <= length_);
106 length_ = length;
107 }
108
109 // Releases the array underlying this vector. Once disposed the
110 // vector is empty.
Dispose()111 void Dispose() {
112 DeleteArray(start_);
113 start_ = NULL;
114 length_ = 0;
115 }
116
117 inline Vector<T> operator+(int offset) {
118 DCHECK(offset < length_);
119 return Vector<T>(start_ + offset, length_ - offset);
120 }
121
122 // Factory method for creating empty vectors.
empty()123 static Vector<T> empty() { return Vector<T>(NULL, 0); }
124
125 template<typename S>
cast(Vector<S> input)126 static Vector<T> cast(Vector<S> input) {
127 return Vector<T>(reinterpret_cast<T*>(input.start()),
128 input.length() * sizeof(S) / sizeof(T));
129 }
130
131 bool operator==(const Vector<T>& other) const {
132 if (length_ != other.length_) return false;
133 if (start_ == other.start_) return true;
134 for (int i = 0; i < length_; ++i) {
135 if (start_[i] != other.start_[i]) {
136 return false;
137 }
138 }
139 return true;
140 }
141
142 protected:
set_start(T * start)143 void set_start(T* start) { start_ = start; }
144
145 private:
146 T* start_;
147 int length_;
148
149 template <typename CookedComparer>
150 class RawComparer {
151 public:
RawComparer(CookedComparer cmp)152 explicit RawComparer(CookedComparer cmp) : cmp_(cmp) {}
operator()153 bool operator()(const T& a, const T& b) {
154 return cmp_(&a, &b) < 0;
155 }
156
157 private:
158 CookedComparer cmp_;
159 };
160 };
161
162
163 template <typename T>
164 class ScopedVector : public Vector<T> {
165 public:
ScopedVector(int length)166 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
~ScopedVector()167 ~ScopedVector() {
168 DeleteArray(this->start());
169 }
170
171 private:
172 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
173 };
174
175
StrLength(const char * string)176 inline int StrLength(const char* string) {
177 size_t length = strlen(string);
178 DCHECK(length == static_cast<size_t>(static_cast<int>(length)));
179 return static_cast<int>(length);
180 }
181
182
183 #define STATIC_CHAR_VECTOR(x) \
184 v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \
185 arraysize(x) - 1)
186
CStrVector(const char * data)187 inline Vector<const char> CStrVector(const char* data) {
188 return Vector<const char>(data, StrLength(data));
189 }
190
OneByteVector(const char * data,int length)191 inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
192 return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
193 }
194
OneByteVector(const char * data)195 inline Vector<const uint8_t> OneByteVector(const char* data) {
196 return OneByteVector(data, StrLength(data));
197 }
198
MutableCStrVector(char * data)199 inline Vector<char> MutableCStrVector(char* data) {
200 return Vector<char>(data, StrLength(data));
201 }
202
MutableCStrVector(char * data,int max)203 inline Vector<char> MutableCStrVector(char* data, int max) {
204 int length = StrLength(data);
205 return Vector<char>(data, (length < max) ? length : max);
206 }
207
208 template <typename T, int N>
ArrayVector(T (& arr)[N])209 inline Vector<T> ArrayVector(T (&arr)[N]) {
210 return Vector<T>(arr);
211 }
212
213 } // namespace internal
214 } // namespace v8
215
216 #endif // V8_VECTOR_H_
217