mxnet
array_view.h
Go to the documentation of this file.
1 
6 #ifndef DMLC_ARRAY_VIEW_H_
7 #define DMLC_ARRAY_VIEW_H_
8 
9 #include <vector>
10 #include <array>
11 
12 namespace dmlc {
13 
35 template<typename ValueType>
36 class array_view {
37  public:
39  array_view() = default;
44  array_view(const array_view<ValueType> &other) = default; // NOLINT(*)
45 #ifndef _MSC_VER
46 
50  array_view(array_view<ValueType>&& other) = default; // NOLINT(*)
51 #else
52 
56  array_view(array_view<ValueType>&& other) { // NOLINT(*)
57  begin_ = other.begin_;
58  size_ = other.size_;
59  other.begin_ = nullptr;
60  }
61 #endif
62 
67  array_view<ValueType>& operator=(const array_view<ValueType>& other) = default; // NOLINT(*)
72  array_view(const std::vector<ValueType>& other) { // NOLINT(*)
73  if (other.size() != 0) {
74  begin_ = &other[0]; size_ = other.size();
75  }
76  }
81  template<std::size_t size>
82  array_view(const std::array<ValueType, size>& other) { // NOLINT(*)
83  if (size != 0) {
84  begin_ = &other[0]; size_ = size;
85  }
86  }
92  array_view(const ValueType* begin, const ValueType* end) {
93  if (begin < end) {
94  begin_ = begin;
95  size_ = end - begin;
96  }
97  }
99  inline size_t size() const {
100  return size_;
101  }
103  inline const ValueType* begin() const {
104  return begin_;
105  }
107  inline const ValueType* end() const {
108  return begin_ + size_;
109  }
115  inline const ValueType& operator[](size_t i) const {
116  return begin_[i];
117  }
118 
119  private:
121  const ValueType* begin_{nullptr};
123  size_t size_{0};
124 };
125 
126 } // namespace dmlc
127 
128 #endif // DMLC_ARRAY_VIEW_H_
const ValueType * begin() const
Definition: array_view.h:103
Read only data structure to reference continuous memory region of array. Provide unified view for vec...
Definition: array_view.h:36
size_t size() const
Definition: array_view.h:99
array_view(const ValueType *begin, const ValueType *end)
construct array view from continuous segment
Definition: array_view.h:92
array_view(const std::vector< ValueType > &other)
construct array view std::vector
Definition: array_view.h:72
namespace for dmlc
Definition: array_view.h:12
const ValueType & operator[](size_t i) const
get i-th element from the view
Definition: array_view.h:115
array_view()=default
default constructor
const ValueType * end() const
Definition: array_view.h:107
array_view(const std::array< ValueType, size > &other)
construct array std::array
Definition: array_view.h:82
array_view< ValueType > & operator=(const array_view< ValueType > &other)=default
default assign constructor