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 *-----------------------------------------------------------------------*/
12 * Defines the fixed-size and runtime-sized external 1D arrays.
14 * @todo Need a better way to designate non-resizable, run-time sized
15 * arrays (e.g. by a resizeable tag).
21 #include <cml/core/common.h>
22 #include <cml/core/cml_meta.h>
23 #include <cml/core/cml_assert.h>
24 #include <cml/external.h>
28 /** Fixed-size external 1D array.
30 * Both the memory and the size are fixed at compile time, and cannot be
33 template<typename Element
, int Size
= -1>
38 /* Require Size > 0: */
39 CML_STATIC_REQUIRE(Size
> 0);
41 /* Record the generator: */
42 typedef external
<Size
,-1> generator_type
;
45 typedef Element value_type
;
46 typedef Element
* pointer
;
47 typedef Element
& reference
;
48 typedef const Element
& const_reference
;
49 typedef const Element
* const_pointer
;
51 /* Array implementation: */
52 typedef value_type array_impl
[Size
];
54 /* For matching by memory type: */
55 typedef external_memory_tag memory_tag
;
57 /* For matching by size type: */
58 typedef fixed_size_tag size_tag
;
60 /* For matching by resizability: */
61 typedef not_resizable_tag resizing_tag
;
63 /* For matching by dimensions: */
64 typedef oned_tag dimension_tag
;
69 /** The length as an enumerated value. */
70 enum { array_size
= Size
};
75 external_1D(pointer
const ptr
)
81 /** Return the number of elements in the array. */
82 size_t size() const { return size_t(array_size
); }
84 /** Access to the data as a C array.
86 * @param i a size_t index into the array.
87 * @return a mutable reference to the array value at i.
89 * @note This function does not range-check the argument.
91 reference
operator[](size_t i
) { return m_data
[i
]; }
93 /** Const access to the data as a C array.
95 * @param i a size_t index into the array.
96 * @return a const reference to the array value at i.
98 * @note This function does not range-check the argument.
100 const_reference
operator[](size_t i
) const { return m_data
[i
]; }
102 /** Return access to the data as a raw pointer. */
103 pointer
data() { return m_data
; }
105 /** Return access to the data as a raw pointer. */
106 const_pointer
data() const { return m_data
; }
111 pointer
const m_data
;
116 /* Initialization without an argument isn't allowed: */
120 /** Run-time sized external 1D array.
122 * Both the memory and the size are fixed at run-time, and cannot be
123 * changed. This is a specialization for the case that Rows and Cols are
124 * not specified (i.e. given as the default of -1,-1).
126 template<typename Element
>
127 class external_1D
<Element
,-1>
131 /* Record the generator. Note: this is *not* unique, as it is the same
132 * generator used by external_2D. However, external_2D is used only by
133 * matrix<> classes, so this is not a problem.
135 typedef external
<> generator_type
;
138 typedef Element value_type
;
139 typedef Element
* pointer
;
140 typedef Element
& reference
;
141 typedef const Element
& const_reference
;
142 typedef const Element
* const_pointer
;
144 /* For matching by memory type: */
145 typedef external_memory_tag memory_tag
;
147 /* For matching by size type: */
148 typedef dynamic_size_tag size_tag
;
150 /* For matching by resizability: */
151 typedef not_resizable_tag resizing_tag
;
153 /* For matching by dimensions: */
154 typedef oned_tag dimension_tag
;
159 /** The length as an enumerated value. */
160 enum { array_size
= -1 };
165 external_1D(pointer
const ptr
, size_t size
)
166 : m_data(ptr
), m_size(size
) {}
171 /** Return the number of elements in the array. */
172 size_t size() const { return m_size
; }
174 /** Access to the data as a C array.
176 * @param i a size_t index into the array.
177 * @return a mutable reference to the array value at i.
179 * @note This function does not range-check the argument.
181 reference
operator[](size_t i
) { return m_data
[i
]; }
183 /** Const access to the data as a C array.
185 * @param i a size_t index into the array.
186 * @return a const reference to the array value at i.
188 * @note This function does not range-check the argument.
190 const_reference
operator[](size_t i
) const { return m_data
[i
]; }
192 /** Return access to the data as a raw pointer. */
193 pointer
data() { return m_data
; }
195 /** Return access to the data as a raw pointer. */
196 const_pointer
data() const { return m_data
; }
201 pointer
const m_data
;
207 /* Initialization without an argument isn't allowed: */
215 // -------------------------------------------------------------------------