10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_DIMENSIONS_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_DIMENSIONS_H
33 template <
typename Index>
struct IndexPair {
34 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE IndexPair() : first(0), second(0) { }
35 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE IndexPair(Index f, Index s) : first(f), second(s) { }
43 template<std::
size_t n,
typename Dimension>
struct dget {
44 static const std::size_t value = get<n, Dimension>::value;
48 template<
typename Index, std::
size_t NumIndices, std::
size_t n,
bool RowMajor>
49 struct fixed_size_tensor_index_linearization_helper
51 template <
typename Dimensions> EIGEN_DEVICE_FUNC
52 static inline Index run(array<Index, NumIndices>
const& indices,
53 const Dimensions& dimensions)
55 return array_get<RowMajor ? n : (NumIndices - n - 1)>(indices) +
56 dget<RowMajor ? n : (NumIndices - n - 1), Dimensions>::value *
57 fixed_size_tensor_index_linearization_helper<Index, NumIndices, n - 1, RowMajor>::run(indices, dimensions);
61 template<
typename Index, std::
size_t NumIndices,
bool RowMajor>
62 struct fixed_size_tensor_index_linearization_helper<Index, NumIndices, 0, RowMajor>
64 template <
typename Dimensions> EIGEN_DEVICE_FUNC
65 static inline Index run(array<Index, NumIndices>
const& indices,
68 return array_get<RowMajor ? 0 : NumIndices - 1>(indices);
72 template<
typename Index, std::
size_t n>
73 struct fixed_size_tensor_index_extraction_helper
75 template <
typename Dimensions> EIGEN_DEVICE_FUNC
76 static inline Index run(
const Index index,
77 const Dimensions& dimensions)
79 const Index mult = (index == n) ? 1 : 0;
80 return array_get<n>(dimensions) * mult +
81 fixed_size_tensor_index_extraction_helper<Index, n - 1>::run(index, dimensions);
85 template<
typename Index>
86 struct fixed_size_tensor_index_extraction_helper<Index, 0>
88 template <
typename Dimensions> EIGEN_DEVICE_FUNC
89 static inline Index run(
const Index index,
90 const Dimensions& dimensions)
92 const Index mult = (index == 0) ? 1 : 0;
93 return array_get<0>(dimensions) * mult;
101 #ifndef EIGEN_EMULATE_CXX11_META_H
102 template <
typename std::ptrdiff_t... Indices>
103 struct Sizes : internal::numeric_list<std::ptrdiff_t, Indices...> {
104 typedef internal::numeric_list<std::ptrdiff_t, Indices...> Base;
105 static const std::ptrdiff_t total_size = internal::arg_prod(Indices...);
107 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t rank()
const {
111 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t TotalSize() {
112 return internal::arg_prod(Indices...);
116 template <
typename DenseIndex>
117 explicit Sizes(
const array<DenseIndex, Base::count>& ) {
120 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
121 template <
typename... DenseIndex> Sizes(DenseIndex...) { }
122 explicit Sizes(std::initializer_list<std::ptrdiff_t> ) {
127 template <
typename T> Sizes& operator = (
const T& ) {
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t operator[] (
const std::size_t index)
const {
133 return internal::fixed_size_tensor_index_extraction_helper<std::ptrdiff_t, Base::count - 1>::run(index, *
this);
136 template <
typename DenseIndex> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
137 size_t IndexOfColMajor(
const array<DenseIndex, Base::count>& indices)
const {
138 return internal::fixed_size_tensor_index_linearization_helper<DenseIndex, Base::count, Base::count - 1, false>::run(indices, *static_cast<const Base*>(
this));
140 template <
typename DenseIndex> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
141 size_t IndexOfRowMajor(
const array<DenseIndex, Base::count>& indices)
const {
142 return internal::fixed_size_tensor_index_linearization_helper<DenseIndex, Base::count, Base::count - 1, true>::run(indices, *static_cast<const Base*>(
this));
147 template <
typename std::ptrdiff_t... Indices>
148 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(
const Sizes<Indices...>&) {
149 return Sizes<Indices...>::total_size;
155 template <std::
size_t n>
156 struct non_zero_size {
157 typedef internal::type2val<std::size_t, n> type;
160 struct non_zero_size<0> {
161 typedef internal::null_type type;
164 template <std::
size_t V1=0, std::
size_t V2=0, std::
size_t V3=0, std::
size_t V4=0, std::
size_t V5=0>
struct Sizes {
165 typedef typename internal::make_type_list<typename non_zero_size<V1>::type,
typename non_zero_size<V2>::type,
typename non_zero_size<V3>::type,
typename non_zero_size<V4>::type,
typename non_zero_size<V5>::type >::type Base;
166 static const size_t count = Base::count;
167 static const std::size_t total_size = internal::arg_prod<Base>::value;
169 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
size_t rank()
const {
173 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
size_t TotalSize() {
174 return internal::arg_prod<Base>::value;
178 template <
typename DenseIndex>
179 explicit Sizes(
const array<DenseIndex, Base::count>& ) {
182 template <
typename T> Sizes& operator = (
const T& ) {
187 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
188 template <
typename... DenseIndex> Sizes(DenseIndex... ) { }
189 explicit Sizes(std::initializer_list<std::size_t>) {
193 EIGEN_DEVICE_FUNC
explicit Sizes(
const DenseIndex) {
195 EIGEN_DEVICE_FUNC
explicit Sizes(
const DenseIndex,
const DenseIndex) {
197 EIGEN_DEVICE_FUNC
explicit Sizes(
const DenseIndex,
const DenseIndex,
const DenseIndex) {
199 EIGEN_DEVICE_FUNC
explicit Sizes(
const DenseIndex,
const DenseIndex,
const DenseIndex,
const DenseIndex) {
201 EIGEN_DEVICE_FUNC
explicit Sizes(
const DenseIndex,
const DenseIndex,
const DenseIndex,
const DenseIndex,
const DenseIndex) {
205 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex operator[] (
const int index)
const {
208 return internal::get<0, Base>::value;
210 return internal::get<1, Base>::value;
212 return internal::get<2, Base>::value;
214 return internal::get<3, Base>::value;
216 return internal::get<4, Base>::value;
218 eigen_assert(
false &&
"index overflow");
219 return static_cast<std::size_t
>(-1);
223 template <
typename DenseIndex> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
224 size_t IndexOfColMajor(
const array<DenseIndex, Base::count>& indices)
const {
225 return internal::fixed_size_tensor_index_linearization_helper<DenseIndex, Base::count, Base::count - 1, false>::run(indices, *reinterpret_cast<const Base*>(
this));
227 template <
typename DenseIndex> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
228 size_t IndexOfRowMajor(
const array<DenseIndex, Base::count>& indices)
const {
229 return internal::fixed_size_tensor_index_linearization_helper<DenseIndex, Base::count, Base::count - 1, true>::run(indices, *reinterpret_cast<const Base*>(
this));
234 template <std::
size_t V1, std::
size_t V2, std::
size_t V3, std::
size_t V4, std::
size_t V5>
235 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t array_prod(
const Sizes<V1, V2, V3, V4, V5>&) {
236 return Sizes<V1, V2, V3, V4, V5>::total_size;
244 template<
typename Index, std::
size_t NumIndices, std::
size_t n,
bool RowMajor>
245 struct tensor_index_linearization_helper
247 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
248 Index run(array<Index, NumIndices>
const& indices, array<Index, NumIndices>
const& dimensions)
250 return array_get<RowMajor ? n : (NumIndices - n - 1)>(indices) +
251 array_get<RowMajor ? n : (NumIndices - n - 1)>(dimensions) *
252 tensor_index_linearization_helper<Index, NumIndices, n - 1, RowMajor>::run(indices, dimensions);
256 template<
typename Index, std::
size_t NumIndices,
bool RowMajor>
257 struct tensor_index_linearization_helper<Index, NumIndices, 0, RowMajor>
259 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
260 Index run(array<Index, NumIndices>
const& indices, array<Index, NumIndices>
const&)
262 return array_get<RowMajor ? 0 : NumIndices - 1>(indices);
270 template <
typename DenseIndex, std::
size_t NumDims>
271 struct DSizes : array<DenseIndex, NumDims> {
272 typedef array<DenseIndex, NumDims> Base;
273 static const std::size_t count = NumDims;
275 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
size_t rank()
const {
279 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex TotalSize()
const {
280 return internal::array_prod(*static_cast<const Base*>(
this));
283 EIGEN_DEVICE_FUNC DSizes() {
284 for (std::size_t i = 0 ; i < NumDims; ++i) {
288 EIGEN_DEVICE_FUNC
explicit DSizes(
const array<DenseIndex, NumDims>& a) : Base(a) { }
290 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
291 template<
typename... IndexTypes> EIGEN_DEVICE_FUNC
292 EIGEN_STRONG_INLINE
explicit DSizes(DenseIndex firstDimension, IndexTypes... otherDimensions) {
293 EIGEN_STATIC_ASSERT(
sizeof...(otherDimensions) + 1 == NumDims, YOU_MADE_A_PROGRAMMING_MISTAKE)
294 (*this) = array<DenseIndex, NumDims>{{firstDimension, otherDimensions...}};
297 EIGEN_DEVICE_FUNC
explicit DSizes(
const DenseIndex i0) {
298 eigen_assert(NumDims == 1);
301 EIGEN_DEVICE_FUNC
explicit DSizes(
const DenseIndex i0,
const DenseIndex i1) {
302 eigen_assert(NumDims == 2);
306 EIGEN_DEVICE_FUNC
explicit DSizes(
const DenseIndex i0,
const DenseIndex i1,
const DenseIndex i2) {
307 eigen_assert(NumDims == 3);
312 EIGEN_DEVICE_FUNC
explicit DSizes(
const DenseIndex i0,
const DenseIndex i1,
const DenseIndex i2,
const DenseIndex i3) {
313 eigen_assert(NumDims == 4);
319 EIGEN_DEVICE_FUNC
explicit DSizes(
const DenseIndex i0,
const DenseIndex i1,
const DenseIndex i2,
const DenseIndex i3,
const DenseIndex i4) {
320 eigen_assert(NumDims == 5);
329 EIGEN_DEVICE_FUNC DSizes& operator = (
const array<DenseIndex, NumDims>& other) {
330 *
static_cast<Base*
>(
this) = other;
335 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex IndexOfColMajor(
const array<DenseIndex, NumDims>& indices)
const {
336 return internal::tensor_index_linearization_helper<DenseIndex, NumDims, NumDims - 1, false>::run(indices, *static_cast<const Base*>(
this));
338 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex IndexOfRowMajor(
const array<DenseIndex, NumDims>& indices)
const {
339 return internal::tensor_index_linearization_helper<DenseIndex, NumDims, NumDims - 1, true>::run(indices, *static_cast<const Base*>(
this));
348 template<
typename Index, std::
size_t NumIndices, std::
size_t n,
bool RowMajor>
349 struct tensor_vsize_index_linearization_helper
351 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
352 Index run(array<Index, NumIndices>
const& indices, std::vector<DenseIndex>
const& dimensions)
354 return array_get<RowMajor ? n : (NumIndices - n - 1)>(indices) +
355 array_get<RowMajor ? n : (NumIndices - n - 1)>(dimensions) *
356 tensor_vsize_index_linearization_helper<Index, NumIndices, n - 1, RowMajor>::run(indices, dimensions);
360 template<
typename Index, std::
size_t NumIndices,
bool RowMajor>
361 struct tensor_vsize_index_linearization_helper<Index, NumIndices, 0, RowMajor>
363 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
364 Index run(array<Index, NumIndices>
const& indices, std::vector<DenseIndex>
const&)
366 return array_get<RowMajor ? 0 : NumIndices - 1>(indices);
374 template <
typename DenseIndex, std::
size_t NumDims>
struct array_size<const DSizes<DenseIndex, NumDims> > {
375 static const size_t value = NumDims;
377 template <
typename DenseIndex, std::
size_t NumDims>
struct array_size<DSizes<DenseIndex, NumDims> > {
378 static const size_t value = NumDims;
380 #ifndef EIGEN_EMULATE_CXX11_META_H
381 template <
typename std::ptrdiff_t... Indices>
struct array_size<const Sizes<Indices...> > {
382 static const std::ptrdiff_t value = Sizes<Indices...>::count;
384 template <
typename std::ptrdiff_t... Indices>
struct array_size<Sizes<Indices...> > {
385 static const std::ptrdiff_t value = Sizes<Indices...>::count;
387 template <std::ptrdiff_t n,
typename std::ptrdiff_t... Indices> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_get(
const Sizes<Indices...>&) {
388 return get<n, internal::numeric_list<std::size_t, Indices...> >::value;
391 template <std::
size_t V1, std::
size_t V2, std::
size_t V3, std::
size_t V4, std::
size_t V5>
struct array_size<const Sizes<V1,V2,V3,V4,V5> > {
392 static const size_t value = Sizes<V1,V2,V3,V4,V5>::count;
394 template <std::
size_t V1, std::
size_t V2, std::
size_t V3, std::
size_t V4, std::
size_t V5>
struct array_size<Sizes<V1,V2,V3,V4,V5> > {
395 static const size_t value = Sizes<V1,V2,V3,V4,V5>::count;
397 template <std::
size_t n, std::
size_t V1, std::
size_t V2, std::
size_t V3, std::
size_t V4, std::
size_t V5> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t array_get(
const Sizes<V1,V2,V3,V4,V5>&) {
398 return get<n, typename Sizes<V1,V2,V3,V4,V5>::Base>::value;
404 template <
typename Dims1,
typename Dims2,
size_t n,
size_t m>
405 struct sizes_match_up_to_dim {
406 static inline bool run(Dims1&, Dims2&) {
410 template <
typename Dims1,
typename Dims2,
size_t n>
411 struct sizes_match_up_to_dim<Dims1, Dims2, n, n> {
412 static inline bool run(Dims1& dims1, Dims2& dims2) {
413 return (array_get<n>(dims1) == array_get<n>(dims2)) &
414 sizes_match_up_to_dim<Dims1, Dims2, n-1, n-1>::run(dims1, dims2);
417 template <
typename Dims1,
typename Dims2>
418 struct sizes_match_up_to_dim<Dims1, Dims2, 0, 0> {
419 static inline bool run(Dims1& dims1, Dims2& dims2) {
420 return (array_get<0>(dims1) == array_get<0>(dims2));
427 template <
typename Dims1,
typename Dims2>
428 bool dimensions_match(Dims1& dims1, Dims2& dims2) {
429 return internal::sizes_match_up_to_dim<Dims1, Dims2, internal::array_size<Dims1>::value-1, internal::array_size<Dims2>::value-1>::run(dims1, dims2);
434 #endif // EIGEN_CXX11_TENSOR_TENSOR_DIMENSIONS_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13