blob: a4fdb1060efa195056e5c9c6e7df11065c4e584a [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
15namespace v8 {
16namespace internal {
17
18
19template <typename T>
20class 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
27 static Vector<T> New(int length) {
28 return Vector<T>(NewArray<T>(length), length);
29 }
30
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);
36 DCHECK(0 <= from);
37 return Vector<T>(start() + from, to - from);
38 }
39
40 // Returns the length of the vector.
41 int length() const { return length_; }
42
43 // Returns whether or not the vector is empty.
44 bool is_empty() const { return length_ == 0; }
45
46 // Returns the pointer to the start of the data in the vector.
47 T* start() const { return start_; }
48
49 // Access individual vector elements - checks bounds in debug mode.
50 T& operator[](int index) const {
51 DCHECK(0 <= index && index < length_);
52 return start_[index];
53 }
54
55 const T& at(int index) const { return operator[](index); }
56
57 T& first() { return start_[0]; }
58
59 T& last() { return start_[length_ - 1]; }
60
Emily Bernierd0a1eb72015-03-24 16:35:39 -040061 typedef T* iterator;
62 inline iterator begin() const { return &start_[0]; }
63 inline iterator end() const { return &start_[length_]; }
64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 // Returns a clone of this vector with a new backing store.
66 Vector<T> Clone() const {
67 T* result = NewArray<T>(length_);
68 for (int i = 0; i < length_; i++) result[i] = start_[i];
69 return Vector<T>(result, length_);
70 }
71
72 void Sort(int (*cmp)(const T*, const T*)) {
73 std::sort(start(), start() + length(), RawComparer(cmp));
74 }
75
76 void Sort() {
77 std::sort(start(), start() + length());
78 }
79
80 void Truncate(int length) {
81 DCHECK(length <= length_);
82 length_ = length;
83 }
84
85 // Releases the array underlying this vector. Once disposed the
86 // vector is empty.
87 void Dispose() {
88 DeleteArray(start_);
89 start_ = NULL;
90 length_ = 0;
91 }
92
93 inline Vector<T> operator+(int offset) {
94 DCHECK(offset < length_);
95 return Vector<T>(start_ + offset, length_ - offset);
96 }
97
98 // Factory method for creating empty vectors.
99 static Vector<T> empty() { return Vector<T>(NULL, 0); }
100
101 template<typename S>
102 static Vector<T> cast(Vector<S> input) {
103 return Vector<T>(reinterpret_cast<T*>(input.start()),
104 input.length() * sizeof(S) / sizeof(T));
105 }
106
107 bool operator==(const Vector<T>& other) const {
108 if (length_ != other.length_) return false;
109 if (start_ == other.start_) return true;
110 for (int i = 0; i < length_; ++i) {
111 if (start_[i] != other.start_[i]) {
112 return false;
113 }
114 }
115 return true;
116 }
117
118 protected:
119 void set_start(T* start) { start_ = start; }
120
121 private:
122 T* start_;
123 int length_;
124
125 class RawComparer {
126 public:
127 explicit RawComparer(int (*cmp)(const T*, const T*)) : cmp_(cmp) {}
128 bool operator()(const T& a, const T& b) {
129 return cmp_(&a, &b) < 0;
130 }
131
132 private:
133 int (*cmp_)(const T*, const T*);
134 };
135};
136
137
138template <typename T>
139class ScopedVector : public Vector<T> {
140 public:
141 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
142 ~ScopedVector() {
143 DeleteArray(this->start());
144 }
145
146 private:
147 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
148};
149
150
151inline int StrLength(const char* string) {
152 size_t length = strlen(string);
153 DCHECK(length == static_cast<size_t>(static_cast<int>(length)));
154 return static_cast<int>(length);
155}
156
157
158#define STATIC_CHAR_VECTOR(x) \
159 v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \
160 arraysize(x) - 1)
161
162inline Vector<const char> CStrVector(const char* data) {
163 return Vector<const char>(data, StrLength(data));
164}
165
166inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
167 return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
168}
169
170inline Vector<const uint8_t> OneByteVector(const char* data) {
171 return OneByteVector(data, StrLength(data));
172}
173
174inline Vector<char> MutableCStrVector(char* data) {
175 return Vector<char>(data, StrLength(data));
176}
177
178inline Vector<char> MutableCStrVector(char* data, int max) {
179 int length = StrLength(data);
180 return Vector<char>(data, (length < max) ? length : max);
181}
182
183
184} } // namespace v8::internal
185
186#endif // V8_VECTOR_H_