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_
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;
42 List<T, P>::ResizeAdd(element, alloc);
47 template<typename T, class P>
48 void List<T, P>::AddAll(const List<T, P>& other, P alloc) {
49 AddAll(other.ToVector(), alloc);
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);
60 length_ = result_length;
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);
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.
81 Resize(new_capacity, alloc);
82 data_[length_++] = temp;
86 template<typename T, class P>
87 void List<T, P>::Resize(int new_capacity, P alloc) {
88 ASSERT_LE(length_, new_capacity);
89 T* new_data = NewData(new_capacity, alloc);
90 memcpy(new_data, data_, length_ * sizeof(T));
91 List<T, P>::DeleteData(data_);
93 capacity_ = new_capacity;
97 template<typename T, class P>
98 Vector<T> List<T, P>::AddBlock(T value, int count, P alloc) {
100 for (int i = 0; i < count; i++) Add(value, alloc);
101 return Vector<T>(&data_[start], count);
105 template<typename T, class P>
106 void List<T, P>::InsertAt(int index, const T& elm, P alloc) {
107 ASSERT(index >= 0 && index <= length_);
109 for (int i = length_ - 1; i > index; --i) {
110 data_[i] = data_[i - 1];
116 template<typename T, class P>
117 T List<T, P>::Remove(int i) {
120 while (i < length_) {
121 data_[i] = data_[i + 1];
128 template<typename T, class P>
129 bool List<T, P>::RemoveElement(const T& elm) {
130 for (int i = 0; i < length_; i++) {
131 if (data_[i] == elm) {
140 template<typename T, class P>
141 void List<T, P>::Allocate(int length, P allocator) {
143 Initialize(length, allocator);
148 template<typename T, class P>
149 void List<T, P>::Clear() {
151 // We don't call Initialize(0) since that requires passing a Zone,
152 // which we don't really need.
159 template<typename T, class P>
160 void List<T, P>::Rewind(int pos) {
165 template<typename T, class P>
166 void List<T, P>::Trim(P alloc) {
167 if (length_ < capacity_ / 4) {
168 Resize(capacity_ / 2, alloc);
173 template<typename T, class P>
174 void List<T, P>::Iterate(void (*callback)(T* x)) {
175 for (int i = 0; i < length_; i++) callback(&data_[i]);
179 template<typename T, class P>
180 template<class Visitor>
181 void List<T, P>::Iterate(Visitor* visitor) {
182 for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]);
186 template<typename T, class P>
187 bool List<T, P>::Contains(const T& elm) const {
188 for (int i = 0; i < length_; i++) {
196 template<typename T, class P>
197 int List<T, P>::CountOccurrences(const T& elm, int start, int end) const {
199 for (int i = start; i <= end; i++) {
200 if (data_[i] == elm) ++result;
206 template<typename T, class P>
207 void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
208 ToVector().Sort(cmp);
210 for (int i = 1; i < length_; i++)
211 ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0);
216 template<typename T, class P>
217 void List<T, P>::Sort() {
218 Sort(PointerValueCompare<T>);
222 template<typename T, class P>
223 void List<T, P>::Initialize(int capacity, P allocator) {
224 ASSERT(capacity >= 0);
225 data_ = (capacity > 0) ? NewData(capacity, allocator) : NULL;
226 capacity_ = capacity;
231 template <typename T, typename P>
232 int SortedListBSearch(const List<T>& list, P cmp) {
234 int high = list.length() - 1;
235 while (low <= high) {
236 int mid = (low + high) / 2;
237 T mid_elem = list[mid];
239 if (cmp(&mid_elem) > 0) {
243 if (cmp(&mid_elem) < 0) {
247 // Found the elememt.
257 explicit ElementCmp(T e) : elem_(e) {}
258 int operator()(const T* other) {
259 return PointerValueCompare(other, &elem_);
266 template <typename T>
267 int SortedListBSearch(const List<T>& list, T elem) {
268 return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem));
272 } } // namespace v8::internal
274 #endif // V8_LIST_INL_H_