blob: 60e8fab6f6fb5196aa6c500e0fd3d7dfd948505a [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_LIST_INL_H_
6#define V8_LIST_INL_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/list.h"
9
10#include "src/base/platform/platform.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000011
12namespace v8 {
13namespace internal {
14
15
16template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017void List<T, P>::Add(const T& element, P alloc) {
Steve Blocka7e24c12009-10-30 11:49:00 +000018 if (length_ < capacity_) {
19 data_[length_++] = element;
20 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021 List<T, P>::ResizeAdd(element, alloc);
Steve Blocka7e24c12009-10-30 11:49:00 +000022 }
23}
24
25
26template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027void List<T, P>::AddAll(const List<T, P>& other, P alloc) {
28 AddAll(other.ToVector(), alloc);
Ben Murdoch257744e2011-11-30 15:57:28 +000029}
30
31
32template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033void List<T, P>::AddAll(const Vector<T>& other, P alloc) {
Ben Murdoch257744e2011-11-30 15:57:28 +000034 int result_length = length_ + other.length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035 if (capacity_ < result_length) Resize(result_length, alloc);
Ben Murdoch257744e2011-11-30 15:57:28 +000036 for (int i = 0; i < other.length(); i++) {
37 data_[length_ + i] = other.at(i);
Steve Blocka7e24c12009-10-30 11:49:00 +000038 }
39 length_ = result_length;
40}
41
42
43// Use two layers of inlining so that the non-inlined function can
44// use the same implementation as the inlined version.
45template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046void List<T, P>::ResizeAdd(const T& element, P alloc) {
47 ResizeAddInternal(element, alloc);
Steve Blocka7e24c12009-10-30 11:49:00 +000048}
49
50
51template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052void List<T, P>::ResizeAddInternal(const T& element, P alloc) {
53 DCHECK(length_ >= capacity_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010054 // Grow the list capacity by 100%, but make sure to let it grow
Steve Blocka7e24c12009-10-30 11:49:00 +000055 // even when the capacity is zero (possible initial case).
Ben Murdoch3ef787d2012-04-12 10:51:47 +010056 int new_capacity = 1 + 2 * capacity_;
Steve Blocka7e24c12009-10-30 11:49:00 +000057 // Since the element reference could be an element of the list, copy
58 // it out of the old backing storage before resizing.
59 T temp = element;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 Resize(new_capacity, alloc);
Steve Blocka7e24c12009-10-30 11:49:00 +000061 data_[length_++] = temp;
62}
63
64
65template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066void List<T, P>::Resize(int new_capacity, P alloc) {
67 DCHECK_LE(length_, new_capacity);
68 T* new_data = NewData(new_capacity, alloc);
69 MemCopy(new_data, data_, length_ * sizeof(T));
Steve Blocka7e24c12009-10-30 11:49:00 +000070 List<T, P>::DeleteData(data_);
71 data_ = new_data;
72 capacity_ = new_capacity;
73}
74
75
76template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077Vector<T> List<T, P>::AddBlock(T value, int count, P alloc) {
Steve Blocka7e24c12009-10-30 11:49:00 +000078 int start = length_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 for (int i = 0; i < count; i++) Add(value, alloc);
Steve Blocka7e24c12009-10-30 11:49:00 +000080 return Vector<T>(&data_[start], count);
81}
82
83
84template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085void List<T, P>::Set(int index, const T& elm) {
86 DCHECK(index >= 0 && index <= length_);
87 data_[index] = elm;
88}
89
90
91template<typename T, class P>
92void List<T, P>::InsertAt(int index, const T& elm, P alloc) {
93 DCHECK(index >= 0 && index <= length_);
94 Add(elm, alloc);
Ben Murdochb0fe1622011-05-05 13:52:32 +010095 for (int i = length_ - 1; i > index; --i) {
96 data_[i] = data_[i - 1];
97 }
98 data_[index] = elm;
99}
100
101
102template<typename T, class P>
Steve Blocka7e24c12009-10-30 11:49:00 +0000103T List<T, P>::Remove(int i) {
104 T element = at(i);
105 length_--;
106 while (i < length_) {
107 data_[i] = data_[i + 1];
108 i++;
109 }
110 return element;
111}
112
113
114template<typename T, class P>
Ben Murdochb0fe1622011-05-05 13:52:32 +0100115bool List<T, P>::RemoveElement(const T& elm) {
116 for (int i = 0; i < length_; i++) {
117 if (data_[i] == elm) {
118 Remove(i);
119 return true;
120 }
121 }
122 return false;
123}
124
125
126template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127void List<T, P>::Allocate(int length, P allocator) {
128 DeleteData(data_);
129 Initialize(length, allocator);
130 length_ = length;
131}
132
133
134template<typename T, class P>
Steve Blocka7e24c12009-10-30 11:49:00 +0000135void List<T, P>::Clear() {
136 DeleteData(data_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 // We don't call Initialize(0) since that requires passing a Zone,
138 // which we don't really need.
139 data_ = NULL;
140 capacity_ = 0;
141 length_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000142}
143
144
145template<typename T, class P>
146void List<T, P>::Rewind(int pos) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147 DCHECK(0 <= pos && pos <= length_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 length_ = pos;
149}
150
151
152template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153void List<T, P>::Trim(P alloc) {
154 if (length_ < capacity_ / 4) {
155 Resize(capacity_ / 2, alloc);
156 }
157}
158
159
160template<typename T, class P>
Steve Blocka7e24c12009-10-30 11:49:00 +0000161void List<T, P>::Iterate(void (*callback)(T* x)) {
162 for (int i = 0; i < length_; i++) callback(&data_[i]);
163}
164
165
166template<typename T, class P>
Iain Merrick75681382010-08-19 15:07:18 +0100167template<class Visitor>
168void List<T, P>::Iterate(Visitor* visitor) {
169 for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]);
170}
171
172
173template<typename T, class P>
Ben Murdochb0fe1622011-05-05 13:52:32 +0100174bool List<T, P>::Contains(const T& elm) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 for (int i = 0; i < length_; i++) {
176 if (data_[i] == elm)
177 return true;
178 }
179 return false;
180}
181
182
183template<typename T, class P>
Ben Murdochb0fe1622011-05-05 13:52:32 +0100184int List<T, P>::CountOccurrences(const T& elm, int start, int end) const {
185 int result = 0;
186 for (int i = start; i <= end; i++) {
187 if (data_[i] == elm) ++result;
188 }
189 return result;
190}
191
192
193template<typename T, class P>
Steve Blocka7e24c12009-10-30 11:49:00 +0000194void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
195 ToVector().Sort(cmp);
196#ifdef DEBUG
197 for (int i = 1; i < length_; i++)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 DCHECK(cmp(&data_[i - 1], &data_[i]) <= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000199#endif
200}
201
202
203template<typename T, class P>
204void List<T, P>::Sort() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 ToVector().Sort();
Steve Blocka7e24c12009-10-30 11:49:00 +0000206}
207
208
209template<typename T, class P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210void List<T, P>::Initialize(int capacity, P allocator) {
211 DCHECK(capacity >= 0);
212 data_ = (capacity > 0) ? NewData(capacity, allocator) : NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 capacity_ = capacity;
214 length_ = 0;
215}
216
217
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218template <typename T, typename P>
219int SortedListBSearch(const List<T>& list, P cmp) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000220 int low = 0;
221 int high = list.length() - 1;
222 while (low <= high) {
223 int mid = (low + high) / 2;
224 T mid_elem = list[mid];
225
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000226 if (cmp(&mid_elem) > 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000227 high = mid - 1;
228 continue;
229 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 if (cmp(&mid_elem) < 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000231 low = mid + 1;
232 continue;
233 }
234 // Found the elememt.
235 return mid;
236 }
237 return -1;
238}
239
240
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241template<typename T>
242class ElementCmp {
243 public:
244 explicit ElementCmp(T e) : elem_(e) {}
245 int operator()(const T* other) {
246 return PointerValueCompare(other, &elem_);
247 }
248 private:
249 T elem_;
250};
251
252
Ben Murdoch589d6972011-11-30 16:04:58 +0000253template <typename T>
254int SortedListBSearch(const List<T>& list, T elem) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem));
Ben Murdoch589d6972011-11-30 16:04:58 +0000256}
257
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100258
Steve Blocka7e24c12009-10-30 11:49:00 +0000259} } // namespace v8::internal
260
261#endif // V8_LIST_INL_H_