ALPSCore reference
data_storage.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 ALPSCORE_GF_TENSORBASE_H
8 #define ALPSCORE_GF_TENSORBASE_H
9 
10 #include <vector>
11 
12 namespace alps {
13  namespace numerics {
14  namespace detail {
15 
16  // Forward declarations
17  template<typename T>
18  class data_view;
19 
26  template<typename T, typename Cont = std::vector<typename std::remove_const<T>::type> >
27  class data_storage {
28  private:
30  Cont data_;
31  public:
32 
34  template<typename T2, typename C2>
35  data_storage(const data_storage<T2, C2> & storage) : data_(storage.size()) {
36  std::copy(storage.data(), storage.data() + storage.size(), data());
37  };
38  template<typename T2, typename C2>
39  data_storage(data_storage<T2, C2> && storage) : data_(storage.size()) {
40  std::copy(storage.data(), storage.data() + storage.size(), data());
41  };
42 
44  data_storage(const data_storage<T, Cont>& rhs) : data_(rhs.data_) {};
46  data_storage(data_storage<T, Cont>&& rhs) : data_(rhs.data_) {};
48  data_storage<T, Cont>& operator=(const data_storage<T, Cont>& rhs) {
49  if(size() != rhs.size()) {
50  resize(rhs.size());
51  }
52  std::copy(rhs.data(), rhs.data() + rhs.size(), data());
53  return *this;
54  };
56  data_storage<T, Cont>& operator=(data_storage<T, Cont>&& rhs) {
57  if(size() != rhs.size()) {
58  resize(rhs.size());
59  }
60  std::copy(rhs.data(), rhs.data() + rhs.size(), data());
61  return *this;
62  };
63 
70  template<typename T2, typename C2>
71  data_storage<T, Cont>& operator=(const data_storage<T2, C2>& rhs) {
72  static_assert(std::is_convertible<T2, T>::value, "Can not perform assignment: T2 can not be cast into T");
73  if(size() != rhs.size()) {
74  resize(rhs.size());
75  }
76  std::copy(rhs.data(), rhs.data() + rhs.size(), data());
77  return *this;
78  };
80  template<typename T2>
81  data_storage(const data_view<T2> & view) : data_(view.size()) {
82  static_assert(std::is_convertible<T2, T>::value, "View type can not be converted into storage");
83  std::copy(view.data(), view.data() + view.size(), data());
84  }
86  template<typename T2>
87  data_storage(data_view<T2> && view) noexcept : data_(view.size()){
88  std::copy(view.data(), view.data() + view.size(), data());
89  };
91  data_storage(const T *data, size_t size) : data_(size) {
92  std::copy(data, data + size, this->data());
93  }
95  explicit data_storage(size_t size) : data_(size) {
96  std::fill(data(), data()+size, T(0));
97  }
98 
100  inline T& data(size_t i) {
101  return data_[i];
102  };
104  inline const T& data(size_t i) const {return data_[i];};
106  inline const T& operator()(size_t i) const {return data_[i];};
107  inline T& operator()(size_t i) {
108  return data_[i];
109  };
111  size_t size() const {return data_.size();}
113  const T* data() const {return data_.data();}
115  T* data() {return data_.data();}
117  void resize(size_t new_size) {
118  data_.resize(new_size);
119  }
120 
129  template<typename T2, typename C2>
130  bool operator==(const data_storage<T2, C2> &r) const {
131  return r.size() == size() && std::equal(r.data(), r.data() + r.size(), data());
132  }
133 
135  template<typename T2>
136  bool operator==(const data_view<T2>& r) const {
137  return r == *this;
138  };
139  };
140  }
141  template<typename T>
142  using simple_storage = detail::data_storage<T, std::vector<T> >;
143  }
144 }
145 
146 #endif //ALPSCORE_GF_TENSORBASE_H
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
bool operator==(const dictionary &lhs, const dictionary &rhs)
Definition: dictionary.hpp:96
detail::data_storage< T, std::vector< T > > simple_storage
T r(T x, T y=T(), T z=T())