v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
list-inl.h
Go to the documentation of this file.
1 // Copyright 2006-2009 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_LIST_INL_H_
29 #define V8_LIST_INL_H_
30 
31 #include "list.h"
32 
33 namespace v8 {
34 namespace internal {
35 
36 
37 template<typename T, class P>
38 void List<T, P>::Add(const T& element, P alloc) {
39  if (length_ < capacity_) {
40  data_[length_++] = element;
41  } else {
42  List<T, P>::ResizeAdd(element, alloc);
43  }
44 }
45 
46 
47 template<typename T, class P>
48 void List<T, P>::AddAll(const List<T, P>& other, P alloc) {
49  AddAll(other.ToVector(), alloc);
50 }
51 
52 
53 template<typename T, class P>
54 void List<T, P>::AddAll(const Vector<T>& other, P alloc) {
55  int result_length = length_ + other.length();
56  if (capacity_ < result_length) Resize(result_length, alloc);
57  for (int i = 0; i < other.length(); i++) {
58  data_[length_ + i] = other.at(i);
59  }
60  length_ = result_length;
61 }
62 
63 
64 // Use two layers of inlining so that the non-inlined function can
65 // use the same implementation as the inlined version.
66 template<typename T, class P>
67 void List<T, P>::ResizeAdd(const T& element, P alloc) {
68  ResizeAddInternal(element, alloc);
69 }
70 
71 
72 template<typename T, class P>
73 void List<T, P>::ResizeAddInternal(const T& element, P alloc) {
74  ASSERT(length_ >= capacity_);
75  // Grow the list capacity by 100%, but make sure to let it grow
76  // even when the capacity is zero (possible initial case).
77  int new_capacity = 1 + 2 * capacity_;
78  // Since the element reference could be an element of the list, copy
79  // it out of the old backing storage before resizing.
80  T temp = element;
81  Resize(new_capacity, alloc);
82  data_[length_++] = temp;
83 }
84 
85 
86 template<typename T, class P>
87 void List<T, P>::Resize(int new_capacity, P alloc) {
88  T* new_data = NewData(new_capacity, alloc);
89  memcpy(new_data, data_, capacity_ * sizeof(T));
90  List<T, P>::DeleteData(data_);
91  data_ = new_data;
92  capacity_ = new_capacity;
93 }
94 
95 
96 template<typename T, class P>
97 Vector<T> List<T, P>::AddBlock(T value, int count, P alloc) {
98  int start = length_;
99  for (int i = 0; i < count; i++) Add(value, alloc);
100  return Vector<T>(&data_[start], count);
101 }
102 
103 
104 template<typename T, class P>
105 void List<T, P>::InsertAt(int index, const T& elm, P alloc) {
106  ASSERT(index >= 0 && index <= length_);
107  Add(elm, alloc);
108  for (int i = length_ - 1; i > index; --i) {
109  data_[i] = data_[i - 1];
110  }
111  data_[index] = elm;
112 }
113 
114 
115 template<typename T, class P>
117  T element = at(i);
118  length_--;
119  while (i < length_) {
120  data_[i] = data_[i + 1];
121  i++;
122  }
123  return element;
124 }
125 
126 
127 template<typename T, class P>
128 bool List<T, P>::RemoveElement(const T& elm) {
129  for (int i = 0; i < length_; i++) {
130  if (data_[i] == elm) {
131  Remove(i);
132  return true;
133  }
134  }
135  return false;
136 }
137 
138 
139 template<typename T, class P>
140 void List<T, P>::Allocate(int length, P allocator) {
141  DeleteData(data_);
142  Initialize(length, allocator);
143  length_ = length;
144 }
145 
146 
147 template<typename T, class P>
148 void List<T, P>::Clear() {
149  DeleteData(data_);
150  // We don't call Initialize(0) since that requires passing a Zone,
151  // which we don't really need.
152  data_ = NULL;
153  capacity_ = 0;
154  length_ = 0;
155 }
156 
157 
158 template<typename T, class P>
159 void List<T, P>::Rewind(int pos) {
160  length_ = pos;
161 }
162 
163 
164 template<typename T, class P>
165 void List<T, P>::Iterate(void (*callback)(T* x)) {
166  for (int i = 0; i < length_; i++) callback(&data_[i]);
167 }
168 
169 
170 template<typename T, class P>
171 template<class Visitor>
172 void List<T, P>::Iterate(Visitor* visitor) {
173  for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]);
174 }
175 
176 
177 template<typename T, class P>
178 bool List<T, P>::Contains(const T& elm) const {
179  for (int i = 0; i < length_; i++) {
180  if (data_[i] == elm)
181  return true;
182  }
183  return false;
184 }
185 
186 
187 template<typename T, class P>
188 int List<T, P>::CountOccurrences(const T& elm, int start, int end) const {
189  int result = 0;
190  for (int i = start; i <= end; i++) {
191  if (data_[i] == elm) ++result;
192  }
193  return result;
194 }
195 
196 
197 template<typename T, class P>
198 void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
199  ToVector().Sort(cmp);
200 #ifdef DEBUG
201  for (int i = 1; i < length_; i++)
202  ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0);
203 #endif
204 }
205 
206 
207 template<typename T, class P>
209  Sort(PointerValueCompare<T>);
210 }
211 
212 
213 template<typename T, class P>
214 void List<T, P>::Initialize(int capacity, P allocator) {
215  ASSERT(capacity >= 0);
216  data_ = (capacity > 0) ? NewData(capacity, allocator) : NULL;
217  capacity_ = capacity;
218  length_ = 0;
219 }
220 
221 
222 template <typename T, typename P>
223 int SortedListBSearch(const List<T>& list, P cmp) {
224  int low = 0;
225  int high = list.length() - 1;
226  while (low <= high) {
227  int mid = (low + high) / 2;
228  T mid_elem = list[mid];
229 
230  if (cmp(&mid_elem) > 0) {
231  high = mid - 1;
232  continue;
233  }
234  if (cmp(&mid_elem) < 0) {
235  low = mid + 1;
236  continue;
237  }
238  // Found the elememt.
239  return mid;
240  }
241  return -1;
242 }
243 
244 
245 template<typename T>
246 class ElementCmp {
247  public:
248  explicit ElementCmp(T e) : elem_(e) {}
249  int operator()(const T* other) {
250  return PointerValueCompare(other, &elem_);
251  }
252  private:
253  T elem_;
254 };
255 
256 
257 template <typename T>
258 int SortedListBSearch(const List<T>& list, T elem) {
259  return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem));
260 }
261 
262 
263 } } // namespace v8::internal
264 
265 #endif // V8_LIST_INL_H_
void InsertAt(int index, const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:105
bool Contains(const T &elm) const
Definition: list-inl.h:178
int operator()(const T *other)
Definition: list-inl.h:249
#define ASSERT(condition)
Definition: checks.h:270
Vector< T > ToVector() const
Definition: list.h:98
T Remove(int i)
Definition: list-inl.h:116
#define T(name, string, precedence)
Definition: token.cc:48
int PointerValueCompare(const T *a, const T *b)
Definition: utils.h:167
int CountOccurrences(const T &elm, int start, int end) const
Definition: list-inl.h:188
int SortedListBSearch(const List< T > &list, P cmp)
Definition: list-inl.h:223
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:38
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
void Iterate(void(*callback)(T *x))
Definition: list-inl.h:165
Vector< T > AddBlock(T value, int count, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:97
bool RemoveElement(const T &elm)
Definition: list-inl.h:128
void AddAll(const List< T, AllocationPolicy > &other, AllocationPolicy allocator=AllocationPolicy())