scal.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 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  * scal.h
22  * June 20, 2013
23  *
24  */
25 
26 #ifndef TILEDARRAY_TILE_OP_SCAL_H__INCLUDED
27 #define TILEDARRAY_TILE_OP_SCAL_H__INCLUDED
28 
30 #include <type_traits>
31 #include "../tile_interface/scale.h"
32 
33 namespace TiledArray {
34 namespace detail {
35 
37 
45 template <typename Result, typename Arg, typename Scalar, bool Consumable>
46 class Scal {
47  public:
49  typedef Arg argument_type;
50  typedef Scalar scalar_type;
51  typedef Result result_type;
52 
53  static constexpr bool is_consumable =
54  Consumable && std::is_same<result_type, argument_type>::value;
55 
56  private:
57  scalar_type factor_;
58 
59  // Permuting tile evaluation function
60  // These operations cannot consume the argument tile since this operation
61  // requires temporary storage space.
62  template <typename Perm,
63  typename = std::enable_if_t<detail::is_permutation_v<Perm>>>
64  result_type eval(const Arg& arg, const Perm& perm) const {
65  using TiledArray::scale;
66  return scale(arg, factor_, perm);
67  }
68 
69  // Non-permuting tile evaluation functions
70  // The compiler will select the correct functions based on the
71  // consumability of the arguments.
72 
73  template <bool C, typename std::enable_if<!C>::type* = nullptr>
74  result_type eval(const argument_type& arg) const {
75  using TiledArray::scale;
76  return scale(arg, factor_);
77  }
78 
79  template <bool C, typename std::enable_if<C>::type* = nullptr>
80  result_type eval(argument_type& arg) const {
82  return scale_to(arg, factor_);
83  }
84 
85  public:
86  // Compiler generated functions
87  Scal(const Scal_&) = default;
88  Scal(Scal_&&) = default;
89  ~Scal() = default;
90  Scal_& operator=(const Scal_&) = default;
91  Scal_& operator=(Scal_&&) = default;
92 
94 
97  explicit Scal(const scalar_type factor) : factor_(factor) {}
98 
100 
104  template <typename Perm,
105  typename = std::enable_if_t<detail::is_permutation_v<Perm>>>
106  result_type operator()(const argument_type& arg, const Perm& perm) const {
107  return eval(arg, perm);
108  }
109 
111 
115  template <typename A>
116  result_type operator()(A&& arg) const {
117  return Scal_::template eval<is_consumable>(std::forward<A>(arg));
118  }
119 
121 
125  constexpr bool can_consume =
127  std::is_same<result_type, argument_type>::value;
128  return Scal_::template eval<can_consume>(arg);
129  }
130 
131 }; // class Scal
132 
133 } // namespace detail
134 } // namespace TiledArray
135 
136 #endif // TILEDARRAY_TILE_OP_SCAL_H__INCLUDED
result_type operator()(A &&arg) const
Consuming scale operation.
Definition: scal.h:116
Tile< Result > & scale_to(Tile< Result > &result, const Scalar factor)
Scale to the result tile.
Definition: tile.h:1204
Consumable tile type trait.
Definition: type_traits.h:611
Scal(Scal_ &&)=default
Scal< Result, Arg, Scalar, Consumable > Scal_
This object type.
Definition: scal.h:48
static constexpr bool is_consumable
Definition: scal.h:53
Scal_ & operator=(Scal_ &&)=default
Scal_ & operator=(const Scal_ &)=default
Scal(const Scal_ &)=default
Scalar scalar_type
The scaling factor type.
Definition: scal.h:50
result_type consume(argument_type &arg) const
Explicit consuming scale operation.
Definition: scal.h:124
result_type operator()(const argument_type &arg, const Perm &perm) const
Scale and permute operator.
Definition: scal.h:106
Scal(const scalar_type factor)
Constructor.
Definition: scal.h:97
decltype(auto) scale(const Tile< Arg > &arg, const Scalar factor)
Scalar the tile argument.
Definition: tile.h:1174
Result result_type
The result tile type.
Definition: scal.h:51
Arg argument_type
The argument type.
Definition: scal.h:49
Tile scaling operation.
Definition: scal.h:46