1 // Copyright 2006-2008 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_
33 namespace v8 { namespace internal {
36 template<typename T, class P>
37 void List<T, P>::Add(const T& element) {
38 if (length_ < capacity_) {
39 data_[length_++] = element;
41 // Grow the list capacity by 50%, but make sure to let it grow
42 // even when the capacity is zero (possible initial case).
43 int new_capacity = 1 + capacity_ + (capacity_ >> 1);
44 T* new_data = NewData(new_capacity);
45 memcpy(new_data, data_, capacity_ * sizeof(T));
46 // Since the element reference could be an element of the list,
47 // assign it to the new backing store before deleting the old.
48 new_data[length_++] = element;
51 capacity_ = new_capacity;
56 template<typename T, class P>
57 Vector<T> List<T, P>::AddBlock(T value, int count) {
59 for (int i = 0; i < count; i++) Add(value);
60 return Vector<T>(&data_[start], count);
64 template<typename T, class P>
65 T List<T, P>::Remove(int i) {
69 data_[i] = data_[i + 1];
76 template<typename T, class P>
77 void List<T, P>::Clear() {
83 template<typename T, class P>
84 void List<T, P>::Rewind(int pos) {
89 template<typename T, class P>
90 void List<T, P>::Iterate(void (*callback)(T* x)) {
91 for (int i = 0; i < length_; i++) callback(&data_[i]);
95 template<typename T, class P>
96 bool List<T, P>::Contains(const T& elm) {
97 for (int i = 0; i < length_; i++) {
105 template<typename T, class P>
106 void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
107 ToVector().Sort(cmp);
109 for (int i = 1; i < length_; i++)
110 ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0);
115 template<typename T, class P>
116 void List<T, P>::Sort() {
117 Sort(PointerValueCompare<T>);
121 template<typename T, class P>
122 void List<T, P>::Initialize(int capacity) {
123 ASSERT(capacity >= 0);
124 data_ = (capacity > 0) ? NewData(capacity) : NULL;
125 capacity_ = capacity;
130 } } // namespace v8::internal
132 #endif // V8_LIST_INL_H_