JayBeams  0.1
Another project to have fun coding.
array_traits.hpp
Go to the documentation of this file.
1 #ifndef jb_detail_array_traits_hpp
2 #define jb_detail_array_traits_hpp
3 
6 
7 #include <boost/multi_array.hpp>
8 #include <type_traits>
9 #include <vector>
10 
11 namespace jb {
12 namespace detail {
13 /**
14  * Alias element type stored on a container.
15  *
16  * The generic version implements the class assuming std::vector<> or
17  * any similar container. We provide specializations for other types
18  * (such as boost::multi_array<>) to be able to operate on them
19  * generically.
20  *
21  * @tparam container_type the type of container, typically std::vector<>
22  */
23 template <typename container_type>
24 struct array_traits {
25  /// Define the type of the elements in the container
26  using element_type = typename container_type::value_type;
27 };
28 
29 /**
30  * Alias array_type based on the container_type shape
31  * to store value_type.
32  *
33  * The generic version implements the class assuming std::vector<> or
34  * any similar container. We provide specializations for other types
35  * (such as boost::multi_array<>) to be able to operate on them
36  * generically.
37  *
38  * @tparam value_type the type to store on the aligned_container_type
39  * @tparam array_type the type of container, typically std::vector<>
40  */
41 template <typename value_type, typename container_type>
44 };
45 
46 /**
47  * Count the number of elements for a vector-like container.
48  *
49  * The generic version implements the class assuming std::vector<> or
50  * any similar container.
51  *
52  * @tparam container_type the type of container, typically std::vector<>
53  */
54 template <typename container_type>
55 inline std::size_t element_count(container_type const& a) {
56  return a.size();
57 }
58 
59 /**
60  * Count the elements in the last dimension of a vector-like container.
61  *
62  * The generic version implements the class assuming std::vector<> or
63  * any similar container.
64  *
65  * @tparam container_type the type of container, typically std::vector<>
66  */
67 template <typename container_type>
68 inline std::size_t nsamples(container_type const& a) {
69  return a.size();
70 }
71 
72 /**
73  * Return the shape of the container in a form suitable for construction
74  * of a vector-like container.
75  *
76  * The generic version implements the class assuming std::vector<> or
77  * any similar container.
78  *
79  * @tparam container_type the type of container, typically std::vector<>
80  */
81 template <typename container_type>
82 inline std::size_t array_shape(container_type const& a) {
83  return a.size();
84 }
85 
86 /**
87  * Alias element type stored on a boost::multi_array<> containers.
88  *
89  * Provides specializations for a boost multi array container to be able
90  * to operate on them generically.
91  *
92  * @tparam T the type stored by the boost::multi_array container
93  * @tparam K boost multi array dimensionality
94  * @tparam A an Allocator type for type T allocator storage
95  */
96 template <typename T, std::size_t K, typename A>
97 struct array_traits<boost::multi_array<T, K, A>> {
98  /// Define the type of the elements in the array
99  using element_type = typename boost::multi_array<T, K, A>::element;
100 };
101 
102 /**
103  * Alias array_type based on the boost::multi_array type shape
104  * to store value_type.
105  *
106  * Provides specializations for a boost multi array container to be able
107  * to operate on them generically.
108  *
109  * @tparam value_type the type to store on the aligned_container_type
110  * @tparam T the type stored by the boost::multi_array container
111  * @tparam K boost multi array dimensionality
112  * @tparam A an Allocator type for type T allocator storage
113  */
114 template <typename value_type, typename T, std::size_t K, typename A>
115 struct aligned_container<value_type, boost::multi_array<T, K, A>> {
116  /// Define the type of the elements in the container
118 };
119 
120 /**
121  * Count the number of elements for boost::multi_array<> containers.
122  *
123  * Provides specializations for a boost multi array container to be able
124  * to operate on them generically.
125  *
126  * @tparam T the type stored by the boost::multi_array container
127  * @tparam K boost multi array dimensionality
128  * @tparam A an Allocator type for type T allocator storage
129  */
130 template <typename T, std::size_t K, typename A>
131 inline std::size_t element_count(boost::multi_array<T, K, A> const& a) {
132  return a.num_elements();
133 }
134 
135 /**
136  * Count the number of elements in the last dimension for
137  * boost::multi_array<> containers.
138  *
139  * Provides specializations for a boost multi array container to be able
140  * to operate on them generically.
141  *
142  * @tparam T the type stored by the boost::multi_array container
143  * @tparam K boost multi array dimensionality
144  * @tparam A an Allocator type for type T allocator storage
145  */
146 template <typename T, std::size_t K, typename A>
147 inline std::size_t nsamples(boost::multi_array<T, K, A> const& a) {
148  return a.shape()[a.num_dimensions() - 1];
149 }
150 
151 /**
152  * Return the shape of the container in a form suitable for construction
153  * of a boost::multi_array<> containers.
154  *
155  * Provides specializations for a boost multi array container to be able
156  * to operate on them generically.
157  *
158  * @tparam T the type stored by the boost::multi_array container
159  * @tparam K boost multi array dimensionality
160  * @tparam A an Allocator type for type T allocator storage
161  */
162 template <typename T, std::size_t K, typename A>
163 inline std::vector<std::size_t>
164 array_shape(boost::multi_array<T, K, A> const& a) {
165  return std::vector<std::size_t>(a.shape(), a.shape() + a.dimensionality);
166 }
167 
168 /**
169  * Determine if a timeseries type guarantees alignment suitable for
170  * SIMD optimizations.
171  *
172  * We provide specializations for aligned_vector (e.g. std::vector<>) and
173  * aligned_multi_array (e.g. boost::multi_array) to be able to operate
174  * on them generically.
175  *
176  * @tparam T the type stored by the timeseries container
177  */
178 template <typename T>
179 struct always_aligned : public std::false_type {};
180 
181 /**
182  * Determine if a aligned_vector timeseries type guarantees alignment suitable
183  * for
184  * SIMD optimizations.
185  *
186  * @tparam T the type stored by the aligned_vector timeseries
187  */
188 template <typename T>
189 struct always_aligned<jb::fftw::aligned_vector<T>> : public std::true_type {};
190 
191 /**
192  * Determine if a aligned_multi_array timeseries type guarantees alignment
193  * suitable for
194  * SIMD optimizations.
195  *
196  * @tparam T the type stored by the aligned_multi_array timeseries
197  * @tparam K aligned_multi_array dimensionality
198  *
199  */
200 template <typename T, std::size_t K>
202  : public std::true_type {};
203 
204 } // namespace detail
205 } // namespace jb
206 
207 #endif // jb_detail_array_traits_hpp
typename jb::fftw::aligned_multi_array< value_type, K > array_type
Define the type of the elements in the container.
typename jb::fftw::aligned_vector< complex_type > array_type
std::vector< T, jb::fftw::allocator< T > > aligned_vector
Alias std::vector with properly allocated storage for FFTW3.
typename container_type::value_type element_type
Define the type of the elements in the container.
boost::multi_array< value_type, num_dims, jb::fftw::allocator< value_type > > aligned_multi_array
Alias boost::multi_array with properly allocated storage for FFTW3.
std::size_t array_shape(container_type const &a)
Return the shape of the container in a form suitable for construction of a vector-like container...
Alias array_type based on the container_type shape to store value_type.
std::size_t nsamples(container_type const &a)
Count the elements in the last dimension of a vector-like container.
Determine if a timeseries type guarantees alignment suitable for SIMD optimizations.
typename boost::multi_array< T, K, A >::element element_type
Define the type of the elements in the array.
std::size_t element_count(container_type const &a)
Count the number of elements for a vector-like container.
Alias element type stored on a container.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7