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 *-----------------------------------------------------------------------*/
16 #include <cml/core/common.h>
17 #include <cml/core/cml_meta.h>
18 #include <cml/core/cml_assert.h>
19 #include <cml/fixed.h>
23 /** Statically-allocated array.
25 * @note This class is designed to have the same size as a C array with the
26 * same length. It's therefore possible (but not recommended!) to coerce
27 * a normal C array into a fixed_1D<> like this:
29 * typedef fixed_1D<double,10> array;
31 * array& array_object = *((array*)&c_array);
32 * double e1 = array_object[1];
34 * It's also possible to do this with a pointer to an array of values (e.g. a
35 * double*), whether or not it was actually declared as a fixed C array. This
36 * is HIGHLY DISCOURAGED, though. It's relatively straightforward to implement
37 * a separate class to take a C array (or pointer) and turn it into an array
42 * @internal Do <em>not</em> add the empty constructor and destructor; at
43 * least one compiler (Intel C++ 9.0) fails to optimize them away, and they
44 * aren't needed anyway here.
46 template<typename Element
, int Size
>
51 /* Require Size > 0: */
52 CML_STATIC_REQUIRE(Size
> 0);
54 /* Record the generator: */
55 typedef fixed
<Size
,-1> generator_type
;
58 typedef Element value_type
;
59 typedef Element
* pointer
;
60 typedef Element
& reference
;
61 typedef const Element
& const_reference
;
62 typedef const Element
* const_pointer
;
64 /* Array implementation: */
65 typedef value_type array_impl
[Size
];
67 /* For matching by memory type: */
68 typedef fixed_memory_tag memory_tag
;
70 /* For matching by size type: */
71 typedef fixed_size_tag size_tag
;
73 /* For matching by resizability: */
74 typedef not_resizable_tag resizing_tag
;
76 /* For matching by dimensions: */
77 typedef oned_tag dimension_tag
;
82 /** The length as an enumerated value. */
83 enum { array_size
= Size
};
88 /** Return the number of elements in the array. */
89 size_t size() const { return size_t(array_size
); }
91 /** Access to the data as a C array.
93 * @param i a size_t index into the array.
94 * @return a mutable reference to the array value at i.
96 * @note This function does not range-check the argument.
98 reference
operator[](size_t i
) { return m_data
[i
]; }
100 /** Const access to the data as a C array.
102 * @param i a size_t index into the array.
103 * @return a const reference to the array value at i.
105 * @note This function does not range-check the argument.
107 const_reference
operator[](size_t i
) const { return m_data
[i
]; }
109 /** Return access to the data as a raw pointer. */
110 pointer
data() { return &m_data
[0]; }
112 /** Return access to the data as a raw pointer. */
113 const_pointer
data() const { return &m_data
[0]; }
129 // -------------------------------------------------------------------------