Debian Codegraph @

src/dmlc-core/include/dmlc/array_view.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2016 by Contributors
3 * \file array_view.h
4 * \brief Read only data structure to reference array
5 */
6 #ifndef DMLC_ARRAY_VIEW_H_
7 #define DMLC_ARRAY_VIEW_H_
8
9 #include <array>
10 #include <cstddef>
11 #include <vector>
12
13 namespace dmlc {
14
15 /*!
16 * \brief Read only data structure to reference continuous memory region of array.
17 * Provide unified view for vector, array and C style array.
18 * This data structure do not guarantee aliveness of referenced array.
19 *
20 * Make sure do not use array_view to record data in async function closures.
21 * Also do not use array_view to create reference to temporary data structure.
22 *
23 * \tparam ValueType The value
24 *
25 * \code
26 * std::vector<int> myvec{1,2,3};
27 * dmlc::array_view<int> view(myvec);
28 * // indexed visit to the view.
29 * LOG(INFO) << view[0];
30 *
31 * for (int v : view) {
32 * // visit each element in the view
33 * }
34 * \endcode
35 */
36 template <typename ValueType>
37 class array_view {
38 public:
39 /*! \brief default constructor */
40 array_view() = default;
41 /*!
42 * \brief default copy constructor
43 * \param other another array view.
44 */
45 array_view(const array_view<ValueType> &other) = default; // NOLINT(*)
46 #ifndef _MSC_VER
47 /*!
48 * \brief default move constructor
49 * \param other another array view.
50 */
51 array_view(array_view<ValueType> &&other) = default; // NOLINT(*)
52 #else
53 /*!
54 * \brief default move constructor
55 * \param other another array view.
56 */
57 array_view(array_view<ValueType> &&other) { // NOLINT(*)
58 begin_ = other.begin_;
59 size_ = other.size_;
60 other.begin_ = nullptr;
61 }
62 #endif
63 /*!
64 * \brief default assign constructor
65 * \param other another array view.
66 * \return self.
67 */
68 array_view<ValueType> &operator=(const array_view<ValueType> &other) = default; // NOLINT(*)
69 /*!
70 * \brief construct array view std::vector
71 * \param other vector container
72 */
73 array_view(const std::vector<ValueType> &other) { // NOLINT(*)
74 if (other.size() != 0) {
75 begin_ = &other[0];
76 size_ = other.size();
77 }
78 }
79 /*!
80 * \brief construct array std::array
81 * \param other another array view.
82 */
83 template <std::size_t size>
84 array_view(const std::array<ValueType, size> &other) { // NOLINT(*)
85 if (size != 0) {
86 begin_ = &other[0];
87 size_ = size;
88 }
89 }
90 /*!
91 * \brief construct array view from continuous segment
92 * \param begin beginning pointre
93 * \param end end pointer
94 */
95 array_view(const ValueType *begin, const ValueType *end) {
96 if (begin < end) {
97 begin_ = begin;
98 size_ = end - begin;
99 }
100 }
101 /*! \return size of the array */
102 inline size_t size() const {
103 return size_;
104 }
105 /*! \return begin of the array */
106 inline const ValueType *begin() const {
107 return begin_;
108 }
109 /*! \return end point of the array */
110 inline const ValueType *end() const {
111 return begin_ + size_;
112 }
113 /*!
114 * \brief get i-th element from the view
115 * \param i The index.
116 * \return const reference to i-th element.
117 */
118 inline const ValueType &operator[](size_t i) const {
119 return begin_[i];
120 }
121
122 private:
123 /*! \brief the begin of the view */
124 const ValueType *begin_{nullptr};
125 /*! \brief The size of the view */
126 size_t size_{0};
127 };
128
129 } // namespace dmlc
130
131 #endif // DMLC_ARRAY_VIEW_H_