12 #include "src/allocation.h" 13 #include "src/checks.h" 14 #include "src/globals.h" 23 constexpr Vector() : start_(nullptr), length_(0) {}
25 Vector(T* data,
size_t length) : start_(data), length_(length) {
26 DCHECK(length == 0 || data !=
nullptr);
30 explicit constexpr Vector(T (&arr)[N]) : start_(arr), length_(N) {}
32 static Vector<T> New(
int length) {
33 return Vector<T>(NewArray<T>(length), length);
38 Vector<T> SubVector(
size_t from,
size_t to)
const {
40 DCHECK_LE(to, length_);
41 return Vector<T>(start() + from, to - from);
46 DCHECK(length_ <= static_cast<size_t>(std::numeric_limits<int>::max()));
47 return static_cast<int>(length_);
51 constexpr
size_t size()
const {
return length_; }
54 constexpr
bool is_empty()
const {
return length_ == 0; }
57 constexpr T* start()
const {
return start_; }
60 T& operator[](
size_t index)
const {
61 DCHECK_LT(index, length_);
65 const T& at(
size_t index)
const {
return operator[](index); }
67 T& first() {
return start_[0]; }
70 DCHECK_LT(0, length_);
71 return start_[length_ - 1];
75 constexpr iterator begin()
const {
return start_; }
76 constexpr iterator end()
const {
return start_ + length_; }
79 Vector<T> Clone()
const {
80 T* result = NewArray<T>(length_);
81 for (
size_t i = 0;
i < length_;
i++) result[
i] = start_[
i];
82 return Vector<T>(result, length_);
85 template <
typename CompareFunction>
86 void Sort(CompareFunction cmp,
size_t s,
size_t l) {
87 std::sort(start() + s, start() + s + l, RawComparer<CompareFunction>(cmp));
90 template <
typename CompareFunction>
91 void Sort(CompareFunction cmp) {
92 std::sort(start(), start() + length(), RawComparer<CompareFunction>(cmp));
96 std::sort(start(), start() + length());
99 template <
typename CompareFunction>
100 void StableSort(CompareFunction cmp,
size_t s,
size_t l) {
101 std::stable_sort(start() + s, start() + s + l,
102 RawComparer<CompareFunction>(cmp));
105 template <
typename CompareFunction>
106 void StableSort(CompareFunction cmp) {
107 std::stable_sort(start(), start() + length(),
108 RawComparer<CompareFunction>(cmp));
111 void StableSort() { std::stable_sort(start(), start() + length()); }
113 void Truncate(
size_t length) {
114 DCHECK(length <= length_);
126 Vector<T> operator+(
size_t offset) {
127 DCHECK_LE(offset, length_);
128 return Vector<T>(start_ + offset, length_ - offset);
131 Vector<T> operator+=(
size_t offset) {
132 DCHECK_LE(offset, length_);
139 inline operator Vector<const T>()
const {
140 return Vector<const T>::cast(*
this);
144 static Vector<T> empty() {
return Vector<T>(
nullptr, 0); }
146 template <
typename S>
147 static constexpr Vector<T> cast(Vector<S> input) {
148 return Vector<T>(
reinterpret_cast<T*
>(input.start()),
149 input.length() *
sizeof(S) /
sizeof(T));
152 bool operator==(
const Vector<const T> other)
const {
153 if (length_ != other.length_)
return false;
154 if (start_ == other.start_)
return true;
155 for (
size_t i = 0;
i < length_; ++
i) {
156 if (start_[
i] != other.start_[
i]) {
167 template <
typename CookedComparer>
170 explicit RawComparer(CookedComparer cmp) : cmp_(cmp) {}
171 bool operator()(
const T& a,
const T& b) {
172 return cmp_(&a, &b) < 0;
181 template <
typename T>
186 DeleteArray(this->start());
193 template <
typename T>
197 OwnedVector(std::unique_ptr<T[]> data,
size_t length)
198 : data_(std::move(data)), length_(length) {
199 DCHECK_IMPLIES(length_ > 0, data_ !=
nullptr);
204 template <
typename U,
205 typename =
typename std::enable_if<std::is_convertible<
206 std::unique_ptr<U>, std::unique_ptr<T>>::value>
::type>
208 : data_(other.ReleaseData()), length_(other.size()) {}
211 constexpr
size_t size()
const {
return length_; }
214 constexpr
bool is_empty()
const {
return length_ == 0; }
218 DCHECK_IMPLIES(length_ > 0, data_ !=
nullptr);
227 std::unique_ptr<T[]> ReleaseData() {
return std::move(data_); }
231 if (size == 0)
return {};
238 template <
typename U,
typename Iterator =
typename std::common_type<
239 decltype(std::begin(std::declval<const U&>())),
240 decltype(std::end(std::declval<const U&>()))>
::type>
242 Iterator begin = std::begin(collection);
243 Iterator end = std::end(collection);
245 std::copy(begin, end, vec.start());
250 std::unique_ptr<T[]> data_;
254 inline int StrLength(
const char*
string) {
255 size_t length = strlen(
string);
256 DCHECK(length == static_cast<size_t>(static_cast<int>(length)));
257 return static_cast<int>(length);
261 #define STATIC_CHAR_VECTOR(x) \ 262 v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \ 265 inline Vector<const char> CStrVector(
const char* data) {
266 return Vector<const char>(data, StrLength(data));
269 inline Vector<const uint8_t> OneByteVector(
const char* data,
int length) {
270 return Vector<const uint8_t>(
reinterpret_cast<const uint8_t*
>(data), length);
273 inline Vector<const uint8_t> OneByteVector(
const char* data) {
274 return OneByteVector(data, StrLength(data));
277 inline Vector<char> MutableCStrVector(
char* data) {
278 return Vector<char>(data, StrLength(data));
281 inline Vector<char> MutableCStrVector(
char* data,
int max) {
282 int length = StrLength(data);
283 return Vector<char>(data, (length < max) ? length : max);
286 template <
typename T,
int N>
287 inline constexpr Vector<T> ArrayVector(T (&arr)[N]) {
288 return Vector<T>(arr);
292 template <
typename Container,
293 typename T =
typename std::remove_reference<
294 decltype(*(std::declval<Container>()).data())>::type,
295 typename = decltype((std::declval<Container>()).size())>
296 inline constexpr Vector<T> VectorOf(Container&& c) {
297 return Vector<T>(c.data(), c.size());
303 #endif // V8_VECTOR_H_