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>
34 
35 namespace TiledArray {
36 
37 namespace detail {
38 
39 template <typename, typename, typename>
40 class TensorInterface;
41 
42 } // namespace detail
43 
44 template <typename T, typename Range_ = Range, typename OpResult = Tensor<T>>
46 
47 template <typename T, typename Range_ = Range, typename OpResult = Tensor<T>>
50 
51 template <typename T, typename Range_ = Range, typename OpResult = Tensor<T>,
52  typename Index>
54  const Index& lower_bound,
55  const Index& upper_bound) {
56  return TensorMap<T, Range_, OpResult>(Range_(lower_bound, upper_bound), data);
57 }
58 
59 template <typename T, typename Range_ = Range, typename OpResult = Tensor<T>>
61  T* const data, const std::initializer_list<std::size_t>& lower_bound,
62  const std::initializer_list<std::size_t>& upper_bound) {
63  return TensorMap<T, Range_, OpResult>(Range_(lower_bound, upper_bound), data);
64 }
65 
66 template <typename T, typename Range_, typename OpResult = Tensor<T>>
67 inline TensorMap<T, std::decay_t<Range_>, OpResult> make_map(T* const data,
68  Range_&& range) {
69  return TensorMap<T, std::decay_t<Range_>>(std::forward<Range_>(range), data);
70 }
71 
72 template <typename T, typename Range_ = Range, typename OpResult = Tensor<T>,
73  typename Index>
74 inline TensorConstMap<T, Range_, OpResult> make_map(const T* const data,
75  const Index& lower_bound,
76  const Index& upper_bound) {
77  return TensorConstMap<T, Range_, OpResult>(Range_(lower_bound, upper_bound),
78  data);
79 }
80 
81 template <typename T, typename Range_ = Range, typename OpResult = Tensor<T>>
83  const T* const data, const std::initializer_list<std::size_t>& lower_bound,
84  const std::initializer_list<std::size_t>& upper_bound) {
85  return TensorConstMap<T, Range_, OpResult>(Range_(lower_bound, upper_bound),
86  data);
87 }
88 
89 template <typename T, typename Range_, typename OpResult = Tensor<T>>
91  const T* const data, Range_&& range) {
93  std::forward<Range_>(range), data);
94 }
95 
96 template <typename T, typename Range_ = Range, typename OpResult = Tensor<T>,
97  typename Index>
99  const T* const data, const Index& lower_bound, const Index& upper_bound) {
100  return TensorConstMap<T, Range_, OpResult>(Range_(lower_bound, upper_bound),
101  data);
102 }
103 
104 template <typename T, typename Range_ = Range, typename OpResult = Tensor<T>>
106  const T* const data, const std::initializer_list<std::size_t>& lower_bound,
107  const std::initializer_list<std::size_t>& upper_bound) {
108  return TensorConstMap<T, Range_, OpResult>(Range_(lower_bound, upper_bound),
109  data);
110 }
111 
112 template <typename T, typename Range_, typename OpResult = Tensor<T>>
114  const T* const data, Range_&& range) {
115  return TensorConstMap<T, std::decay_t<Range_>, OpResult>(
116  std::forward<Range_>(range), data);
117 }
118 
119 template <typename T, typename Range_, typename OpResult = Tensor<T>,
120  typename Index>
122  T* const data, const Index& lower_bound, const Index& upper_bound) {
123  return TensorConstMap<T, Range_, OpResult>(Range_(lower_bound, upper_bound),
124  const_cast<const T*>(data));
125 }
126 
127 template <typename T, typename Range_, typename OpResult = Tensor<T>>
129  T* const data, const std::initializer_list<std::size_t>& lower_bound,
130  const std::initializer_list<std::size_t>& upper_bound) {
131  return TensorConstMap<T, Range_, OpResult>(Range_(lower_bound, upper_bound),
132  const_cast<const T*>(data));
133 }
134 
135 template <typename T, typename Range_, typename OpResult = Tensor<T>>
137  T* const data, Range_&& range) {
138  return TensorConstMap<T, std::decay_t<Range_>, OpResult>(
139  std::forward<Range_>(range), const_cast<const T*>(data));
140 }
141 
143 template <typename T, typename Range_, typename OpResult, typename Index1,
144  typename Index2>
145 inline void remap(TensorMap<T, Range_, OpResult>& map, T* const data,
146  const Index1& lower_bound, const Index2& upper_bound) {
147  map.range_.resize(lower_bound, upper_bound);
148  map.data_ = data;
149 }
150 
151 template <typename T, typename Range_, typename OpResult, typename Index1,
152  typename Index2,
153  typename = std::enable_if_t<!std::is_const<T>::value>>
154 inline void remap(TensorConstMap<T, Range_, OpResult>& map, T* const data,
155  const Index1& lower_bound, const Index2& upper_bound) {
156  map.range_.resize(lower_bound, upper_bound);
157  map.data_ = const_cast<const T*>(data);
158 }
159 
160 template <typename T, typename Range_, typename OpResult, typename Index1,
161  typename Index2>
162 inline void remap(TensorMap<T, Range_, OpResult>& map, T* const data,
163  const std::initializer_list<Index1>& lower_bound,
164  const std::initializer_list<Index2>& upper_bound) {
165  map.range_.resize(lower_bound, upper_bound);
166  map.data_ = data;
167 }
168 
169 template <typename T, typename Range_, typename OpResult, typename Index1,
170  typename Index2,
171  typename = std::enable_if_t<!std::is_const<T>::value>>
172 inline void remap(TensorConstMap<T, Range_, OpResult>& map, T* const data,
173  const std::initializer_list<Index1>& lower_bound,
174  const std::initializer_list<Index2>& upper_bound) {
175  map.range_.resize(lower_bound, upper_bound);
176  map.data_ = const_cast<const T*>(data);
177 }
178 
179 } // namespace TiledArray
180 
181 #endif // TILEDARRAY_TENSOR_TENSOR_MAP_H__INCLUDED
TensorConstMap< T, Range_, OpResult > make_const_map(const T *const data, const Index &lower_bound, const Index &upper_bound)
Definition: tensor_map.h:98
TensorMap< T, Range_, OpResult > make_map(T *const data, const Index &lower_bound, const Index &upper_bound)
Definition: tensor_map.h:53
Tensor interface for external data.
void remap(detail::TensorInterface< T, Range_, OpResult > &, T *const, const Index1 &, const Index2 &)
Definition: tensor_map.h:154