ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame^] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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 | |
| 33 | namespace v8 { namespace internal { |
| 34 | |
| 35 | |
| 36 | template<typename T, class P> |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 37 | void List<T, P>::Add(const T& element) { |
| 38 | if (length_ < capacity_) { |
| 39 | data_[length_++] = element; |
| 40 | } else { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame^] | 41 | List<T, P>::ResizeAdd(element); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame^] | 46 | // Use two layers of inlining so that the non-inlined function can |
| 47 | // use the same implementation as the inlined version. |
| 48 | template<typename T, class P> |
| 49 | void List<T, P>::ResizeAdd(const T& element) { |
| 50 | ResizeAddInternal(element); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | template<typename T, class P> |
| 55 | void List<T, P>::ResizeAddInternal(const T& element) { |
| 56 | ASSERT(length_ >= capacity_); |
| 57 | // Grow the list capacity by 50%, but make sure to let it grow |
| 58 | // even when the capacity is zero (possible initial case). |
| 59 | int new_capacity = 1 + capacity_ + (capacity_ >> 1); |
| 60 | T* new_data = List<T, P>::NewData(new_capacity); |
| 61 | memcpy(new_data, data_, capacity_ * sizeof(T)); |
| 62 | // Since the element reference could be an element of the list, |
| 63 | // assign it to the new backing store before deleting the old. |
| 64 | new_data[length_++] = element; |
| 65 | List<T, P>::DeleteData(data_); |
| 66 | data_ = new_data; |
| 67 | capacity_ = new_capacity; |
| 68 | } |
| 69 | |
| 70 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | template<typename T, class P> |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 72 | Vector<T> List<T, P>::AddBlock(T value, int count) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 73 | int start = length_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 74 | for (int i = 0; i < count; i++) Add(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 75 | return Vector<T>(&data_[start], count); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | template<typename T, class P> |
| 80 | T List<T, P>::Remove(int i) { |
| 81 | T element = at(i); |
| 82 | length_--; |
| 83 | while (i < length_) { |
| 84 | data_[i] = data_[i + 1]; |
| 85 | i++; |
| 86 | } |
| 87 | return element; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | template<typename T, class P> |
| 92 | void List<T, P>::Clear() { |
| 93 | DeleteData(data_); |
| 94 | Initialize(0); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | template<typename T, class P> |
| 99 | void List<T, P>::Rewind(int pos) { |
| 100 | length_ = pos; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | template<typename T, class P> |
| 105 | void List<T, P>::Iterate(void (*callback)(T* x)) { |
| 106 | for (int i = 0; i < length_; i++) callback(&data_[i]); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | template<typename T, class P> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 111 | bool List<T, P>::Contains(const T& elm) { |
| 112 | for (int i = 0; i < length_; i++) { |
| 113 | if (data_[i] == elm) |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | template<typename T, class P> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 122 | ToVector().Sort(cmp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | #ifdef DEBUG |
| 124 | for (int i = 1; i < length_; i++) |
| 125 | ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0); |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | |
| 130 | template<typename T, class P> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 131 | void List<T, P>::Sort() { |
| 132 | Sort(PointerValueCompare<T>); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | template<typename T, class P> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | void List<T, P>::Initialize(int capacity) { |
| 138 | ASSERT(capacity >= 0); |
| 139 | data_ = (capacity > 0) ? NewData(capacity) : NULL; |
| 140 | capacity_ = capacity; |
| 141 | length_ = 0; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | } } // namespace v8::internal |
| 146 | |
| 147 | #endif // V8_LIST_INL_H_ |