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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 38 | void List<T, P>::Add(const T& element, P alloc) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 39 | if (length_ < capacity_) { |
| 40 | data_[length_++] = element; |
| 41 | } else { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 42 | List<T, P>::ResizeAdd(element, alloc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 47 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 48 | void List<T, P>::AddAll(const List<T, P>& other, P alloc) { |
| 49 | AddAll(other.ToVector(), alloc); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
| 53 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 54 | void List<T, P>::AddAll(const Vector<T>& other, P alloc) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 55 | int result_length = length_ + other.length(); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 56 | if (capacity_ < result_length) Resize(result_length, alloc); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 57 | for (int i = 0; i < other.length(); i++) { |
| 58 | data_[length_ + i] = other.at(i); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 59 | } |
| 60 | length_ = result_length; |
| 61 | } |
| 62 | |
| 63 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 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> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 67 | void List<T, P>::ResizeAdd(const T& element, P alloc) { |
| 68 | ResizeAddInternal(element, alloc); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 73 | void List<T, P>::ResizeAddInternal(const T& element, P alloc) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 74 | ASSERT(length_ >= capacity_); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 75 | // Grow the list capacity by 100%, but make sure to let it grow |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 76 | // even when the capacity is zero (possible initial case). |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 77 | int new_capacity = 1 + 2 * capacity_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 78 | // 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; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 81 | Resize(new_capacity, alloc); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 82 | data_[length_++] = temp; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 87 | void List<T, P>::Resize(int new_capacity, P alloc) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 88 | ASSERT_LE(length_, new_capacity); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 89 | T* new_data = NewData(new_capacity, alloc); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 90 | memcpy(new_data, data_, length_ * sizeof(T)); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 91 | List<T, P>::DeleteData(data_); |
| 92 | data_ = new_data; |
| 93 | capacity_ = new_capacity; |
| 94 | } |
| 95 | |
| 96 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 98 | Vector<T> List<T, P>::AddBlock(T value, int count, P alloc) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | int start = length_; |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 100 | for (int i = 0; i < count; i++) Add(value, alloc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 101 | return Vector<T>(&data_[start], count); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 106 | void List<T, P>::InsertAt(int index, const T& elm, P alloc) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 107 | ASSERT(index >= 0 && index <= length_); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 108 | Add(elm, alloc); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 109 | for (int i = length_ - 1; i > index; --i) { |
| 110 | data_[i] = data_[i - 1]; |
| 111 | } |
| 112 | data_[index] = elm; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | template<typename T, class P> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | T List<T, P>::Remove(int i) { |
| 118 | T element = at(i); |
| 119 | length_--; |
| 120 | while (i < length_) { |
| 121 | data_[i] = data_[i + 1]; |
| 122 | i++; |
| 123 | } |
| 124 | return element; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | template<typename T, class P> |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 129 | bool List<T, P>::RemoveElement(const T& elm) { |
| 130 | for (int i = 0; i < length_; i++) { |
| 131 | if (data_[i] == elm) { |
| 132 | Remove(i); |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 141 | void List<T, P>::Allocate(int length, P allocator) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 142 | DeleteData(data_); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 143 | Initialize(length, allocator); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 144 | length_ = length; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | template<typename T, class P> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | void List<T, P>::Clear() { |
| 150 | DeleteData(data_); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 151 | // We don't call Initialize(0) since that requires passing a Zone, |
| 152 | // which we don't really need. |
| 153 | data_ = NULL; |
| 154 | capacity_ = 0; |
| 155 | length_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | template<typename T, class P> |
| 160 | void List<T, P>::Rewind(int pos) { |
| 161 | length_ = pos; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | template<typename T, class P> |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 166 | void List<T, P>::Trim(P alloc) { |
| 167 | if (length_ < capacity_ / 4) { |
| 168 | Resize(capacity_ / 2, alloc); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | template<typename T, class P> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | void List<T, P>::Iterate(void (*callback)(T* x)) { |
| 175 | for (int i = 0; i < length_; i++) callback(&data_[i]); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | template<typename T, class P> |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 180 | template<class Visitor> |
| 181 | void List<T, P>::Iterate(Visitor* visitor) { |
| 182 | for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | template<typename T, class P> |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 187 | bool List<T, P>::Contains(const T& elm) const { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 188 | for (int i = 0; i < length_; i++) { |
| 189 | if (data_[i] == elm) |
| 190 | return true; |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | template<typename T, class P> |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 197 | int List<T, P>::CountOccurrences(const T& elm, int start, int end) const { |
| 198 | int result = 0; |
| 199 | for (int i = start; i <= end; i++) { |
| 200 | if (data_[i] == elm) ++result; |
| 201 | } |
| 202 | return result; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | template<typename T, class P> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | 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] | 208 | ToVector().Sort(cmp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 209 | #ifdef DEBUG |
| 210 | for (int i = 1; i < length_; i++) |
| 211 | ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0); |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | |
| 216 | template<typename T, class P> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 217 | void List<T, P>::Sort() { |
| 218 | Sort(PointerValueCompare<T>); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | template<typename T, class P> |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 223 | void List<T, P>::Initialize(int capacity, P allocator) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | ASSERT(capacity >= 0); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 225 | data_ = (capacity > 0) ? NewData(capacity, allocator) : NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | capacity_ = capacity; |
| 227 | length_ = 0; |
| 228 | } |
| 229 | |
| 230 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 231 | template <typename T, typename P> |
| 232 | int SortedListBSearch(const List<T>& list, P cmp) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 233 | int low = 0; |
| 234 | int high = list.length() - 1; |
| 235 | while (low <= high) { |
| 236 | int mid = (low + high) / 2; |
| 237 | T mid_elem = list[mid]; |
| 238 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 239 | if (cmp(&mid_elem) > 0) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 240 | high = mid - 1; |
| 241 | continue; |
| 242 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 243 | if (cmp(&mid_elem) < 0) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 244 | low = mid + 1; |
| 245 | continue; |
| 246 | } |
| 247 | // Found the elememt. |
| 248 | return mid; |
| 249 | } |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 254 | template<typename T> |
| 255 | class ElementCmp { |
| 256 | public: |
| 257 | explicit ElementCmp(T e) : elem_(e) {} |
| 258 | int operator()(const T* other) { |
| 259 | return PointerValueCompare(other, &elem_); |
| 260 | } |
| 261 | private: |
| 262 | T elem_; |
| 263 | }; |
| 264 | |
| 265 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 266 | template <typename T> |
| 267 | int SortedListBSearch(const List<T>& list, T elem) { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 268 | return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem)); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 269 | } |
| 270 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 271 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | } } // namespace v8::internal |
| 273 | |
| 274 | #endif // V8_LIST_INL_H_ |