Home | History | Annotate | Download | only in src
      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.
      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 
     15 namespace v8 {
     16 namespace internal {
     17 
     18 
     19 template <typename T>
     20 class Vector {
     21  public:
     22   Vector() : start_(NULL), length_(0) {}
     23   Vector(T* data, int length) : start_(data), length_(length) {
     24     ASSERT(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_ASSERT(to <= length_);
     35     SLOW_ASSERT(from < to);
     36     ASSERT(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     ASSERT(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 
     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_);
     66   }
     67 
     68   void Sort(int (*cmp)(const T*, const T*)) {
     69     std::sort(start(), start() + length(), RawComparer(cmp));
     70   }
     71 
     72   void Sort() {
     73     std::sort(start(), start() + length());
     74   }
     75 
     76   void Truncate(int length) {
     77     ASSERT(length <= length_);
     78     length_ = length;
     79   }
     80 
     81   // Releases the array underlying this vector. Once disposed the
     82   // vector is empty.
     83   void Dispose() {
     84     DeleteArray(start_);
     85     start_ = NULL;
     86     length_ = 0;
     87   }
     88 
     89   inline Vector<T> operator+(int offset) {
     90     ASSERT(offset < length_);
     91     return Vector<T>(start_ + offset, length_ - offset);
     92   }
     93 
     94   // Factory method for creating empty vectors.
     95   static Vector<T> empty() { return Vector<T>(NULL, 0); }
     96 
     97   template<typename S>
     98   static Vector<T> cast(Vector<S> input) {
     99     return Vector<T>(reinterpret_cast<T*>(input.start()),
    100                      input.length() * sizeof(S) / sizeof(T));
    101   }
    102 
    103  protected:
    104   void set_start(T* start) { start_ = start; }
    105 
    106  private:
    107   T* start_;
    108   int length_;
    109 
    110   class RawComparer {
    111    public:
    112     explicit RawComparer(int (*cmp)(const T*, const T*)) : cmp_(cmp) {}
    113     bool operator()(const T& a, const T& b) {
    114       return cmp_(&a, &b) < 0;
    115     }
    116 
    117    private:
    118     int (*cmp_)(const T*, const T*);
    119   };
    120 };
    121 
    122 
    123 template <typename T>
    124 class ScopedVector : public Vector<T> {
    125  public:
    126   explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
    127   ~ScopedVector() {
    128     DeleteArray(this->start());
    129   }
    130 
    131  private:
    132   DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
    133 };
    134 
    135 
    136 inline int StrLength(const char* string) {
    137   size_t length = strlen(string);
    138   ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
    139   return static_cast<int>(length);
    140 }
    141 
    142 
    143 #define STATIC_ASCII_VECTOR(x)                        \
    144   v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \
    145                                       ARRAY_SIZE(x)-1)
    146 
    147 inline Vector<const char> CStrVector(const char* data) {
    148   return Vector<const char>(data, StrLength(data));
    149 }
    150 
    151 inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
    152   return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
    153 }
    154 
    155 inline Vector<const uint8_t> OneByteVector(const char* data) {
    156   return OneByteVector(data, StrLength(data));
    157 }
    158 
    159 inline Vector<char> MutableCStrVector(char* data) {
    160   return Vector<char>(data, StrLength(data));
    161 }
    162 
    163 inline Vector<char> MutableCStrVector(char* data, int max) {
    164   int length = StrLength(data);
    165   return Vector<char>(data, (length < max) ? length : max);
    166 }
    167 
    168 
    169 } }  // namespace v8::internal
    170 
    171 #endif  // V8_VECTOR_H_
    172