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