Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_LIST_INL_H_ |
| 6 | #define V8_LIST_INL_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/list.h" |
| 9 | |
| 10 | #include "src/base/platform/platform.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
| 15 | |
| 16 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17 | void List<T, P>::Add(const T& element, P alloc) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18 | if (length_ < capacity_) { |
| 19 | data_[length_++] = element; |
| 20 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 21 | List<T, P>::ResizeAdd(element, alloc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
| 25 | |
| 26 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | void List<T, P>::AddAll(const List<T, P>& other, P alloc) { |
| 28 | AddAll(other.ToVector(), alloc); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | |
| 32 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | void List<T, P>::AddAll(const Vector<T>& other, P alloc) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 34 | int result_length = length_ + other.length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 35 | if (capacity_ < result_length) Resize(result_length, alloc); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 36 | for (int i = 0; i < other.length(); i++) { |
| 37 | data_[length_ + i] = other.at(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | } |
| 39 | length_ = result_length; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | // Use two layers of inlining so that the non-inlined function can |
| 44 | // use the same implementation as the inlined version. |
| 45 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 46 | void List<T, P>::ResizeAdd(const T& element, P alloc) { |
| 47 | ResizeAddInternal(element, alloc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| 51 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 52 | void List<T, P>::ResizeAddInternal(const T& element, P alloc) { |
| 53 | DCHECK(length_ >= capacity_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 54 | // Grow the list capacity by 100%, but make sure to let it grow |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | // even when the capacity is zero (possible initial case). |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 56 | int new_capacity = 1 + 2 * capacity_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | // Since the element reference could be an element of the list, copy |
| 58 | // it out of the old backing storage before resizing. |
| 59 | T temp = element; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | Resize(new_capacity, alloc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | data_[length_++] = temp; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | void List<T, P>::Resize(int new_capacity, P alloc) { |
| 67 | DCHECK_LE(length_, new_capacity); |
| 68 | T* new_data = NewData(new_capacity, alloc); |
| 69 | MemCopy(new_data, data_, length_ * sizeof(T)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | List<T, P>::DeleteData(data_); |
| 71 | data_ = new_data; |
| 72 | capacity_ = new_capacity; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | Vector<T> List<T, P>::AddBlock(T value, int count, P alloc) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | int start = length_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | for (int i = 0; i < count; i++) Add(value, alloc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | return Vector<T>(&data_[start], count); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | void List<T, P>::Set(int index, const T& elm) { |
| 86 | DCHECK(index >= 0 && index <= length_); |
| 87 | data_[index] = elm; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | template<typename T, class P> |
| 92 | void List<T, P>::InsertAt(int index, const T& elm, P alloc) { |
| 93 | DCHECK(index >= 0 && index <= length_); |
| 94 | Add(elm, alloc); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 95 | for (int i = length_ - 1; i > index; --i) { |
| 96 | data_[i] = data_[i - 1]; |
| 97 | } |
| 98 | data_[index] = elm; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | template<typename T, class P> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | T List<T, P>::Remove(int i) { |
| 104 | T element = at(i); |
| 105 | length_--; |
| 106 | while (i < length_) { |
| 107 | data_[i] = data_[i + 1]; |
| 108 | i++; |
| 109 | } |
| 110 | return element; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | template<typename T, class P> |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 115 | bool List<T, P>::RemoveElement(const T& elm) { |
| 116 | for (int i = 0; i < length_; i++) { |
| 117 | if (data_[i] == elm) { |
| 118 | Remove(i); |
| 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 127 | void List<T, P>::Allocate(int length, P allocator) { |
| 128 | DeleteData(data_); |
| 129 | Initialize(length, allocator); |
| 130 | length_ = length; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | template<typename T, class P> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | void List<T, P>::Clear() { |
| 136 | DeleteData(data_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | // We don't call Initialize(0) since that requires passing a Zone, |
| 138 | // which we don't really need. |
| 139 | data_ = NULL; |
| 140 | capacity_ = 0; |
| 141 | length_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | template<typename T, class P> |
| 146 | void List<T, P>::Rewind(int pos) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 147 | DCHECK(0 <= pos && pos <= length_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | length_ = pos; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | void List<T, P>::Trim(P alloc) { |
| 154 | if (length_ < capacity_ / 4) { |
| 155 | Resize(capacity_ / 2, alloc); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | template<typename T, class P> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | void List<T, P>::Iterate(void (*callback)(T* x)) { |
| 162 | for (int i = 0; i < length_; i++) callback(&data_[i]); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | template<typename T, class P> |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 167 | template<class Visitor> |
| 168 | void List<T, P>::Iterate(Visitor* visitor) { |
| 169 | for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | template<typename T, class P> |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 174 | bool List<T, P>::Contains(const T& elm) const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | for (int i = 0; i < length_; i++) { |
| 176 | if (data_[i] == elm) |
| 177 | return true; |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | template<typename T, class P> |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 184 | int List<T, P>::CountOccurrences(const T& elm, int start, int end) const { |
| 185 | int result = 0; |
| 186 | for (int i = start; i <= end; i++) { |
| 187 | if (data_[i] == elm) ++result; |
| 188 | } |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | template<typename T, class P> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) { |
| 195 | ToVector().Sort(cmp); |
| 196 | #ifdef DEBUG |
| 197 | for (int i = 1; i < length_; i++) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 198 | DCHECK(cmp(&data_[i - 1], &data_[i]) <= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 199 | #endif |
| 200 | } |
| 201 | |
| 202 | |
| 203 | template<typename T, class P> |
| 204 | void List<T, P>::Sort() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 205 | ToVector().Sort(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
| 209 | template<typename T, class P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | void List<T, P>::Initialize(int capacity, P allocator) { |
| 211 | DCHECK(capacity >= 0); |
| 212 | data_ = (capacity > 0) ? NewData(capacity, allocator) : NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 213 | capacity_ = capacity; |
| 214 | length_ = 0; |
| 215 | } |
| 216 | |
| 217 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 218 | template <typename T, typename P> |
| 219 | int SortedListBSearch(const List<T>& list, P cmp) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 220 | int low = 0; |
| 221 | int high = list.length() - 1; |
| 222 | while (low <= high) { |
| 223 | int mid = (low + high) / 2; |
| 224 | T mid_elem = list[mid]; |
| 225 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 226 | if (cmp(&mid_elem) > 0) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 227 | high = mid - 1; |
| 228 | continue; |
| 229 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 230 | if (cmp(&mid_elem) < 0) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 231 | low = mid + 1; |
| 232 | continue; |
| 233 | } |
| 234 | // Found the elememt. |
| 235 | return mid; |
| 236 | } |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 241 | template<typename T> |
| 242 | class ElementCmp { |
| 243 | public: |
| 244 | explicit ElementCmp(T e) : elem_(e) {} |
| 245 | int operator()(const T* other) { |
| 246 | return PointerValueCompare(other, &elem_); |
| 247 | } |
| 248 | private: |
| 249 | T elem_; |
| 250 | }; |
| 251 | |
| 252 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 253 | template <typename T> |
| 254 | int SortedListBSearch(const List<T>& list, T elem) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 255 | return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem)); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 258 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | } } // namespace v8::internal |
| 260 | |
| 261 | #endif // V8_LIST_INL_H_ |