| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #ifndef TerminatedArrayBuilder_h | 4 #ifndef TerminatedArrayBuilder_h |
| 5 #define TerminatedArrayBuilder_h | 5 #define TerminatedArrayBuilder_h |
| 6 | 6 |
| 7 #include "wtf/OwnPtr.h" | 7 #include "wtf/OwnPtr.h" |
| 8 | 8 |
| 9 namespace WTF { | 9 namespace WTF { |
| 10 | 10 |
| 11 template<typename T, template <typename> class ArrayType = TerminatedArray> | 11 template <typename T, template <typename> class ArrayType = TerminatedArray> |
| 12 class TerminatedArrayBuilder { | 12 class TerminatedArrayBuilder { |
| 13 DISALLOW_NEW(); | 13 DISALLOW_NEW(); |
| 14 WTF_MAKE_NONCOPYABLE(TerminatedArrayBuilder); | 14 WTF_MAKE_NONCOPYABLE(TerminatedArrayBuilder); |
| 15 public: | 15 |
| 16 explicit TerminatedArrayBuilder(typename ArrayType<T>::Allocator::PassPtr ar
ray) | 16 public: |
| 17 : m_array(array) | 17 explicit TerminatedArrayBuilder( |
| 18 , m_count(0) | 18 typename ArrayType<T>::Allocator::PassPtr array) |
| 19 , m_capacity(0) | 19 : m_array(array), m_count(0), m_capacity(0) { |
| 20 { | 20 if (!m_array) |
| 21 if (!m_array) | 21 return; |
| 22 return; | 22 m_capacity = m_count = m_array->size(); |
| 23 m_capacity = m_count = m_array->size(); | 23 } |
| 24 |
| 25 void grow(size_t count) { |
| 26 ASSERT(count); |
| 27 if (!m_array) { |
| 28 ASSERT(!m_count); |
| 29 ASSERT(!m_capacity); |
| 30 m_capacity = count; |
| 31 m_array = ArrayType<T>::Allocator::create(m_capacity); |
| 32 return; |
| 24 } | 33 } |
| 34 m_capacity += count; |
| 35 m_array = ArrayType<T>::Allocator::resize(m_array.release(), m_capacity); |
| 36 m_array->at(m_count - 1).setLastInArray(false); |
| 37 } |
| 25 | 38 |
| 26 void grow(size_t count) | 39 void append(const T& item) { |
| 27 { | 40 RELEASE_ASSERT(m_count < m_capacity); |
| 28 ASSERT(count); | 41 ASSERT(!item.isLastInArray()); |
| 29 if (!m_array) { | 42 m_array->at(m_count++) = item; |
| 30 ASSERT(!m_count); | 43 } |
| 31 ASSERT(!m_capacity); | 44 |
| 32 m_capacity = count; | 45 typename ArrayType<T>::Allocator::PassPtr release() { |
| 33 m_array = ArrayType<T>::Allocator::create(m_capacity); | 46 RELEASE_ASSERT(m_count == m_capacity); |
| 34 return; | 47 if (m_array) |
| 35 } | 48 m_array->at(m_count - 1).setLastInArray(true); |
| 36 m_capacity += count; | 49 assertValid(); |
| 37 m_array = ArrayType<T>::Allocator::resize(m_array.release(), m_capacity)
; | 50 return m_array.release(); |
| 38 m_array->at(m_count - 1).setLastInArray(false); | 51 } |
| 52 |
| 53 private: |
| 54 #if ENABLE(ASSERT) |
| 55 void assertValid() { |
| 56 for (size_t i = 0; i < m_count; ++i) { |
| 57 bool isLastInArray = (i + 1 == m_count); |
| 58 ASSERT(m_array->at(i).isLastInArray() == isLastInArray); |
| 39 } | 59 } |
| 40 | 60 } |
| 41 void append(const T& item) | |
| 42 { | |
| 43 RELEASE_ASSERT(m_count < m_capacity); | |
| 44 ASSERT(!item.isLastInArray()); | |
| 45 m_array->at(m_count++) = item; | |
| 46 } | |
| 47 | |
| 48 typename ArrayType<T>::Allocator::PassPtr release() | |
| 49 { | |
| 50 RELEASE_ASSERT(m_count == m_capacity); | |
| 51 if (m_array) | |
| 52 m_array->at(m_count - 1).setLastInArray(true); | |
| 53 assertValid(); | |
| 54 return m_array.release(); | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 #if ENABLE(ASSERT) | |
| 59 void assertValid() | |
| 60 { | |
| 61 for (size_t i = 0; i < m_count; ++i) { | |
| 62 bool isLastInArray = (i + 1 == m_count); | |
| 63 ASSERT(m_array->at(i).isLastInArray() == isLastInArray); | |
| 64 } | |
| 65 } | |
| 66 #else | 61 #else |
| 67 void assertValid() { } | 62 void assertValid() {} |
| 68 #endif | 63 #endif |
| 69 | 64 |
| 70 typename ArrayType<T>::Allocator::Ptr m_array; | 65 typename ArrayType<T>::Allocator::Ptr m_array; |
| 71 size_t m_count; | 66 size_t m_count; |
| 72 size_t m_capacity; | 67 size_t m_capacity; |
| 73 }; | 68 }; |
| 74 | 69 |
| 75 } // namespace WTF | 70 } // namespace WTF |
| 76 | 71 |
| 77 using WTF::TerminatedArrayBuilder; | 72 using WTF::TerminatedArrayBuilder; |
| 78 | 73 |
| 79 #endif // TerminatedArrayBuilder_h | 74 #endif // TerminatedArrayBuilder_h |
| OLD | NEW |