ALPSCore reference
check_size.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #ifndef ALPS_NUMERIC_CHECK_SIZE_HEADER
8 #define ALPS_NUMERIC_CHECK_SIZE_HEADER
9 
12 
13 #include <boost/array.hpp>
14 
15 #include <type_traits>
16 #include <vector>
17 #include <stdexcept>
18 #include <algorithm>
19 
20 namespace alps {
21  namespace numeric {
22 
23  namespace detail {
24 
25  template <class X, class Y>
26  inline typename std::enable_if<!(is_sequence<X>::value || is_sequence<Y>::value),void>::type
27  resize_same_as(X&, const Y&) {}
28 
29  template <class X, class Y>
30  inline typename std::enable_if<is_sequence<X>::value && is_sequence<Y>::value,void>::type
31  resize_same_as(X& a, const Y& y) {
32  a.resize(y.size());
33  }
34 
35  template<typename T, typename U, std::size_t N>
36  inline void resize_same_as(boost::array<T, N> & a, boost::array<U, N> const & y) {}
37  }
38 
39  template<typename T, typename U>
40  inline void check_size(T & /*a*/, U const & /*b*/) {}
41 
42  template<typename T, typename U>
43  inline void check_size(std::vector<T> & a, std::vector<U> const & b) {
44  if(a.size() == 0)
45  detail::resize_same_as(a, b);
46  else if(a.size() != b.size())
47  boost::throw_exception(std::runtime_error("vectors must have the same size!" + ALPS_STACKTRACE));
48  }
49 
50  template<typename T, typename U, std::size_t N, std::size_t M>
51  inline void check_size(boost::array<T, N> & a, boost::array<U, M> const & b) {
52  boost::throw_exception(std::runtime_error("boost::array s must have the same size!" + ALPS_STACKTRACE));
53  }
54 
55  template<typename T, typename U, std::size_t N>
56  inline void check_size(boost::array<T, N> & a, boost::array<U, N> const & b) {}
57 
58  }
59 }
60 
61 #endif
void check_size(T &, U const &)
Definition: check_size.hpp:40
static constexpr bool value
#define ALPS_STACKTRACE
Definition: stacktrace.hpp:37