ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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> |
| 37 | T& List<T, P>::Add(const T& element) { |
| 38 | if (length_ >= capacity_) { |
| 39 | // Grow the list capacity by 50%, but make sure to let it grow |
| 40 | // even when the capacity is zero (possible initial case). |
| 41 | int new_capacity = 1 + capacity_ + (capacity_ >> 1); |
| 42 | T* new_data = NewData(new_capacity); |
| 43 | memcpy(new_data, data_, capacity_ * sizeof(T)); |
| 44 | DeleteData(data_); |
| 45 | data_ = new_data; |
| 46 | capacity_ = new_capacity; |
| 47 | } |
| 48 | return data_[length_++] = element; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | template<typename T, class P> |
| 53 | Vector<T> List<T, P>::AddBlock(const T& element, int count) { |
| 54 | int start = length_; |
| 55 | for (int i = 0; i < count; i++) |
| 56 | Add(element); |
| 57 | return Vector<T>(&data_[start], count); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | template<typename T, class P> |
| 62 | T List<T, P>::Remove(int i) { |
| 63 | T element = at(i); |
| 64 | length_--; |
| 65 | while (i < length_) { |
| 66 | data_[i] = data_[i + 1]; |
| 67 | i++; |
| 68 | } |
| 69 | return element; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | template<typename T, class P> |
| 74 | void List<T, P>::Clear() { |
| 75 | DeleteData(data_); |
| 76 | Initialize(0); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | template<typename T, class P> |
| 81 | void List<T, P>::Rewind(int pos) { |
| 82 | length_ = pos; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | template<typename T, class P> |
| 87 | void List<T, P>::Iterate(void (*callback)(T* x)) { |
| 88 | for (int i = 0; i < length_; i++) callback(&data_[i]); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | template<typename T, class P> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 93 | bool List<T, P>::Contains(const T& elm) { |
| 94 | for (int i = 0; i < length_; i++) { |
| 95 | if (data_[i] == elm) |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | template<typename T, class P> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | 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^] | 104 | ToVector().Sort(cmp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | #ifdef DEBUG |
| 106 | for (int i = 1; i < length_; i++) |
| 107 | ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0); |
| 108 | #endif |
| 109 | } |
| 110 | |
| 111 | |
| 112 | template<typename T, class P> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 113 | void List<T, P>::Sort() { |
| 114 | Sort(PointerValueCompare<T>); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | template<typename T, class P> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | void List<T, P>::Initialize(int capacity) { |
| 120 | ASSERT(capacity >= 0); |
| 121 | data_ = (capacity > 0) ? NewData(capacity) : NULL; |
| 122 | capacity_ = capacity; |
| 123 | length_ = 0; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | } } // namespace v8::internal |
| 128 | |
| 129 | #endif // V8_LIST_INL_H_ |