OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // Since the element reference could be an element of the list, copy | 78 // Since the element reference could be an element of the list, copy |
79 // it out of the old backing storage before resizing. | 79 // it out of the old backing storage before resizing. |
80 T temp = element; | 80 T temp = element; |
81 Resize(new_capacity, alloc); | 81 Resize(new_capacity, alloc); |
82 data_[length_++] = temp; | 82 data_[length_++] = temp; |
83 } | 83 } |
84 | 84 |
85 | 85 |
86 template<typename T, class P> | 86 template<typename T, class P> |
87 void List<T, P>::Resize(int new_capacity, P alloc) { | 87 void List<T, P>::Resize(int new_capacity, P alloc) { |
88 ASSERT_LE(length_, new_capacity); | |
89 T* new_data = NewData(new_capacity, alloc); | 88 T* new_data = NewData(new_capacity, alloc); |
90 memcpy(new_data, data_, length_ * sizeof(T)); | 89 memcpy(new_data, data_, capacity_ * sizeof(T)); |
91 List<T, P>::DeleteData(data_); | 90 List<T, P>::DeleteData(data_); |
92 data_ = new_data; | 91 data_ = new_data; |
93 capacity_ = new_capacity; | 92 capacity_ = new_capacity; |
94 } | 93 } |
95 | 94 |
96 | 95 |
97 template<typename T, class P> | 96 template<typename T, class P> |
98 Vector<T> List<T, P>::AddBlock(T value, int count, P alloc) { | 97 Vector<T> List<T, P>::AddBlock(T value, int count, P alloc) { |
99 int start = length_; | 98 int start = length_; |
100 for (int i = 0; i < count; i++) Add(value, alloc); | 99 for (int i = 0; i < count; i++) Add(value, alloc); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 } | 155 } |
157 | 156 |
158 | 157 |
159 template<typename T, class P> | 158 template<typename T, class P> |
160 void List<T, P>::Rewind(int pos) { | 159 void List<T, P>::Rewind(int pos) { |
161 length_ = pos; | 160 length_ = pos; |
162 } | 161 } |
163 | 162 |
164 | 163 |
165 template<typename T, class P> | 164 template<typename T, class P> |
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> | |
174 void List<T, P>::Iterate(void (*callback)(T* x)) { | 165 void List<T, P>::Iterate(void (*callback)(T* x)) { |
175 for (int i = 0; i < length_; i++) callback(&data_[i]); | 166 for (int i = 0; i < length_; i++) callback(&data_[i]); |
176 } | 167 } |
177 | 168 |
178 | 169 |
179 template<typename T, class P> | 170 template<typename T, class P> |
180 template<class Visitor> | 171 template<class Visitor> |
181 void List<T, P>::Iterate(Visitor* visitor) { | 172 void List<T, P>::Iterate(Visitor* visitor) { |
182 for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]); | 173 for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]); |
183 } | 174 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 256 |
266 template <typename T> | 257 template <typename T> |
267 int SortedListBSearch(const List<T>& list, T elem) { | 258 int SortedListBSearch(const List<T>& list, T elem) { |
268 return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem)); | 259 return SortedListBSearch<T, ElementCmp<T> > (list, ElementCmp<T>(elem)); |
269 } | 260 } |
270 | 261 |
271 | 262 |
272 } } // namespace v8::internal | 263 } } // namespace v8::internal |
273 | 264 |
274 #endif // V8_LIST_INL_H_ | 265 #endif // V8_LIST_INL_H_ |
OLD | NEW |