File: tensor_iterator_wrapper.h

package info (click to toggle)
bagel 1.2.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 134,940 kB
  • sloc: cpp: 1,236,571; javascript: 15,383; python: 1,461; ansic: 674; makefile: 253; sh: 109
file content (46 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef __BTAS_TENSOR_ITERATOR_WRAPPER_H
#define __BTAS_TENSOR_ITERATOR_WRAPPER_H 1

#include <type_traits>

#include <btas/tensor_traits.h>

namespace btas {

template<bool _HasData>
struct tensor_iterator_wrapper
{
   template<class _Tensor>
   static auto begin(_Tensor& x) -> decltype(x.begin()) { return x.begin(); }

   template<class _Tensor>
   static auto end  (_Tensor& x) -> decltype(x.end  ()) { return x.end  (); }
};

template<>
struct tensor_iterator_wrapper<true>
{
   template<class _Tensor>
   static auto begin(_Tensor& x) -> decltype(x.data()) { return x.data(); }

   template<class _Tensor>
   static auto end  (_Tensor& x) -> decltype(x.data()) { return x.data()+x.size(); }
};

/// \return wrapped iterator of Tensor to the first
template<class _Tensor, class = typename std::enable_if<is_tensor<_Tensor>::value>::type>
auto tbegin (_Tensor& x) -> decltype(tensor_iterator_wrapper<has_data<_Tensor>::value>::begin(x))
{
   return tensor_iterator_wrapper<has_data<_Tensor>::value>::begin(x);
}

/// \return wrapped iterator of Tensor to the last
template<class _Tensor, class = typename std::enable_if<is_tensor<_Tensor>::value>::type>
auto tend (_Tensor& x) -> decltype(tensor_iterator_wrapper<has_data<_Tensor>::value>::end(x))
{
   return tensor_iterator_wrapper<has_data<_Tensor>::value>::end(x);
}

} // namespace btas

#endif // __BTAS_TENSOR_ITERATOR_WRAPPER_H