blob: 8ef7514f4f61d5bad8b374d95f8aae8ab0cff296 [file] [log] [blame]
ager@chromium.org5ec48922009-05-05 07:25:34 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36
37template<typename T, class P>
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000038void List<T, P>::Add(const T& element) {
39 if (length_ < capacity_) {
40 data_[length_++] = element;
41 } else {
ager@chromium.org5ec48922009-05-05 07:25:34 +000042 List<T, P>::ResizeAdd(element);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044}
45
46
kasperl@chromium.org71affb52009-05-26 05:44:31 +000047template<typename T, class P>
48void List<T, P>::AddAll(const List<T, P>& other) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000049 AddAll(other.ToVector());
50}
51
52
53template<typename T, class P>
54void List<T, P>::AddAll(const Vector<T>& other) {
55 int result_length = length_ + other.length();
kasperl@chromium.org71affb52009-05-26 05:44:31 +000056 if (capacity_ < result_length) Resize(result_length);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000057 for (int i = 0; i < other.length(); i++) {
58 data_[length_ + i] = other.at(i);
kasperl@chromium.org71affb52009-05-26 05:44:31 +000059 }
60 length_ = result_length;
61}
62
63
ager@chromium.org5ec48922009-05-05 07:25:34 +000064// Use two layers of inlining so that the non-inlined function can
65// use the same implementation as the inlined version.
66template<typename T, class P>
67void List<T, P>::ResizeAdd(const T& element) {
68 ResizeAddInternal(element);
69}
70
71
72template<typename T, class P>
73void List<T, P>::ResizeAddInternal(const T& element) {
74 ASSERT(length_ >= capacity_);
75 // Grow the list capacity by 50%, but make sure to let it grow
76 // even when the capacity is zero (possible initial case).
77 int new_capacity = 1 + capacity_ + (capacity_ >> 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +000078 // 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);
82 data_[length_++] = temp;
83}
84
85
86template<typename T, class P>
87void List<T, P>::Resize(int new_capacity) {
ager@chromium.org5ec48922009-05-05 07:25:34 +000088 T* new_data = List<T, P>::NewData(new_capacity);
89 memcpy(new_data, data_, capacity_ * sizeof(T));
ager@chromium.org5ec48922009-05-05 07:25:34 +000090 List<T, P>::DeleteData(data_);
91 data_ = new_data;
92 capacity_ = new_capacity;
93}
94
95
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096template<typename T, class P>
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000097Vector<T> List<T, P>::AddBlock(T value, int count) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098 int start = length_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000099 for (int i = 0; i < count; i++) Add(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 return Vector<T>(&data_[start], count);
101}
102
103
104template<typename T, class P>
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000105void List<T, P>::InsertAt(int index, const T& elm) {
106 ASSERT(index >= 0 && index <= length_);
107 Add(elm);
108 for (int i = length_ - 1; i > index; --i) {
109 data_[i] = data_[i - 1];
110 }
111 data_[index] = elm;
112}
113
114
115template<typename T, class P>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116T List<T, P>::Remove(int i) {
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
127template<typename T, class P>
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000128bool 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
139template<typename T, class P>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140void List<T, P>::Clear() {
141 DeleteData(data_);
142 Initialize(0);
143}
144
145
146template<typename T, class P>
147void List<T, P>::Rewind(int pos) {
148 length_ = pos;
149}
150
151
152template<typename T, class P>
153void List<T, P>::Iterate(void (*callback)(T* x)) {
154 for (int i = 0; i < length_; i++) callback(&data_[i]);
155}
156
157
158template<typename T, class P>
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000159template<class Visitor>
160void List<T, P>::Iterate(Visitor* visitor) {
161 for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]);
162}
163
164
165template<typename T, class P>
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000166bool List<T, P>::Contains(const T& elm) const {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000167 for (int i = 0; i < length_; i++) {
168 if (data_[i] == elm)
169 return true;
170 }
171 return false;
172}
173
174
175template<typename T, class P>
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000176int List<T, P>::CountOccurrences(const T& elm, int start, int end) const {
177 int result = 0;
178 for (int i = start; i <= end; i++) {
179 if (data_[i] == elm) ++result;
180 }
181 return result;
182}
183
184
185template<typename T, class P>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000187 ToVector().Sort(cmp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188#ifdef DEBUG
189 for (int i = 1; i < length_; i++)
190 ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0);
191#endif
192}
193
194
195template<typename T, class P>
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000196void List<T, P>::Sort() {
197 Sort(PointerValueCompare<T>);
198}
199
200
201template<typename T, class P>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202void List<T, P>::Initialize(int capacity) {
203 ASSERT(capacity >= 0);
204 data_ = (capacity > 0) ? NewData(capacity) : NULL;
205 capacity_ = capacity;
206 length_ = 0;
207}
208
209
210} } // namespace v8::internal
211
212#endif // V8_LIST_INL_H_