TiledArray  0.7.0
utility.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  * utility.h
22  * Oct 18, 2013
23  *
24  */
25 
26 #ifndef TILEDARRAY_UTILITY_H__INCLUDED
27 #define TILEDARRAY_UTILITY_H__INCLUDED
28 
29 #include <TiledArray/madness.h>
30 #include <TiledArray/error.h>
31 #include <TiledArray/type_traits.h>
32 #include <iosfwd>
33 #include <vector>
34 #include <array>
35 #include <initializer_list>
36 
37 namespace TiledArray {
38  namespace detail {
39 
40 
42 
46  template <typename T, std::size_t N>
47  inline constexpr std::size_t size(T (&)[N]) { return N; }
48 
50 
54  template <typename T, std::size_t N>
55  inline constexpr std::size_t size(const std::array<T, N>&) { return N; }
56 
58 
62  template <typename T,
63  typename std::enable_if<! std::is_array<T>::value>::type* = nullptr>
64  inline auto size(const T &a){ return a.size(); }
65 
67 
71  template <typename T>
72  inline auto size(std::initializer_list<T> a) { return a.size(); }
73 
75 
79  template <typename ... Ts>
80  inline auto size(const std::tuple<Ts...>& a) { return std::tuple_size<std::tuple<Ts...>>::value; }
81 
83 
87  template <typename T,
88  typename std::enable_if<! std::is_pointer<T>::value>::type* = nullptr>
89  inline auto data(T& t)
90  { return t.data(); }
91 
92 
93 
95 
99  template <typename T,
100  typename std::enable_if<! std::is_pointer<T>::value>::type* = nullptr>
101  inline auto data(const T& t)
102  { return t.data(); }
103 
105 
109  template <typename T,
110  typename std::enable_if<std::is_pointer<T>::value>::type* = nullptr>
111  inline T data(T t) { return t; }
112 
114 
118  template <typename T, std::size_t N>
119  inline T* data(T (&a)[N]) { return a; }
120 
122 
126  template <typename T, std::size_t N>
127  inline const T* data(const T (&a)[N]) { return a; }
128 
129 
131 
135  template <typename T>
136  inline T* data(std::initializer_list<T>& l) { return l.begin(); }
137 
139 
143  template <typename T>
144  inline const T* data(const std::initializer_list<T>& l) { return l.begin(); }
145 
147 
152  template <typename A>
153  inline void print_array(std::ostream& out, const A& a, const std::size_t n) {
154  out << "[";
155  for(std::size_t i = 0; i < n; ++i) {
156  out << a[i];
157  if (i != (n - 1))
158  out << ",";
159  }
160  out << "]";
161  }
162 
164 
168  template <typename A>
169  inline void print_array(std::ostream& out, const A& a) {
170  print_array(out, a, size(a));
171  }
172 
173  } // namespace detail
174 } // namespace TiledArray
175 
176 namespace std {
177 
179  template <typename T, typename A>
180  inline std::ostream& operator<<(std::ostream& os, const std::vector<T, A>& vec) {
182  return os;
183  }
184 
185 } // namespace std
186 
187 #endif // TILEDARRAY_UTILITY_H__INCLUDED
auto data(T &t)
Container data pointer accessor.
Definition: utility.h:89
STL namespace.
constexpr std::size_t size(T(&)[N])
Array size accessor.
Definition: utility.h:47
void print_array(std::ostream &out, const A &a, const std::size_t n)
Print the content of an array like object.
Definition: utility.h:153