TiledArray  0.7.0
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 
29 #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 
54  private:
55 
56  T* const tensor_;
57 
58  public:
59  // Compiler generated functions
60  ShiftWrapper() = delete;
61  ShiftWrapper(const ShiftWrapper&) = default;
62  ShiftWrapper(ShiftWrapper&&) = default;
63  ~ShiftWrapper() = default;
64  ShiftWrapper& operator=(const ShiftWrapper&) = delete;
65  ShiftWrapper& operator=(ShiftWrapper&&) = delete;
66 
67  ShiftWrapper(T& tensor) : tensor_(&tensor) { }
68 
70 
76  template <typename U>
77  ShiftWrapper<T>& operator=(U&& other) {
78  typedef typename std::decay<U>::type arg_type;
79  detail::inplace_tensor_op([] (reference MADNESS_RESTRICT l,
80  typename arg_type::const_reference MADNESS_RESTRICT r) { l = r; },
81  *this, other);
82  return *this;
83  }
84 
86 
88  T& get() const { return *tensor_; }
89 
91 
93 // operator T&() const { return get(); }
94 
96 
98  decltype(auto) range() const { return tensor_->range(); }
99 
101 
103  decltype(auto) data() const { return tensor_->data(); }
104 
106 
108  decltype(auto) empty() const { return tensor_->empty(); }
109 
110  }; // class ShiftWrapper
111 
112 
114 
121  template <typename Left, typename Right>
122  inline bool is_range_congruent(const Left& left, const ShiftWrapper<Right>& right) {
123  return (left.range().rank() == right.range().rank()) &&
124  std::equal(left.range().extent_data(),
125  left.range().extent_data() + left.range().rank(), right.range().extent_data());
126  }
127 
128  } // namespace detail
129 
131 
135  template <typename T>
137  return detail::ShiftWrapper<T>(tensor);
138  }
139 
140 
142 
146  template <typename T>
148  return detail::ShiftWrapper<const T>(tensor);
149  }
150 
151 } // namespace TiledArray
152 
153 #endif // TILEDARRAY_TENSOR_SHIFT_WRAPPER_H__INCLUDED
detail::ShiftWrapper< T > shift(T &tensor)
Shift a tensor from one range to another.
void inplace_tensor_op(Op &&op, TR &result, const Ts &... tensors)
In-place tensor operations with contiguous data.
Definition: kernels.h:163
decltype(auto) range() const
Tensor type conversion operator.
Definition: shift_wrapper.h:98
tensor_type::range_type range_type
Definition: shift_wrapper.h:47
bool is_range_congruent(const Left &left, const ShiftWrapper< Right > &right)
Check for congruent range objects with a shifted tensor.
std::remove_const< T >::type tensor_type
Definition: shift_wrapper.h:44
ShiftWrapper & operator=(const ShiftWrapper &)=delete
tensor_type::const_reference const_reference
Definition: shift_wrapper.h:49
tensor_type::value_type value_type
Definition: shift_wrapper.h:45
tensor_type::const_pointer const_pointer
Definition: shift_wrapper.h:51
tensor_type::reference reference
Definition: shift_wrapper.h:48
decltype(auto) data() const
Tensor data pointer accessor.
tensor_type::size_type size_type
Definition: shift_wrapper.h:46
ShiftWrapper< T > & operator=(U &&other)
Assignment operator.
Definition: shift_wrapper.h:77
decltype(auto) empty() const
Check for an empty tensor.
tensor_type::pointer pointer
Definition: shift_wrapper.h:50