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
;
40 /* Array implementation: */
41 typedef std::vector
<Element
,allocator_type
> array_impl
;
44 typedef typename
array_impl::value_type value_type
;
45 typedef typename
array_impl::pointer pointer
;
46 typedef typename
array_impl::reference reference
;
47 typedef typename
array_impl::const_reference const_reference
;
48 typedef typename
array_impl::const_pointer const_pointer
;
50 /* For matching by memory type: */
51 typedef dynamic_memory_tag memory_tag
;
53 /* For matching by size type: */
54 typedef dynamic_size_tag size_tag
;
56 /* For matching by resizability: */
57 typedef resizable_tag resizing_tag
;
59 /* For matching by dimensions: */
60 typedef oned_tag dimension_tag
;
65 /** Dynamic arrays have no fixed size. */
66 enum { array_size
= -1 };
71 /** Construct a dynamic array with no size. */
74 /** Construct a dynamic array given the size. */
75 explicit dynamic_1D(size_t size
) : m_data(size
) {}
80 /** Return the number of elements in the array. */
81 size_t size() const { return this->m_data
.size(); }
83 /** Access to the data as a C array.
85 * @param i a size_t index into the array.
86 * @return a mutable reference to the array value at i.
88 * @note This function does not range-check the argument.
90 reference
operator[](size_t i
) { return this->m_data
[i
]; }
92 /** Const access to the data as a C array.
94 * @param i a size_t index into the array.
95 * @return a const reference to the array value at i.
97 * @note This function does not range-check the argument.
99 const_reference
operator[](size_t i
) const { return this->m_data
[i
]; }
101 /** Return access to the data as a raw pointer. */
102 pointer
data() { return &m_data
[0]; }
104 /** Return access to the data as a raw pointer. */
105 const_pointer
data() const { return &m_data
[0]; }
110 /** Set the array size to the given value.
112 * @warning This is not guaranteed to preserve the original data.
114 void resize(size_t s
) { this->m_data
.resize(s
); }
126 // -------------------------------------------------------------------------