shift_wrapper.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  * shift_wrapper.h
22  * May 31, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_TENSOR_SHIFT_WRAPPER_H__INCLUDED
27 #define TILEDARRAY_TENSOR_SHIFT_WRAPPER_H__INCLUDED
28 
30 #include <TiledArray/type_traits.h>
31 
32 namespace TiledArray {
33 namespace detail {
34 
36 
40 template <typename T>
41 class ShiftWrapper {
42  public:
44  typedef typename std::remove_const<T>::type tensor_type;
45  typedef typename tensor_type::value_type value_type;
46  typedef typename tensor_type::size_type size_type;
47  typedef typename tensor_type::range_type range_type;
48  typedef typename tensor_type::reference reference;
49  typedef typename tensor_type::const_reference const_reference;
50  typedef typename tensor_type::pointer pointer;
51  typedef typename tensor_type::const_pointer const_pointer;
52 
53  private:
54  T* const tensor_;
55 
56  public:
57  // Compiler generated functions
58  ShiftWrapper() = delete;
59  ShiftWrapper(const ShiftWrapper&) = default;
60  ShiftWrapper(ShiftWrapper&&) = default;
61  ~ShiftWrapper() = default;
62  ShiftWrapper& operator=(const ShiftWrapper&) = delete;
64 
65  ShiftWrapper(T& tensor) : tensor_(&tensor) {}
66 
68 
74  template <typename U>
75  ShiftWrapper<T>& operator=(U&& other) {
76  typedef typename std::decay<U>::type arg_type;
78  [](reference MADNESS_RESTRICT l,
79  typename arg_type::const_reference MADNESS_RESTRICT r) { l = r; },
80  *this, other);
81  return *this;
82  }
83 
85 
87  T& get() const { return *tensor_; }
88 
90 
92  // operator T&() const { return get(); }
93 
95 
97  decltype(auto) range() const { return tensor_->range(); }
98 
100 
102  decltype(auto) data() const { return tensor_->data(); }
103 
105 
107  decltype(auto) empty() const { return tensor_->empty(); }
108 
109 }; // class ShiftWrapper
110 
112 
119 template <typename Left, typename Right>
120 inline bool is_range_congruent(const Left& left,
121  const ShiftWrapper<Right>& right) {
122  return (left.range().rank() == right.range().rank()) &&
123  std::equal(left.range().extent_data(),
124  left.range().extent_data() + left.range().rank(),
125  right.range().extent_data());
126 }
127 
128 } // namespace detail
129 
131 
135 template <typename T>
137  return detail::ShiftWrapper<T>(tensor);
138 }
139 
141 
145 template <typename T>
147  return detail::ShiftWrapper<const T>(tensor);
148 }
149 
150 } // namespace TiledArray
151 
152 #endif // TILEDARRAY_TENSOR_SHIFT_WRAPPER_H__INCLUDED
tensor_type::range_type range_type
Definition: shift_wrapper.h:47
tensor_type::const_reference const_reference
Definition: shift_wrapper.h:49
bool is_range_congruent(const Left &left, const ShiftWrapper< Right > &right)
Check for congruent range objects with a shifted tensor.
detail::ShiftWrapper< T > shift(T &tensor)
Shift a tensor from one range to another.
ShiftWrapper(ShiftWrapper &&)=default
tensor_type::const_pointer const_pointer
Definition: shift_wrapper.h:51
decltype(auto) data() const
Tensor data pointer accessor.
ShiftWrapper & operator=(ShiftWrapper &&)=delete
tensor_type::reference reference
Definition: shift_wrapper.h:48
decltype(auto) range() const
Tensor type conversion operator.
Definition: shift_wrapper.h:97
ShiftWrapper(const ShiftWrapper &)=default
T & get() const
Tensor accessor.
Definition: shift_wrapper.h:87
std::remove_const< T >::type tensor_type
Definition: shift_wrapper.h:44
tensor_type::value_type value_type
Definition: shift_wrapper.h:45
tensor_type::size_type size_type
Definition: shift_wrapper.h:46
ShiftWrapper< T > & operator=(U &&other)
Assignment operator.
Definition: shift_wrapper.h:75
void inplace_tensor_op(Op &&op, TR &result, const Ts &... tensors)
In-place tensor operations with contiguous data.
Definition: kernels.h:197
tensor_type::pointer pointer
Definition: shift_wrapper.h:50
decltype(auto) empty() const
Check for an empty tensor.
ShiftWrapper & operator=(const ShiftWrapper &)=delete