1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
17 #include <cml/core/common.h>
18 #include <cml/dynamic.h>
22 /** Dynamically-sized and allocated 1D array.
24 * @note The allocator should be an STL-compatible allocator.
26 * @internal The internal array type <em>must</em> have the proper copy
27 * semantics, otherwise copy construction will fail.
29 template<typename Element
, class Alloc
>
34 /* Record the allocator type: */
35 typedef typename
Alloc::template rebind
<Element
>::other allocator_type
;
37 /* Record the generator: */
38 typedef dynamic
<Alloc
> generator_type
;
41 typedef typename
allocator_type::value_type value_type
;
42 typedef typename
allocator_type::pointer pointer
;
43 typedef typename
allocator_type::reference reference
;
44 typedef typename
allocator_type::const_reference const_reference
;
45 typedef typename
allocator_type::const_pointer const_pointer
;
47 /* For matching by memory type: */
48 typedef dynamic_memory_tag memory_tag
;
50 /* For matching by size type: */
51 typedef dynamic_size_tag size_tag
;
53 /* For matching by resizability: */
54 typedef resizable_tag resizing_tag
;
56 /* For matching by dimensions: */
57 typedef oned_tag dimension_tag
;
62 /** Dynamic arrays have no fixed size. */
63 enum { array_size
= -1 };
68 /** Construct a dynamic array with no size. */
69 dynamic_1D() : m_size(0), m_data(0), m_alloc() {}
71 /** Construct a dynamic array given the size. */
72 explicit dynamic_1D(size_t size
) : m_size(0), m_data(0), m_alloc() {
76 /** Copy construct a dynamic array. */
77 dynamic_1D(const dynamic_1D
& other
)
78 : m_size(0), m_data(0), m_alloc()
90 /** Return the number of elements in the array. */
91 size_t size() const { return m_size
; }
93 /** Access to the data as a C array.
95 * @param i a size_t index into the array.
96 * @return a mutable reference to the array value at i.
98 * @note This function does not range-check the argument.
100 reference
operator[](size_t i
) { return m_data
[i
]; }
102 /** Const access to the data as a C array.
104 * @param i a size_t index into the array.
105 * @return a const reference to the array value at i.
107 * @note This function does not range-check the argument.
109 const_reference
operator[](size_t i
) const { return m_data
[i
]; }
111 /** Return access to the data as a raw pointer. */
112 pointer
data() { return &m_data
[0]; }
114 /** Return access to the data as a raw pointer. */
115 const_pointer
data() const { return &m_data
[0]; }
120 /** Set the array size to the given value. The previous contents are
121 * destroyed before reallocating the array. If s == size(),
124 * @warning This is not guaranteed to preserve the original data.
126 void resize(size_t s
) {
128 /* Nothing to do if the size isn't changing: */
129 if(s
== m_size
) return;
131 /* Destroy the current array contents: */
134 /* Set the new size if non-zero: */
136 value_type
* data
= m_alloc
.allocate(s
);
137 for(size_t i
= 0; i
< s
; ++ i
)
138 m_alloc
.construct(&data
[i
], value_type());
140 /* Success, save s and data: */
146 /** Copy the source array. The previous contents are destroyed before
147 * reallocating the array. If other == *this, nothing happens.
149 void copy(const dynamic_1D
& other
) {
151 /* Nothing to do if it's the same array: */
152 if(&other
== this) return;
154 /* Destroy the current array contents: */
157 /* Set the new size if non-zero: */
158 size_t s
= other
.size();
160 value_type
* data
= m_alloc
.allocate(s
);
161 for(size_t i
= 0; i
< s
; ++ i
)
162 m_alloc
.construct(&data
[i
], other
[i
]);
164 /* Success, so save the new array and the size: */
173 /** Destroy the current contents of the array. */
176 for(size_t i
= 0; i
< m_size
; ++ i
)
177 m_alloc
.destroy(&m_data
[i
]);
178 m_alloc
.deallocate(m_data
, m_size
);
187 /** Current array size (may be 0). */
190 /** Array data (may be NULL). */
193 /** Allocator for the array. */
194 allocator_type m_alloc
;
201 // -------------------------------------------------------------------------