vector.h 8.4 KB
Newer Older
1
// Copyright 2014 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_UTILS_VECTOR_H_
#define V8_UTILS_VECTOR_H_
7 8

#include <algorithm>
9 10
#include <cstring>
#include <iterator>
11

12 13
#include "src/common/checks.h"
#include "src/common/globals.h"
14
#include "src/utils/allocation.h"
15 16 17 18 19 20 21

namespace v8 {
namespace internal {

template <typename T>
class Vector {
 public:
22
  constexpr Vector() : start_(nullptr), length_(0) {}
23

24 25
  constexpr Vector(T* data, size_t length) : start_(data), length_(length) {
#ifdef V8_CAN_HAVE_DCHECK_IN_CONSTEXPR
26
    DCHECK(length == 0 || data != nullptr);
27
#endif
28 29
  }

30
  static Vector<T> New(size_t length) {
31 32 33 34 35
    return Vector<T>(NewArray<T>(length), length);
  }

  // Returns a vector using the same backing storage as this one,
  // spanning from and including 'from', to but not including 'to'.
36
  Vector<T> SubVector(size_t from, size_t to) const {
37 38
    DCHECK_LE(from, to);
    DCHECK_LE(to, length_);
39
    return Vector<T>(begin() + from, to - from);
40 41
  }

42 43
  // Returns the length of the vector. Only use this if you really need an
  // integer return value. Use {size()} otherwise.
44
  int length() const {
45
    DCHECK_GE(std::numeric_limits<int>::max(), length_);
46 47 48 49
    return static_cast<int>(length_);
  }

  // Returns the length of the vector as a size_t.
50
  constexpr size_t size() const { return length_; }
51 52

  // Returns whether or not the vector is empty.
53
  constexpr bool empty() const { return length_ == 0; }
54 55

  // Access individual vector elements - checks bounds in debug mode.
56
  T& operator[](size_t index) const {
57
    DCHECK_LT(index, length_);
58 59 60
    return start_[index];
  }

61
  const T& at(size_t index) const { return operator[](index); }
62 63 64

  T& first() { return start_[0]; }

65 66 67 68
  T& last() {
    DCHECK_LT(0, length_);
    return start_[length_ - 1];
  }
69

70 71 72 73 74
  // Returns a pointer to the start of the data in the vector.
  constexpr T* begin() const { return start_; }

  // Returns a pointer past the end of the data in the vector.
  constexpr T* end() const { return start_ + length_; }
75

76 77 78
  // Returns a clone of this vector with a new backing store.
  Vector<T> Clone() const {
    T* result = NewArray<T>(length_);
79
    for (size_t i = 0; i < length_; i++) result[i] = start_[i];
80 81 82
    return Vector<T>(result, length_);
  }

83
  void Truncate(size_t length) {
84
    DCHECK(length <= length_);
85 86 87 88 89 90 91
    length_ = length;
  }

  // Releases the array underlying this vector. Once disposed the
  // vector is empty.
  void Dispose() {
    DeleteArray(start_);
92
    start_ = nullptr;
93 94 95
    length_ = 0;
  }

96
  Vector<T> operator+(size_t offset) {
97
    DCHECK_LE(offset, length_);
98 99 100
    return Vector<T>(start_ + offset, length_ - offset);
  }

101 102 103 104 105 106 107
  Vector<T> operator+=(size_t offset) {
    DCHECK_LE(offset, length_);
    start_ += offset;
    length_ -= offset;
    return *this;
  }

108
  // Implicit conversion from Vector<T> to Vector<const T>.
109 110 111
  inline operator Vector<const T>() const {
    return Vector<const T>::cast(*this);
  }
112

113 114
  template <typename S>
  static constexpr Vector<T> cast(Vector<S> input) {
115
    return Vector<T>(reinterpret_cast<T*>(input.begin()),
116 117 118
                     input.length() * sizeof(S) / sizeof(T));
  }

119
  bool operator==(const Vector<const T> other) const {
120 121
    if (length_ != other.length_) return false;
    if (start_ == other.start_) return true;
122
    for (size_t i = 0; i < length_; ++i) {
123 124 125 126 127 128 129
      if (start_[i] != other.start_[i]) {
        return false;
      }
    }
    return true;
  }

130 131
 private:
  T* start_;
132
  size_t length_;
133 134 135 136 137
};

template <typename T>
class ScopedVector : public Vector<T> {
 public:
138 139
  explicit ScopedVector(size_t length)
      : Vector<T>(NewArray<T>(length), length) {}
140
  ~ScopedVector() { DeleteArray(this->begin()); }
141 142 143 144 145

 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
};

146 147 148 149 150 151 152 153
template <typename T>
class OwnedVector {
 public:
  MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(OwnedVector);
  OwnedVector(std::unique_ptr<T[]> data, size_t length)
      : data_(std::move(data)), length_(length) {
    DCHECK_IMPLIES(length_ > 0, data_ != nullptr);
  }
154 155 156 157 158 159 160
  // Implicit conversion from {OwnedVector<U>} to {OwnedVector<T>}, instantiable
  // if {std::unique_ptr<U>} can be converted to {std::unique_ptr<T>}.
  // Can be used to convert {OwnedVector<T>} to {OwnedVector<const T>}.
  template <typename U,
            typename = typename std::enable_if<std::is_convertible<
                std::unique_ptr<U>, std::unique_ptr<T>>::value>::type>
  OwnedVector(OwnedVector<U>&& other)
161 162 163 164
      : data_(std::move(other.data_)), length_(other.length_) {
    STATIC_ASSERT(sizeof(U) == sizeof(T));
    other.length_ = 0;
  }
165 166 167 168 169

  // Returns the length of the vector as a size_t.
  constexpr size_t size() const { return length_; }

  // Returns whether or not the vector is empty.
170
  constexpr bool empty() const { return length_ == 0; }
171 172 173 174 175 176 177

  // Returns the pointer to the start of the data in the vector.
  T* start() const {
    DCHECK_IMPLIES(length_ > 0, data_ != nullptr);
    return data_.get();
  }

178 179 180
  constexpr T* begin() const { return start(); }
  constexpr T* end() const { return start() + size(); }

181 182 183 184 185 186
  // Access individual vector elements - checks bounds in debug mode.
  T& operator[](size_t index) const {
    DCHECK_LT(index, length_);
    return data_[index];
  }

187 188 189 190
  // Returns a {Vector<T>} view of the data in this vector.
  Vector<T> as_vector() const { return Vector<T>(start(), size()); }

  // Releases the backing data from this vector and transfers ownership to the
191 192 193 194 195
  // caller. This vector will be empty afterwards.
  std::unique_ptr<T[]> ReleaseData() {
    length_ = 0;
    return std::move(data_);
  }
196 197 198

  // Allocates a new vector of the specified size via the default allocator.
  static OwnedVector<T> New(size_t size) {
199
    if (size == 0) return {};
200 201 202
    return OwnedVector<T>(std::unique_ptr<T[]>(new T[size]), size);
  }

203 204 205 206 207 208 209 210 211 212 213 214 215 216
  // Allocates a new vector containing the specified collection of values.
  // {Iterator} is the common type of {std::begin} and {std::end} called on a
  // {const U&}. This function is only instantiable if that type exists.
  template <typename U, typename Iterator = typename std::common_type<
                            decltype(std::begin(std::declval<const U&>())),
                            decltype(std::end(std::declval<const U&>()))>::type>
  static OwnedVector<T> Of(const U& collection) {
    Iterator begin = std::begin(collection);
    Iterator end = std::end(collection);
    OwnedVector<T> vec = New(std::distance(begin, end));
    std::copy(begin, end, vec.start());
    return vec;
  }

217 218 219
  bool operator==(std::nullptr_t) const { return data_ == nullptr; }
  bool operator!=(std::nullptr_t) const { return data_ != nullptr; }

220
 private:
221 222 223
  template <typename U>
  friend class OwnedVector;

224 225 226
  std::unique_ptr<T[]> data_;
  size_t length_ = 0;
};
227

228 229 230 231
template <size_t N>
constexpr Vector<const uint8_t> StaticCharVector(const char (&array)[N]) {
  return Vector<const uint8_t>::cast(Vector<const char>(array, N - 1));
}
232 233

inline Vector<const char> CStrVector(const char* data) {
234
  return Vector<const char>(data, strlen(data));
235 236
}

237
inline Vector<const uint8_t> OneByteVector(const char* data, size_t length) {
238 239 240 241
  return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
}

inline Vector<const uint8_t> OneByteVector(const char* data) {
242
  return OneByteVector(data, strlen(data));
243 244 245
}

inline Vector<char> MutableCStrVector(char* data) {
246
  return Vector<char>(data, strlen(data));
247 248
}

249 250
inline Vector<char> MutableCStrVector(char* data, size_t max) {
  return Vector<char>(data, strnlen(data, max));
251 252
}

253
template <typename T, size_t N>
254
inline constexpr Vector<T> ArrayVector(T (&arr)[N]) {
255
  return Vector<T>{arr, N};
256
}
257

258 259 260 261 262 263
// Construct a Vector from a start pointer and a size.
template <typename T>
inline constexpr Vector<T> VectorOf(T* start, size_t size) {
  return Vector<T>(start, size);
}

264
// Construct a Vector from anything providing a {data()} and {size()} accessor.
265 266 267 268
template <typename Container>
inline constexpr auto VectorOf(Container&& c)
    -> decltype(VectorOf(c.data(), c.size())) {
  return VectorOf(c.data(), c.size());
269 270
}

271
template <typename T, size_t kSize>
272 273 274 275
class EmbeddedVector : public Vector<T> {
 public:
  EmbeddedVector() : Vector<T>(buffer_, kSize) {}

276 277
  explicit EmbeddedVector(const T& initial_value) : Vector<T>(buffer_, kSize) {
    std::fill_n(buffer_, kSize, initial_value);
278 279 280 281
  }

 private:
  T buffer_[kSize];
282 283

  DISALLOW_COPY_AND_ASSIGN(EmbeddedVector);
284 285
};

286 287
}  // namespace internal
}  // namespace v8
288

289
#endif  // V8_UTILS_VECTOR_H_