1 /* cache-aligned array, based on boost::array
3 * The following code declares class array,
4 * an STL container (as wrapper) for arrays of constant size.
7 * http://www.boost.org/libs/array/
10 * The original author site is at: http://www.josuttis.com/
12 * (C) Copyright Nicolai M. Josuttis 2001.
14 * Distributed under the Boost Software License, Version 1.0. (See
15 * accompanying file LICENSE_1_0.txt or copy at
16 * http://www.boost.org/LICENSE_1_0.txt)
18 * 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
19 * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
20 * 05 Aug 2001 - minor update (Nico Josuttis)
21 * 20 Jan 2001 - STLport fix (Beman Dawes)
22 * 29 Sep 2000 - Initial Revision (Nico Josuttis)
26 #ifndef CACHE_ALIGNED_ARRAY_HPP
27 #define CACHE_ALIGNED_ARRAY_HPP
31 #include <boost/assert.hpp>
33 // Handles broken standard libraries better than <iterator>
34 #include <boost/detail/iterator.hpp>
35 #include <boost/throw_exception.hpp>
38 // FIXES for broken compilers
39 #include <boost/config.hpp>
41 #include "malloc_aligned.hpp"
45 template<class T
, std::size_t N
>
48 T
* elems
; // fixed-size array of elements of type T
53 elems
= (T
*)malloc_aligned(N
* sizeof(T
));
54 for (int i
= 0; i
!= N
; ++i
)
58 aligned_array(aligned_array
const & rhs
)
60 elems
= (T
*)malloc_aligned(N
* sizeof(T
));
61 for (int i
= 0; i
!= N
; ++i
)
68 for (int i
= 0; i
!= N
; ++i
)
76 typedef const T
* const_iterator
;
78 typedef const T
& const_reference
;
79 typedef std::size_t size_type
;
80 typedef std::ptrdiff_t difference_type
;
83 iterator
begin() { return elems
; }
84 const_iterator
begin() const { return elems
; }
85 iterator
end() { return elems
+N
; }
86 const_iterator
end() const { return elems
+N
; }
88 // reverse iterator support
89 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
90 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
91 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
92 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
93 // workaround for broken reverse_iterator in VC7
94 typedef std::reverse_iterator
<std::_Ptrit
<value_type
, difference_type
, iterator
,
95 reference
, iterator
, reference
> > reverse_iterator
;
96 typedef std::reverse_iterator
<std::_Ptrit
<value_type
, difference_type
, const_iterator
,
97 const_reference
, iterator
, reference
> > const_reverse_iterator
;
99 // workaround for broken reverse_iterator implementations
100 typedef std::reverse_iterator
<iterator
,T
> reverse_iterator
;
101 typedef std::reverse_iterator
<const_iterator
,T
> const_reverse_iterator
;
104 reverse_iterator
rbegin() { return reverse_iterator(end()); }
105 const_reverse_iterator
rbegin() const {
106 return const_reverse_iterator(end());
108 reverse_iterator
rend() { return reverse_iterator(begin()); }
109 const_reverse_iterator
rend() const {
110 return const_reverse_iterator(begin());
114 reference
operator[](size_type i
)
116 BOOST_ASSERT( i
< N
&& "out of range" );
120 const_reference
operator[](size_type i
) const
122 BOOST_ASSERT( i
< N
&& "out of range" );
126 // at() with range check
127 reference
at(size_type i
) { rangecheck(i
); return elems
[i
]; }
128 const_reference
at(size_type i
) const { rangecheck(i
); return elems
[i
]; }
130 // front() and back()
136 const_reference
front() const
146 const_reference
back() const
152 static size_type
size() { return N
; }
153 static bool empty() { return false; }
154 static size_type
max_size() { return N
; }
155 enum { static_size
= N
};
157 // swap (note: linear complexity)
158 void swap (aligned_array
<T
,N
>& y
) {
159 std::swap_ranges(begin(),end(),y
.begin());
162 // direct access to data (read-only)
163 const T
* data() const { return elems
; }
164 T
* data() { return elems
; }
166 // use array as C array (direct read/write access to data)
167 T
* c_array() { return elems
; }
169 // assignment with type conversion
170 aligned_array
& operator=(const aligned_array
& rhs
)
172 std::copy(rhs
.begin(),rhs
.end(), begin());
176 template <typename T2
>
177 aligned_array
<T
,N
>& operator= (const aligned_array
<T2
,N
>& rhs
) {
178 std::copy(rhs
.begin(),rhs
.end(), begin());
182 // assign one value to all elements
183 void assign (const T
& value
)
185 std::fill_n(begin(),size(),value
);
188 // check range (may be private because it is static)
189 static void rangecheck (size_type i
) {
191 throw std::out_of_range("aligned_array<>: index out of range");
197 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
199 class aligned_array
< T
, 0 > {
203 typedef T value_type
;
205 typedef const T
* const_iterator
;
206 typedef T
& reference
;
207 typedef const T
& const_reference
;
208 typedef std::size_t size_type
;
209 typedef std::ptrdiff_t difference_type
;
212 iterator
begin() { return iterator( reinterpret_cast< T
* >( this ) ); }
213 const_iterator
begin() const { return const_iterator( reinterpret_cast< const T
* >( this ) ); }
214 iterator
end() { return begin(); }
215 const_iterator
end() const { return begin(); }
217 // reverse iterator support
218 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
219 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
220 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
221 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
222 // workaround for broken reverse_iterator in VC7
223 typedef std::reverse_iterator
<std::_Ptrit
<value_type
, difference_type
, iterator
,
224 reference
, iterator
, reference
> > reverse_iterator
;
225 typedef std::reverse_iterator
<std::_Ptrit
<value_type
, difference_type
, const_iterator
,
226 const_reference
, iterator
, reference
> > const_reverse_iterator
;
228 // workaround for broken reverse_iterator implementations
229 typedef std::reverse_iterator
<iterator
,T
> reverse_iterator
;
230 typedef std::reverse_iterator
<const_iterator
,T
> const_reverse_iterator
;
233 reverse_iterator
rbegin() { return reverse_iterator(end()); }
234 const_reverse_iterator
rbegin() const {
235 return const_reverse_iterator(end());
237 reverse_iterator
rend() { return reverse_iterator(begin()); }
238 const_reverse_iterator
rend() const {
239 return const_reverse_iterator(begin());
243 reference
operator[](size_type i
)
245 return failed_rangecheck();
248 const_reference
operator[](size_type i
) const
250 return failed_rangecheck();
253 // at() with range check
254 reference
at(size_type i
) { return failed_rangecheck(); }
255 const_reference
at(size_type i
) const { return failed_rangecheck(); }
257 // front() and back()
260 return failed_rangecheck();
263 const_reference
front() const
265 return failed_rangecheck();
270 return failed_rangecheck();
273 const_reference
back() const
275 return failed_rangecheck();
279 static size_type
size() { return 0; }
280 static bool empty() { return true; }
281 static size_type
max_size() { return 0; }
282 enum { static_size
= 0 };
284 void swap (aligned_array
<T
,0>& y
) {
287 // direct access to data (read-only)
288 const T
* data() const { return 0; }
289 T
* data() { return 0; }
291 // use array as C array (direct read/write access to data)
292 T
* c_array() { return 0; }
294 // assignment with type conversion
295 template <typename T2
>
296 aligned_array
<T
,0>& operator= (const aligned_array
<T2
,0>& ) {
300 // assign one value to all elements
301 void assign (const T
& ) { }
303 // check range (may be private because it is static)
304 static reference
failed_rangecheck () {
305 std::out_of_range
e("attempt to access element of an empty aligned_array");
306 boost::throw_exception(e
);
308 // We need to return something here to keep
309 // some compilers happy: however we will never
310 // actually get here....
312 static T placeholder
;
319 template<class T
, std::size_t N
>
320 bool operator== (const aligned_array
<T
,N
>& x
, const aligned_array
<T
,N
>& y
) {
321 return std::equal(x
.begin(), x
.end(), y
.begin());
323 template<class T
, std::size_t N
>
324 bool operator< (const aligned_array
<T
,N
>& x
, const aligned_array
<T
,N
>& y
) {
325 return std::lexicographical_compare(x
.begin(),x
.end(),y
.begin(),y
.end());
327 template<class T
, std::size_t N
>
328 bool operator!= (const aligned_array
<T
,N
>& x
, const aligned_array
<T
,N
>& y
) {
331 template<class T
, std::size_t N
>
332 bool operator> (const aligned_array
<T
,N
>& x
, const aligned_array
<T
,N
>& y
) {
335 template<class T
, std::size_t N
>
336 bool operator<= (const aligned_array
<T
,N
>& x
, const aligned_array
<T
,N
>& y
) {
339 template<class T
, std::size_t N
>
340 bool operator>= (const aligned_array
<T
,N
>& x
, const aligned_array
<T
,N
>& y
) {
345 template<class T
, std::size_t N
>
346 inline void swap (aligned_array
<T
,N
>& x
, aligned_array
<T
,N
>& y
) {
350 template <typename sample_type
, unsigned int n
>
352 inline sample_type
__attribute__((aligned(64))) * get_samples(aligned_array
<sample_type
, n
> & arg
)
354 inline sample_type
* get_samples(aligned_array
<sample_type
, n
> & arg
)
360 template <typename sample_type
, unsigned int n
>
362 inline const sample_type
__attribute__((aligned(64))) * get_samples(aligned_array
<sample_type
, n
> const & arg
)
364 inline const sample_type
* get_samples(aligned_array
<sample_type
, n
> const & arg
)
371 } /* namespace nova */
373 #endif /*CACHE_ALIGNED_ARRAY_HPP*/