X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2Fcml%2Fcore%2Fdynamic_1D.h;h=f69305db9bcd19333a75437a03472e241b25fe12;hp=2aae2c6c0b680a2160ced8ca78f60791e857b71b;hb=40755d4c6251206c18ce4784967d3a910cee096f;hpb=d08114d4e7315636ff62127845150273e0cbf66f diff --git a/src/Moof/cml/core/dynamic_1D.h b/src/Moof/cml/core/dynamic_1D.h index 2aae2c6..f69305d 100644 --- a/src/Moof/cml/core/dynamic_1D.h +++ b/src/Moof/cml/core/dynamic_1D.h @@ -13,7 +13,7 @@ Boost Software License, v1.0 (see cml/LICENSE for details). #ifndef dynamic_1D_h #define dynamic_1D_h -#include +#include #include #include @@ -37,15 +37,12 @@ class dynamic_1D /* Record the generator: */ typedef dynamic generator_type; - /* Array implementation: */ - typedef std::vector array_impl; - /* Standard: */ - typedef typename array_impl::value_type value_type; - typedef typename array_impl::pointer pointer; - typedef typename array_impl::reference reference; - typedef typename array_impl::const_reference const_reference; - typedef typename array_impl::const_pointer const_pointer; + typedef typename allocator_type::value_type value_type; + typedef typename allocator_type::pointer pointer; + typedef typename allocator_type::reference reference; + typedef typename allocator_type::const_reference const_reference; + typedef typename allocator_type::const_pointer const_pointer; /* For matching by memory type: */ typedef dynamic_memory_tag memory_tag; @@ -69,16 +66,29 @@ class dynamic_1D public: /** Construct a dynamic array with no size. */ - dynamic_1D() {} + dynamic_1D() : m_size(0), m_data(0), m_alloc() {} /** Construct a dynamic array given the size. */ - explicit dynamic_1D(size_t size) : m_data(size) {} + explicit dynamic_1D(size_t size) : m_size(0), m_data(0), m_alloc() { + this->resize(size); + } + + /** Copy construct a dynamic array. */ + dynamic_1D(const dynamic_1D& other) + : m_size(0), m_data(0), m_alloc() + { + this->copy(other); + } + + ~dynamic_1D() { + this->destroy(); + } public: /** Return the number of elements in the array. */ - size_t size() const { return this->m_data.size(); } + size_t size() const { return m_size; } /** Access to the data as a C array. * @@ -87,7 +97,7 @@ class dynamic_1D * * @note This function does not range-check the argument. */ - reference operator[](size_t i) { return this->m_data[i]; } + reference operator[](size_t i) { return m_data[i]; } /** Const access to the data as a C array. * @@ -96,7 +106,7 @@ class dynamic_1D * * @note This function does not range-check the argument. */ - const_reference operator[](size_t i) const { return this->m_data[i]; } + const_reference operator[](size_t i) const { return m_data[i]; } /** Return access to the data as a raw pointer. */ pointer data() { return &m_data[0]; } @@ -107,16 +117,81 @@ class dynamic_1D public: - /** Set the array size to the given value. + /** Set the array size to the given value. The previous contents are + * destroyed before reallocating the array. If s == size(), + * nothing happens. * * @warning This is not guaranteed to preserve the original data. */ - void resize(size_t s) { this->m_data.resize(s); } + void resize(size_t s) { + + /* Nothing to do if the size isn't changing: */ + if(s == m_size) return; + + /* Destroy the current array contents: */ + this->destroy(); + + /* Set the new size if non-zero: */ + if(s > 0) { + value_type* data = m_alloc.allocate(s); + for(size_t i = 0; i < s; ++ i) + m_alloc.construct(&data[i], value_type()); + + /* Success, save s and data: */ + m_size = s; + m_data = data; + } + } + + /** Copy the source array. The previous contents are destroyed before + * reallocating the array. If other == *this, nothing happens. + */ + void copy(const dynamic_1D& other) { + + /* Nothing to do if it's the same array: */ + if(&other == this) return; + + /* Destroy the current array contents: */ + this->destroy(); + + /* Set the new size if non-zero: */ + size_t s = other.size(); + if(s > 0) { + value_type* data = m_alloc.allocate(s); + for(size_t i = 0; i < s; ++ i) + m_alloc.construct(&data[i], other[i]); + + /* Success, so save the new array and the size: */ + m_size = s; + m_data = data; + } + } protected: - array_impl m_data; + /** Destroy the current contents of the array. */ + void destroy() { + if(m_data) { + for(size_t i = 0; i < m_size; ++ i) + m_alloc.destroy(&m_data[i]); + m_alloc.deallocate(m_data, m_size); + m_size = 0; + m_data = 0; + } + } + + + protected: + + /** Current array size (may be 0). */ + size_t m_size; + + /** Array data (may be NULL). */ + value_type* m_data; + + /** Allocator for the array. */ + allocator_type m_alloc; }; } // namespace cml