| Index: src/list-inl.h
|
| ===================================================================
|
| --- src/list-inl.h (revision 2025)
|
| +++ src/list-inl.h (working copy)
|
| @@ -34,32 +34,23 @@
|
|
|
|
|
| template<typename T, class P>
|
| -List<T, P>::List(const List<T, P>& other) {
|
| - ASSERT(other.capacity() >= 0);
|
| - capacity_ = other.capacity();
|
| - length_ = other.length();
|
| - if (capacity_ > 0) {
|
| - data_ = NewData(capacity_);
|
| - int copy_size = length_ * sizeof(T);
|
| - const int kMinMemCpySize = 64;
|
| - if (copy_size < kMinMemCpySize) {
|
| - for (int i = 0; i < length_; i++) data_[i] = other.data_[i];
|
| - } else {
|
| - memcpy(data_, other.data_, copy_size);
|
| - }
|
| +void List<T, P>::Add(const T& element) {
|
| + if (length_ < capacity_) {
|
| + data_[length_++] = element;
|
| } else {
|
| - data_ = NULL;
|
| + List<T, P>::ResizeAdd(element);
|
| }
|
| }
|
|
|
|
|
| template<typename T, class P>
|
| -void List<T, P>::Add(const T& element) {
|
| - if (length_ < capacity_) {
|
| - data_[length_++] = element;
|
| - } else {
|
| - List<T, P>::ResizeAdd(element);
|
| +void List<T, P>::AddAll(const List<T, P>& other) {
|
| + int result_length = length_ + other.length_;
|
| + if (capacity_ < result_length) Resize(result_length);
|
| + for (int i = 0; i < other.length_; i++) {
|
| + data_[length_ + i] = other.data_[i];
|
| }
|
| + length_ = result_length;
|
| }
|
|
|
|
|
| @@ -77,11 +68,18 @@
|
| // Grow the list capacity by 50%, but make sure to let it grow
|
| // even when the capacity is zero (possible initial case).
|
| int new_capacity = 1 + capacity_ + (capacity_ >> 1);
|
| + // Since the element reference could be an element of the list, copy
|
| + // it out of the old backing storage before resizing.
|
| + T temp = element;
|
| + Resize(new_capacity);
|
| + data_[length_++] = temp;
|
| +}
|
| +
|
| +
|
| +template<typename T, class P>
|
| +void List<T, P>::Resize(int new_capacity) {
|
| T* new_data = List<T, P>::NewData(new_capacity);
|
| memcpy(new_data, data_, capacity_ * sizeof(T));
|
| - // Since the element reference could be an element of the list,
|
| - // assign it to the new backing store before deleting the old.
|
| - new_data[length_++] = element;
|
| List<T, P>::DeleteData(data_);
|
| data_ = new_data;
|
| capacity_ = new_capacity;
|
|
|