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.
11 #include "src/allocation.h"
12 #include "src/checks.h"
13 #include "src/globals.h"
22 Vector() : start_(NULL), length_(0) {}
23 Vector(T* data, int length) : start_(data), length_(length) {
24 DCHECK(length == 0 || (length > 0 && data != NULL));
27 static Vector<T> New(int length) {
28 return Vector<T>(NewArray<T>(length), length);
31 // Returns a vector using the same backing storage as this one,
32 // spanning from and including 'from', to but not including 'to'.
33 Vector<T> SubVector(int from, int to) {
34 SLOW_DCHECK(to <= length_);
35 SLOW_DCHECK(from < to);
37 return Vector<T>(start() + from, to - from);
40 // Returns the length of the vector.
41 int length() const { return length_; }
43 // Returns whether or not the vector is empty.
44 bool is_empty() const { return length_ == 0; }
46 // Returns the pointer to the start of the data in the vector.
47 T* start() const { return start_; }
49 // Access individual vector elements - checks bounds in debug mode.
50 T& operator[](int index) const {
51 DCHECK(0 <= index && index < length_);
55 const T& at(int index) const { return operator[](index); }
57 T& first() { return start_[0]; }
59 T& last() { return start_[length_ - 1]; }
61 // Returns a clone of this vector with a new backing store.
62 Vector<T> Clone() const {
63 T* result = NewArray<T>(length_);
64 for (int i = 0; i < length_; i++) result[i] = start_[i];
65 return Vector<T>(result, length_);
68 void Sort(int (*cmp)(const T*, const T*)) {
69 std::sort(start(), start() + length(), RawComparer(cmp));
73 std::sort(start(), start() + length());
76 void Truncate(int length) {
77 DCHECK(length <= length_);
81 // Releases the array underlying this vector. Once disposed the
89 inline Vector<T> operator+(int offset) {
90 DCHECK(offset < length_);
91 return Vector<T>(start_ + offset, length_ - offset);
94 // Factory method for creating empty vectors.
95 static Vector<T> empty() { return Vector<T>(NULL, 0); }
98 static Vector<T> cast(Vector<S> input) {
99 return Vector<T>(reinterpret_cast<T*>(input.start()),
100 input.length() * sizeof(S) / sizeof(T));
103 bool operator==(const Vector<T>& other) const {
104 if (length_ != other.length_) return false;
105 if (start_ == other.start_) return true;
106 for (int i = 0; i < length_; ++i) {
107 if (start_[i] != other.start_[i]) {
115 void set_start(T* start) { start_ = start; }
123 explicit RawComparer(int (*cmp)(const T*, const T*)) : cmp_(cmp) {}
124 bool operator()(const T& a, const T& b) {
125 return cmp_(&a, &b) < 0;
129 int (*cmp_)(const T*, const T*);
134 template <typename T>
135 class ScopedVector : public Vector<T> {
137 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
139 DeleteArray(this->start());
143 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
147 inline int StrLength(const char* string) {
148 size_t length = strlen(string);
149 DCHECK(length == static_cast<size_t>(static_cast<int>(length)));
150 return static_cast<int>(length);
154 #define STATIC_CHAR_VECTOR(x) \
155 v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \
158 inline Vector<const char> CStrVector(const char* data) {
159 return Vector<const char>(data, StrLength(data));
162 inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
163 return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
166 inline Vector<const uint8_t> OneByteVector(const char* data) {
167 return OneByteVector(data, StrLength(data));
170 inline Vector<char> MutableCStrVector(char* data) {
171 return Vector<char>(data, StrLength(data));
174 inline Vector<char> MutableCStrVector(char* data, int max) {
175 int length = StrLength(data);
176 return Vector<char>(data, (length < max) ? length : max);
180 } } // namespace v8::internal
182 #endif // V8_VECTOR_H_