#ifndef dynamic_1D_h
#define dynamic_1D_h
-#include <vector>
+#include <memory>
#include <cml/core/common.h>
#include <cml/dynamic.h>
/* Record the generator: */
typedef dynamic<Alloc> generator_type;
- /* Array implementation: */
- typedef std::vector<Element,allocator_type> 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;
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.
*
*
* @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.
*
*
* @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]; }
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