Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: src/vector.h

Issue 254823002: Revert 20962 "Break cyclic reference between utils and platform." (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/utils.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_VECTOR_H_
29 #define V8_VECTOR_H_
30
31 #include <string.h>
32 #include <algorithm>
33
34 #include "allocation.h"
35 #include "checks.h"
36 #include "globals.h"
37
38 namespace v8 {
39 namespace internal {
40
41
42 template <typename T>
43 class Vector {
44 public:
45 Vector() : start_(NULL), length_(0) {}
46 Vector(T* data, int length) : start_(data), length_(length) {
47 ASSERT(length == 0 || (length > 0 && data != NULL));
48 }
49
50 static Vector<T> New(int length) {
51 return Vector<T>(NewArray<T>(length), length);
52 }
53
54 // Returns a vector using the same backing storage as this one,
55 // spanning from and including 'from', to but not including 'to'.
56 Vector<T> SubVector(int from, int to) {
57 SLOW_ASSERT(to <= length_);
58 SLOW_ASSERT(from < to);
59 ASSERT(0 <= from);
60 return Vector<T>(start() + from, to - from);
61 }
62
63 // Returns the length of the vector.
64 int length() const { return length_; }
65
66 // Returns whether or not the vector is empty.
67 bool is_empty() const { return length_ == 0; }
68
69 // Returns the pointer to the start of the data in the vector.
70 T* start() const { return start_; }
71
72 // Access individual vector elements - checks bounds in debug mode.
73 T& operator[](int index) const {
74 ASSERT(0 <= index && index < length_);
75 return start_[index];
76 }
77
78 const T& at(int index) const { return operator[](index); }
79
80 T& first() { return start_[0]; }
81
82 T& last() { return start_[length_ - 1]; }
83
84 // Returns a clone of this vector with a new backing store.
85 Vector<T> Clone() const {
86 T* result = NewArray<T>(length_);
87 for (int i = 0; i < length_; i++) result[i] = start_[i];
88 return Vector<T>(result, length_);
89 }
90
91 void Sort(int (*cmp)(const T*, const T*)) {
92 std::sort(start(), start() + length(), RawComparer(cmp));
93 }
94
95 void Sort() {
96 std::sort(start(), start() + length());
97 }
98
99 void Truncate(int length) {
100 ASSERT(length <= length_);
101 length_ = length;
102 }
103
104 // Releases the array underlying this vector. Once disposed the
105 // vector is empty.
106 void Dispose() {
107 DeleteArray(start_);
108 start_ = NULL;
109 length_ = 0;
110 }
111
112 inline Vector<T> operator+(int offset) {
113 ASSERT(offset < length_);
114 return Vector<T>(start_ + offset, length_ - offset);
115 }
116
117 // Factory method for creating empty vectors.
118 static Vector<T> empty() { return Vector<T>(NULL, 0); }
119
120 template<typename S>
121 static Vector<T> cast(Vector<S> input) {
122 return Vector<T>(reinterpret_cast<T*>(input.start()),
123 input.length() * sizeof(S) / sizeof(T));
124 }
125
126 protected:
127 void set_start(T* start) { start_ = start; }
128
129 private:
130 T* start_;
131 int length_;
132
133 class RawComparer {
134 public:
135 explicit RawComparer(int (*cmp)(const T*, const T*)) : cmp_(cmp) {}
136 bool operator()(const T& a, const T& b) {
137 return cmp_(&a, &b) < 0;
138 }
139
140 private:
141 int (*cmp_)(const T*, const T*);
142 };
143 };
144
145
146 template <typename T>
147 class ScopedVector : public Vector<T> {
148 public:
149 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
150 ~ScopedVector() {
151 DeleteArray(this->start());
152 }
153
154 private:
155 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
156 };
157
158
159 inline int StrLength(const char* string) {
160 size_t length = strlen(string);
161 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
162 return static_cast<int>(length);
163 }
164
165
166 #define STATIC_ASCII_VECTOR(x) \
167 v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \
168 ARRAY_SIZE(x)-1)
169
170 inline Vector<const char> CStrVector(const char* data) {
171 return Vector<const char>(data, StrLength(data));
172 }
173
174 inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
175 return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
176 }
177
178 inline Vector<const uint8_t> OneByteVector(const char* data) {
179 return OneByteVector(data, StrLength(data));
180 }
181
182 inline Vector<char> MutableCStrVector(char* data) {
183 return Vector<char>(data, StrLength(data));
184 }
185
186 inline Vector<char> MutableCStrVector(char* data, int max) {
187 int length = StrLength(data);
188 return Vector<char>(data, (length < max) ? length : max);
189 }
190
191
192 } } // namespace v8::internal
193
194 #endif // V8_VECTOR_H_
OLDNEW
« no previous file with comments | « src/utils.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698