TiledArray  0.7.0
tensor_map.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2015 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * tensor_map.h
22  * Jun 16, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_TENSOR_TENSOR_MAP_H__INCLUDED
27 #define TILEDARRAY_TENSOR_TENSOR_MAP_H__INCLUDED
28 
29 #include <initializer_list>
30 #include <type_traits>
31 
32 #include <TiledArray/range.h>
33 
34 namespace TiledArray {
35 
36  namespace detail {
37 
38  template <typename, typename> class TensorInterface;
39 
40  } // namespace detail
41 
42  template <typename T, typename Range_ = Range>
43  using TensorMap =
45 
46  template <typename T, typename Range_ = Range>
47  using TensorConstMap =
49 
50  template <typename T, typename Index>
51  inline TensorMap<T> make_map(T* const data, const Index& lower_bound,
52  const Index& upper_bound)
53  { return TensorMap<T>(Range(lower_bound, upper_bound), data); }
54 
55  template <typename T>
56  inline TensorMap<T> make_map(T* const data,
57  const std::initializer_list<std::size_t>& lower_bound,
58  const std::initializer_list<std::size_t>& upper_bound)
59  { return TensorMap<T>(Range(lower_bound, upper_bound), data); }
60 
61  template <typename T, typename Range_>
62  inline TensorMap<T, std::decay_t<Range_>> make_map(T* const data, Range_ && range)
63  { return TensorMap<T, std::decay_t<Range_>>(std::forward<Range_>(range), data); }
64 
65  template <typename T, typename Index>
66  inline TensorConstMap<T> make_map(const T* const data, const Index& lower_bound,
67  const Index& upper_bound)
68  { return TensorConstMap<T>(Range(lower_bound, upper_bound), data); }
69 
70 
71  template <typename T>
72  inline TensorConstMap<T> make_map(const T* const data,
73  const std::initializer_list<std::size_t>& lower_bound,
74  const std::initializer_list<std::size_t>& upper_bound)
75  { return TensorConstMap<T>(Range(lower_bound, upper_bound), data); }
76 
77  template <typename T, typename Range_>
78  inline TensorConstMap<T, std::decay_t<Range_>> make_map(const T* const data, Range_ && range)
79  { return TensorConstMap<T, std::decay_t<Range_>>(std::forward<Range_>(range), data); }
80 
81 
82  template <typename T, typename Index>
83  inline TensorConstMap<T> make_const_map(const T* const data, const Index& lower_bound,
84  const Index& upper_bound)
85  { return TensorConstMap<T>(Range(lower_bound, upper_bound), data); }
86 
87 
88  template <typename T>
89  inline TensorConstMap<T> make_const_map(const T* const data,
90  const std::initializer_list<std::size_t>& lower_bound,
91  const std::initializer_list<std::size_t>& upper_bound)
92  { return TensorConstMap<T>(Range(lower_bound, upper_bound), data); }
93 
94  template <typename T>
95  inline TensorConstMap<T> make_const_map(const T* const data, const Range& range)
96  { return TensorConstMap<T>(range, data); }
97 
98  template <typename T, typename Index>
99  inline TensorConstMap<T> make_const_map(T* const data, const Index& lower_bound,
100  const Index& upper_bound)
101  { return TensorConstMap<T>(Range(lower_bound, upper_bound), const_cast<const T*>(data)); }
102 
103 
104  template <typename T>
106  const std::initializer_list<std::size_t>& lower_bound,
107  const std::initializer_list<std::size_t>& upper_bound)
108  { return TensorConstMap<T>(Range(lower_bound, upper_bound), const_cast<const T*>(data)); }
109 
110  template <typename T, typename Range_>
111  inline TensorConstMap<T, std::decay_t<Range_>> make_const_map(T* const data, Range_ && range)
112  { return TensorConstMap<T, std::decay_t<Range_>>(std::forward<Range_>(range), const_cast<const T*>(data)); }
113 
115  template <typename T, typename Index>
116  inline void remap(TensorMap<T> &map, T* const data, const Index &lower_bound,
117  const Index &upper_bound)
118  {
119  map.range_.resize(lower_bound, upper_bound);
120  map.data_ = data;
121  }
122 
123  template <typename T, typename Index>
124  inline void remap(TensorConstMap<T>& map, T* const data, const Index& lower_bound,
125  const Index& upper_bound) {
126  map.range_.resize(lower_bound, upper_bound);
127  map.data_ = const_cast<const T*>(data);
128  }
129 
130  template <typename T>
131  inline void remap(TensorMap<T> &map, T* const data,
132  const std::initializer_list<std::size_t> &lower_bound,
133  const std::initializer_list<std::size_t> &upper_bound)
134  {
135  map.range_.resize(lower_bound, upper_bound);
136  map.data_ = data;
137  }
138 
139  template <typename T>
140  inline void remap(TensorConstMap<T>& map, T* const data,
141  const std::initializer_list<std::size_t>& lower_bound,
142  const std::initializer_list<std::size_t>& upper_bound) {
143  map.range_.resize(lower_bound, upper_bound);
144  map.data_ = const_cast<const T*>(data);
145  }
146 
147 } // namespace TiledArray
148 
149 #endif // TILEDARRAY_TENSOR_TENSOR_MAP_H__INCLUDED
A (hyperrectangular) interval on , space of integer n-indices.
Definition: range.h:39
auto data(T &t)
Container data pointer accessor.
Definition: utility.h:89
TensorMap< T > make_map(T *const data, const Index &lower_bound, const Index &upper_bound)
Definition: tensor_map.h:51
TensorConstMap< T > make_const_map(const T *const data, const Index &lower_bound, const Index &upper_bound)
Definition: tensor_map.h:83
Tensor interface for external data.
void remap(detail::TensorInterface< T, Range > &, T *const, const Index &, const Index &)
detail::TensorInterface< T, Range_ > TensorMap
Definition: tensor_map.h:44