OLD | NEW |
1 // Copyright 2006-2008 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 |
11 // with the distribution. | 11 // with the distribution. |
(...skipping 19 matching lines...) Expand all Loading... |
31 #include "list.h" | 31 #include "list.h" |
32 | 32 |
33 namespace v8 { namespace internal { | 33 namespace v8 { namespace internal { |
34 | 34 |
35 | 35 |
36 template<typename T, class P> | 36 template<typename T, class P> |
37 void List<T, P>::Add(const T& element) { | 37 void List<T, P>::Add(const T& element) { |
38 if (length_ < capacity_) { | 38 if (length_ < capacity_) { |
39 data_[length_++] = element; | 39 data_[length_++] = element; |
40 } else { | 40 } else { |
41 // Grow the list capacity by 50%, but make sure to let it grow | 41 List<T, P>::ResizeAdd(element); |
42 // even when the capacity is zero (possible initial case). | |
43 int new_capacity = 1 + capacity_ + (capacity_ >> 1); | |
44 T* new_data = NewData(new_capacity); | |
45 memcpy(new_data, data_, capacity_ * sizeof(T)); | |
46 // Since the element reference could be an element of the list, | |
47 // assign it to the new backing store before deleting the old. | |
48 new_data[length_++] = element; | |
49 DeleteData(data_); | |
50 data_ = new_data; | |
51 capacity_ = new_capacity; | |
52 } | 42 } |
53 } | 43 } |
54 | 44 |
55 | 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> |
| 49 void List<T, P>::ResizeAdd(const T& element) { |
| 50 ResizeAddInternal(element); |
| 51 } |
| 52 |
| 53 |
| 54 template<typename T, class P> |
| 55 void List<T, P>::ResizeAddInternal(const T& element) { |
| 56 ASSERT(length_ >= capacity_); |
| 57 // Grow the list capacity by 50%, but make sure to let it grow |
| 58 // even when the capacity is zero (possible initial case). |
| 59 int new_capacity = 1 + capacity_ + (capacity_ >> 1); |
| 60 T* new_data = List<T, P>::NewData(new_capacity); |
| 61 memcpy(new_data, data_, capacity_ * sizeof(T)); |
| 62 // Since the element reference could be an element of the list, |
| 63 // assign it to the new backing store before deleting the old. |
| 64 new_data[length_++] = element; |
| 65 List<T, P>::DeleteData(data_); |
| 66 data_ = new_data; |
| 67 capacity_ = new_capacity; |
| 68 } |
| 69 |
| 70 |
56 template<typename T, class P> | 71 template<typename T, class P> |
57 Vector<T> List<T, P>::AddBlock(T value, int count) { | 72 Vector<T> List<T, P>::AddBlock(T value, int count) { |
58 int start = length_; | 73 int start = length_; |
59 for (int i = 0; i < count; i++) Add(value); | 74 for (int i = 0; i < count; i++) Add(value); |
60 return Vector<T>(&data_[start], count); | 75 return Vector<T>(&data_[start], count); |
61 } | 76 } |
62 | 77 |
63 | 78 |
64 template<typename T, class P> | 79 template<typename T, class P> |
65 T List<T, P>::Remove(int i) { | 80 T List<T, P>::Remove(int i) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 ASSERT(capacity >= 0); | 138 ASSERT(capacity >= 0); |
124 data_ = (capacity > 0) ? NewData(capacity) : NULL; | 139 data_ = (capacity > 0) ? NewData(capacity) : NULL; |
125 capacity_ = capacity; | 140 capacity_ = capacity; |
126 length_ = 0; | 141 length_ = 0; |
127 } | 142 } |
128 | 143 |
129 | 144 |
130 } } // namespace v8::internal | 145 } } // namespace v8::internal |
131 | 146 |
132 #endif // V8_LIST_INL_H_ | 147 #endif // V8_LIST_INL_H_ |
OLD | NEW |