permute.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2016 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  * permute.h
22  * Jan 10, 2016
23  *
24  */
25 
26 #ifndef TILEDARRAY_TILE_INTERFACE_PERMUTE_H__INCLUDED
27 #define TILEDARRAY_TILE_INTERFACE_PERMUTE_H__INCLUDED
28 
29 #include "../tensor/type_traits.h"
30 #include "../tile_interface/cast.h"
31 #include "../type_traits.h"
32 
33 namespace TiledArray {
34 
36 
42 template <
43  typename Arg, typename Perm,
44  typename = std::enable_if_t<detail::is_permutation_v<Perm> &&
45  detail::has_member_function_permute_anyreturn_v<
46  const Arg, const Perm&>>>
47 inline auto permute(const Arg& arg, const Perm& perm) {
48  return arg.permute(perm);
49 }
50 
51 template <typename>
52 struct permute_trait;
53 
54 namespace tile_interface {
55 
57 
58 template <typename T>
59 using result_of_permute_t = typename std::decay<decltype(
60  permute(std::declval<T>(), std::declval<Permutation>()))>::type;
61 
62 template <typename Tile, typename Enabler = void>
63 struct permute_trait {
64  typedef Tile type;
65 };
66 
67 template <typename Arg>
68 struct permute_trait<Arg, typename std::enable_if<TiledArray::detail::is_type<
69  result_of_permute_t<Arg>>::value>::type> {
71 };
72 
73 template <typename Result, typename Arg, typename Enabler = void>
74 class Permute {
75  public:
76  typedef Result result_type;
77  typedef Arg argument_type;
78 
79  template <typename Perm,
80  typename = std::enable_if_t<detail::is_permutation_v<Perm>>>
81  result_type operator()(const argument_type& arg, const Perm& perm) const {
82  using TiledArray::permute;
83  if constexpr (detail::is_bipartite_permutable_v<argument_type>) {
84  return permute(arg, perm);
85  } else {
86  TA_ASSERT(inner_size(perm));
87  return permute(arg, outer(perm));
88  }
89  }
90 };
91 
92 template <typename Result, typename Arg>
93 class Permute<Result, Arg,
94  typename std::enable_if<
95  !std::is_same<Result, result_of_permute_t<Arg>>::value>::type>
96  : public TiledArray::Cast<Result, result_of_permute_t<Arg>> {
97  private:
99 
100  public:
101  typedef Result result_type;
102  typedef Arg argument_type;
103 
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  using TiledArray::permute;
108  if constexpr (detail::is_bipartite_permutable_v<argument_type>) {
109  return Cast_::operator()(permute(arg, perm));
110  } else {
111  TA_ASSERT(inner_size(perm));
112  return Cast_::operator()(permute(arg, outer(perm)));
113  }
114  }
115 };
116 
117 } // namespace tile_interface
118 
120 
125 template <typename Arg>
127 
129 
133 template <typename Result, typename Arg>
134 class Permute : public TiledArray::tile_interface::Permute<Result, Arg> {};
135 
136 } // namespace TiledArray
137 
138 #endif // TILEDARRAY_TILE_INTERFACE_PERMUTE_H__INCLUDED
std::enable_if<!TiledArray::detail::is_permutation_v< Perm >, TiledArray::Range >::type permute(const TiledArray::Range &r, const Perm &p)
Definition: btas.h:803
Arg argument_type
Argument tile type.
Definition: permute.h:77
Permute trait.
Definition: permute.h:126
result_type operator()(const argument_type &arg, const Perm &perm) const
Definition: permute.h:81
auto outer(const Permutation &p)
Definition: permutation.h:820
Tile cast operation.
Definition: cast.h:168
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
auto inner_size(const Permutation &p)
Definition: permutation.h:822
Result result_type
Result tile type.
Definition: permute.h:76
Permute a tile.
Definition: permute.h:134
typename std::decay< decltype(permute(std::declval< T >(), std::declval< Permutation >()))>::type result_of_permute_t
Definition: permute.h:60
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82