ALPSCore reference
data_view.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_TENSORVIEW_H
8 #define ALPSCORE_GF_TENSORVIEW_H
9 
10 
11 
12 #include <vector>
13 #include <array>
14 #include "data_storage.hpp"
15 #include "view.hpp"
16 
17 namespace alps {
18  namespace numerics {
19  namespace detail {
20 
26  template<typename T>
27  class data_view {
28  private:
30  view<T> data_slice_;
32  size_t size_;
33  public:
35  data_view(data_storage<T> & storage) : data_slice_(storage), size_(storage.size()) {}
36  data_view(const data_storage<T> & storage) : data_slice_(storage.data(), storage.size()), size_(storage.size()) {}
37  template<typename S>
38  data_view(const data_storage<S> & storage, size_t size, size_t offset = 0) : data_slice_(storage.data() + offset, size), size_(size) {}
40  data_view(data_storage<T> & storage, size_t size, size_t offset = 0) : data_slice_(storage.data() + offset, size), size_(size) {}
42  data_view(data_view<T> && storage, size_t size, size_t offset) : data_slice_(storage.data_slice_.data() + offset, size), size_(size) {}
44  data_view(const data_view<T> & storage, size_t size, size_t offset) : data_slice_(storage.data_slice_.data() + offset, size), size_(size) {}
46  data_view(T*data, size_t size) : data_slice_(data, size), size_(size){}
48  data_view(const data_view<T> & storage) = default;
49 
50  template<typename S>
51  data_view(const data_view<S>& storage, size_t size = 0, size_t offset = 0) :
52  data_slice_(&storage.data()[offset], size), size_(size) {};
53 
54 
56  data_view(data_view<T> && storage) = default;
57 
59  data_view<T>& operator=(const data_view<T>& rhs) = default;
61  data_view<T>& operator=(data_view<T>&& rhs) = default;
62 
64  T& data(size_t i) {return data_slice_.data(i);};
66  const T& data(size_t i) const {return data_slice_.data(i);};
68  inline const T& operator()(size_t i) const {return data_slice_.data(i);};
69  inline T& operator()(size_t i) {return data_slice_.data(i);};
71  size_t size() const {return size_;}
73  T* data() {return data_slice_.data();}
75  const T* data() const {return data_slice_.data();}
76 
78  template<typename T2>
79  bool operator==(const data_view<T2>& r) const {
80  return size() == r.size() && std::equal(r.data(), r.data() + r.size(), data());
81  }
82 
84  template<typename T2>
85  bool operator==(const data_storage<T2>& r) const {
86  return size() == r.size() && std::equal(r.data(), r.data() + r.size(), data());
87  }
88  };
89  }
90  }
91 }
92 #endif //ALPSCORE_GF_TENSORVIEW_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
T r(T x, T y=T(), T z=T())