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 16 matching lines...) Expand all Loading... |
27 | 27 |
28 #ifndef V8_LIST_INL_H_ | 28 #ifndef V8_LIST_INL_H_ |
29 #define V8_LIST_INL_H_ | 29 #define V8_LIST_INL_H_ |
30 | 30 |
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 List<T, P>::List(const List<T, P>& other) { | |
38 ASSERT(other.capacity() >= 0); | |
39 capacity_ = other.capacity(); | |
40 length_ = other.length(); | |
41 if (capacity_ > 0) { | |
42 data_ = NewData(capacity_); | |
43 int copy_size = length_ * sizeof(T); | |
44 const int kMinMemCpySize = 64; | |
45 if (copy_size < kMinMemCpySize) { | |
46 for (int i = 0; i < length_; i++) data_[i] = other.data_[i]; | |
47 } else { | |
48 memcpy(data_, other.data_, copy_size); | |
49 } | |
50 } else { | |
51 data_ = NULL; | |
52 } | |
53 } | |
54 | |
55 | |
56 template<typename T, class P> | |
57 void List<T, P>::Add(const T& element) { | 37 void List<T, P>::Add(const T& element) { |
58 if (length_ < capacity_) { | 38 if (length_ < capacity_) { |
59 data_[length_++] = element; | 39 data_[length_++] = element; |
60 } else { | 40 } else { |
61 List<T, P>::ResizeAdd(element); | 41 List<T, P>::ResizeAdd(element); |
62 } | 42 } |
63 } | 43 } |
64 | 44 |
65 | 45 |
| 46 template<typename T, class P> |
| 47 void List<T, P>::AddAll(const List<T, P>& other) { |
| 48 int result_length = length_ + other.length_; |
| 49 if (capacity_ < result_length) Resize(result_length); |
| 50 for (int i = 0; i < other.length_; i++) { |
| 51 data_[length_ + i] = other.data_[i]; |
| 52 } |
| 53 length_ = result_length; |
| 54 } |
| 55 |
| 56 |
66 // Use two layers of inlining so that the non-inlined function can | 57 // Use two layers of inlining so that the non-inlined function can |
67 // use the same implementation as the inlined version. | 58 // use the same implementation as the inlined version. |
68 template<typename T, class P> | 59 template<typename T, class P> |
69 void List<T, P>::ResizeAdd(const T& element) { | 60 void List<T, P>::ResizeAdd(const T& element) { |
70 ResizeAddInternal(element); | 61 ResizeAddInternal(element); |
71 } | 62 } |
72 | 63 |
73 | 64 |
74 template<typename T, class P> | 65 template<typename T, class P> |
75 void List<T, P>::ResizeAddInternal(const T& element) { | 66 void List<T, P>::ResizeAddInternal(const T& element) { |
76 ASSERT(length_ >= capacity_); | 67 ASSERT(length_ >= capacity_); |
77 // Grow the list capacity by 50%, but make sure to let it grow | 68 // Grow the list capacity by 50%, but make sure to let it grow |
78 // even when the capacity is zero (possible initial case). | 69 // even when the capacity is zero (possible initial case). |
79 int new_capacity = 1 + capacity_ + (capacity_ >> 1); | 70 int new_capacity = 1 + capacity_ + (capacity_ >> 1); |
| 71 // Since the element reference could be an element of the list, copy |
| 72 // it out of the old backing storage before resizing. |
| 73 T temp = element; |
| 74 Resize(new_capacity); |
| 75 data_[length_++] = temp; |
| 76 } |
| 77 |
| 78 |
| 79 template<typename T, class P> |
| 80 void List<T, P>::Resize(int new_capacity) { |
80 T* new_data = List<T, P>::NewData(new_capacity); | 81 T* new_data = List<T, P>::NewData(new_capacity); |
81 memcpy(new_data, data_, capacity_ * sizeof(T)); | 82 memcpy(new_data, data_, capacity_ * sizeof(T)); |
82 // Since the element reference could be an element of the list, | |
83 // assign it to the new backing store before deleting the old. | |
84 new_data[length_++] = element; | |
85 List<T, P>::DeleteData(data_); | 83 List<T, P>::DeleteData(data_); |
86 data_ = new_data; | 84 data_ = new_data; |
87 capacity_ = new_capacity; | 85 capacity_ = new_capacity; |
88 } | 86 } |
89 | 87 |
90 | 88 |
91 template<typename T, class P> | 89 template<typename T, class P> |
92 Vector<T> List<T, P>::AddBlock(T value, int count) { | 90 Vector<T> List<T, P>::AddBlock(T value, int count) { |
93 int start = length_; | 91 int start = length_; |
94 for (int i = 0; i < count; i++) Add(value); | 92 for (int i = 0; i < count; i++) Add(value); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 ASSERT(capacity >= 0); | 156 ASSERT(capacity >= 0); |
159 data_ = (capacity > 0) ? NewData(capacity) : NULL; | 157 data_ = (capacity > 0) ? NewData(capacity) : NULL; |
160 capacity_ = capacity; | 158 capacity_ = capacity; |
161 length_ = 0; | 159 length_ = 0; |
162 } | 160 } |
163 | 161 |
164 | 162 |
165 } } // namespace v8::internal | 163 } } // namespace v8::internal |
166 | 164 |
167 #endif // V8_LIST_INL_H_ | 165 #endif // V8_LIST_INL_H_ |
OLD | NEW |