// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_VECTOR_H_ #define V8_VECTOR_H_ #include #include #include "src/allocation.h" #include "src/checks.h" #include "src/globals.h" namespace v8 { namespace internal { template class Vector { public: Vector() : start_(NULL), length_(0) {} Vector(T* data, int length) : start_(data), length_(length) { DCHECK(length == 0 || (length > 0 && data != NULL)); } static Vector New(int length) { return Vector(NewArray(length), length); } // Returns a vector using the same backing storage as this one, // spanning from and including 'from', to but not including 'to'. Vector SubVector(int from, int to) { DCHECK(0 <= from); SLOW_DCHECK(from < to); SLOW_DCHECK(static_cast(to) <= static_cast(length_)); return Vector(start() + from, to - from); } // Returns the length of the vector. int length() const { return length_; } // Returns whether or not the vector is empty. bool is_empty() const { return length_ == 0; } // Returns the pointer to the start of the data in the vector. T* start() const { return start_; } // Access individual vector elements - checks bounds in debug mode. T& operator[](int index) const { DCHECK(0 <= index && index < length_); return start_[index]; } const T& at(int index) const { return operator[](index); } T& first() { return start_[0]; } T& last() { return start_[length_ - 1]; } typedef T* iterator; inline iterator begin() const { return &start_[0]; } inline iterator end() const { return &start_[length_]; } // Returns a clone of this vector with a new backing store. Vector Clone() const { T* result = NewArray(length_); for (int i = 0; i < length_; i++) result[i] = start_[i]; return Vector(result, length_); } template void Sort(CompareFunction cmp, size_t s, size_t l) { std::sort(start() + s, start() + s + l, RawComparer(cmp)); } template void Sort(CompareFunction cmp) { std::sort(start(), start() + length(), RawComparer(cmp)); } void Sort() { std::sort(start(), start() + length()); } template void StableSort(CompareFunction cmp, size_t s, size_t l) { std::stable_sort(start() + s, start() + s + l, RawComparer(cmp)); } template void StableSort(CompareFunction cmp) { std::stable_sort(start(), start() + length(), RawComparer(cmp)); } void StableSort() { std::stable_sort(start(), start() + length()); } void Truncate(int length) { DCHECK(length <= length_); length_ = length; } // Releases the array underlying this vector. Once disposed the // vector is empty. void Dispose() { DeleteArray(start_); start_ = NULL; length_ = 0; } inline Vector operator+(int offset) { DCHECK(offset < length_); return Vector(start_ + offset, length_ - offset); } // Factory method for creating empty vectors. static Vector empty() { return Vector(NULL, 0); } template static Vector cast(Vector input) { return Vector(reinterpret_cast(input.start()), input.length() * sizeof(S) / sizeof(T)); } bool operator==(const Vector& other) const { if (length_ != other.length_) return false; if (start_ == other.start_) return true; for (int i = 0; i < length_; ++i) { if (start_[i] != other.start_[i]) { return false; } } return true; } protected: void set_start(T* start) { start_ = start; } private: T* start_; int length_; template class RawComparer { public: explicit RawComparer(CookedComparer cmp) : cmp_(cmp) {} bool operator()(const T& a, const T& b) { return cmp_(&a, &b) < 0; } private: CookedComparer cmp_; }; }; template class ScopedVector : public Vector { public: explicit ScopedVector(int length) : Vector(NewArray(length), length) { } ~ScopedVector() { DeleteArray(this->start()); } private: DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); }; inline int StrLength(const char* string) { size_t length = strlen(string); DCHECK(length == static_cast(static_cast(length))); return static_cast(length); } #define STATIC_CHAR_VECTOR(x) \ v8::internal::Vector(reinterpret_cast(x), \ arraysize(x) - 1) inline Vector CStrVector(const char* data) { return Vector(data, StrLength(data)); } inline Vector OneByteVector(const char* data, int length) { return Vector(reinterpret_cast(data), length); } inline Vector OneByteVector(const char* data) { return OneByteVector(data, StrLength(data)); } inline Vector MutableCStrVector(char* data) { return Vector(data, StrLength(data)); } inline Vector MutableCStrVector(char* data, int max) { int length = StrLength(data); return Vector(data, (length < max) ? length : max); } } // namespace internal } // namespace v8 #endif // V8_VECTOR_H_