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
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.
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.
28 #ifndef V8_LIST_INL_H_
29 #define V8_LIST_INL_H_
38 template<typename T, class P>
39 void List<T, P>::Add(const T& element, P alloc) {
40 if (length_ < capacity_) {
41 data_[length_++] = element;
43 List<T, P>::ResizeAdd(element, alloc);
48 template<typename T, class P>
49 void List<T, P>::AddAll(const List<T, P>& other, P alloc) {
50 AddAll(other.ToVector(), alloc);
54 template<typename T, class P>
55 void List<T, P>::AddAll(const Vector<T>& other, P alloc) {
56 int result_length = length_ + other.length();
57 if (capacity_ < result_length) Resize(result_length, alloc);
58 for (int i = 0; i < other.length(); i++) {
59 data_[length_ + i] = other.at(i);
61 length_ = result_length;
65 // Use two layers of inlining so that the non-inlined function can
66 // use the same implementation as the inlined version.
67 template<typename T, class P>
68 void List<T, P>::ResizeAdd(const T& element, P alloc) {
69 ResizeAddInternal(element, alloc);
73 template<typename T, class P>
74 void List<T, P>::ResizeAddInternal(const T& element, P alloc) {
75 ASSERT(length_ >= capacity_);
76 // Grow the list capacity by 100%, but make sure to let it grow
77 // even when the capacity is zero (possible initial case).
78 int new_capacity = 1 + 2 * capacity_;
79 // Since the element reference could be an element of the list, copy
80 // it out of the old backing storage before resizing.
82 Resize(new_capacity, alloc);
83 data_[length_++] = temp;
87 template<typename T, class P>
88 void List<T, P>::Resize(int new_capacity, P alloc) {
89 ASSERT_LE(length_, new_capacity);
90 T* new_data = NewData(new_capacity, alloc);
91 OS::MemCopy(new_data, data_, length_ * sizeof(T));
92 List<T, P>::DeleteData(data_);
94 capacity_ = new_capacity;
98 template<typename T, class P>
99 Vector<T> List<T, P>::AddBlock(T value, int count, P alloc) {
101 for (int i = 0; i < count; i++) Add(value, alloc);
102 return Vector<T>(&data_[start], count);
106 template<typename T, class P>
107 void List<T, P>::InsertAt(int index, const T& elm, P alloc) {
108 ASSERT(index >= 0 && index <= length_);
110 for (int i = length_ - 1; i > index; --i) {
111 data_[i] = data_[i - 1];
117 template<typename T, class P>
118 T List<T, P>::Remove(int i) {
121 while (i < length_) {
122 data_[i] = data_[i + 1];
129 template<typename T, class P>
130 bool List<T, P>::RemoveElement(const T& elm) {
131 for (int i = 0; i < length_; i++) {
132 if (data_[i] == elm) {
141 template<typename T, class P>
142 void List<T, P>::Allocate(int length, P allocator) {
144 Initialize(length, allocator);
149 template<typename T, class P>
150 void List<T, P>::Clear() {
152 // We don't call Initialize(0) since that requires passing a Zone,
153 // which we don't really need.
160 template<typename T, class P>
161 void List<T, P>::Rewind(int pos) {
166 template<typename T, class P>
167 void List<T, P>::Trim(P alloc) {
168 if (length_ < capacity_ / 4) {
169 Resize(capacity_ / 2, alloc);
174 template<typename T, class P>
175 void List<T, P>::Iterate(void (*callback)(T* x)) {
176 for (int i = 0; i < length_; i++) callback(&data_[i]);
180 template<typename T, class P>
181 template<class Visitor>
182 void List<T, P>::Iterate(Visitor* visitor) {
183 for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]);
187 template<typename T, class P>
188 bool List<T, P>::Contains(const T& elm) const {
189 for (int i = 0; i < length_; i++) {
197 template<typename T, class P>
198 int List<T, P>::CountOccurrences(const T& elm, int start, int end) const {
200 for (int i = start; i <= end; i++) {
201 if (data_[i] == elm) ++result;
207 template<typename T, class P>
208 void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
209 ToVector().Sort(cmp);
211 for (int i = 1; i < length_; i++)
212 ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0);
217 template<typename T, class P>
218 void List<T, P>::Sort() {
223 template<typename T, class P>
224 void List<T, P>::Initialize(int capacity, P allocator) {
225 ASSERT(capacity >= 0);
226 data_ = (capacity > 0) ? NewData(capacity, allocator) : NULL;
227 capacity_ = capacity;
232 template <typename T, typename P>
233 int SortedListBSearch(const List<T>& list, P cmp) {
235 int high = list.length() - 1;
236 while (low <= high) {
237 int mid = (low + high) / 2;
238 T mid_elem = list[mid];
240 if (cmp(&mid_elem) > 0) {
244 if (cmp(&mid_elem) < 0) {
248 // Found the elememt.
258 explicit ElementCmp(T e) : elem_(e) {}
259 int operator()(const T* other) {
260 return PointerValueCompare(other, &elem_);
267 template <typename T>
268 int SortedListBSearch(const List<T>& list, T elem) {
269 return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem));
273 } } // namespace v8::internal
275 #endif // V8_LIST_INL_H_