vector_view.h 4.38 KB
Newer Older
Minjie Wang's avatar
Minjie Wang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// vector view
#ifndef DGL_VECTOR_VIEW_H_
#define DGL_VECTOR_VIEW_H_

#include <vector>
#include <memory>
#include <stdint.h>

#include <dmlc/logging.h>

namespace dgl {

/*!
 * \brief A vector-like data structure that can be either a vector or a read-only view.
 */
template<typename ValueType>
class vector_view {
 public:
Minjie Wang's avatar
impl  
Minjie Wang committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  /*! \brief iterator class */
  class iterator : public std::iterator<std::forward_iterator_tag, ValueType> {
   public:
    /*! \brief iterator constructor */
    iterator(const vector_view<ValueType>* vec, size_t pos): vec_(vec), pos_(pos) {}
    /*! \brief move to next */
    iterator& operator++() {
      ++pos_;
      return *this;
    }
    /*! \brief move to next */
    iterator operator++(int) {
      iterator retval = *this;
      ++(*this);
      return retval;
    }
    /*! \brief equal operator */
    bool operator==(iterator other) const {
      return vec_ == other.vec_ and pos_ == other.pos_;
    }
    /*! \brief not equal operator */
    bool operator!=(iterator other) const {
      return !(*this == other);
    }
    /*! \brief dereference operator */
    const ValueType& operator*() const {
      return (*vec_)[pos_];
    }
   private:
    /*! \brief vector_view pointer */
    const vector_view<ValueType>* vec_;
    /*! \brief current position */
    size_t pos_;
  };

Minjie Wang's avatar
Minjie Wang committed
54
55
56
57
58
59
60
61
62
  /*! \brief Default constructor. Create an empty vector. */
  vector_view()
    : data_(std::make_shared<std::vector<ValueType> >()) {}

  /*! \brief Construct a new vector view that shares the data with the given vec. */
  vector_view(const vector_view<ValueType>& vec, const std::vector<uint64_t>& index)
    : data_(vec.data_), index_(index), is_view_(true) {}

  /*! \brief constructor from a vector pointer */
Minjie Wang's avatar
impl  
Minjie Wang committed
63
  explicit vector_view(const std::shared_ptr<std::vector<ValueType> >& other)
Minjie Wang's avatar
Minjie Wang committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    : data_(other) {}

  /*! \brief default copy constructor */
  vector_view(const vector_view<ValueType>& other) = default;

#ifndef _MSC_VER
  /*! \brief default move constructor */
  vector_view(vector_view<ValueType>&& other) = default;
#else
  /*! \brief default move constructor */
  vector_view(vector_view<ValueType>&& other) {
    data_ = other.data_;
    index_ = other.index_;
    is_view_ = other.is_view_;
    other.data_ = std::make_shared<std::vector<ValueType> >();
    other.index_ = std::vector<uint64_t>();
    other.is_view_ = false;
  }
#endif  // _MSC_VER

  /*! \brief default destructor */
  ~vector_view() = default;

  /*! \brief default assign constructor */
Minjie Wang's avatar
impl  
Minjie Wang committed
88
  vector_view<ValueType>& operator=(const vector_view<ValueType>& other) = default;
Minjie Wang's avatar
Minjie Wang committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

  /*! \return the size of the vector */
  size_t size() const {
    if (is_view_) {
      return index_.size();
    } else {
      return data_->size();
    }
  }

  /*!
   * \brief Return the i^th element of the vector.
   * \param i The index
   * \return reference to the i^th element
   */
  ValueType& operator[](size_t i) {
    CHECK(!is_view_);
Minjie Wang's avatar
Minjie Wang committed
106
    return (*data_)[i];
Minjie Wang's avatar
Minjie Wang committed
107
108
109
110
111
112
113
114
115
  }

  /*!
   * \brief Return the i^th element of the vector.
   * \param i The index
   * \return reference to the i^th element
   */
  const ValueType& operator[](size_t i) const {
    if (is_view_) {
Minjie Wang's avatar
Minjie Wang committed
116
      return (*data_)[index_[i]];
Minjie Wang's avatar
Minjie Wang committed
117
    } else {
Minjie Wang's avatar
Minjie Wang committed
118
      return (*data_)[i];
Minjie Wang's avatar
Minjie Wang committed
119
120
121
    }
  }

Minjie Wang's avatar
impl  
Minjie Wang committed
122
123
124
125
  /*! \return an iterator pointing at the first element */
  iterator begin() const {
    return iterator(this, 0);
  }
Minjie Wang's avatar
Minjie Wang committed
126

Minjie Wang's avatar
impl  
Minjie Wang committed
127
128
129
130
  /*! \return an iterator pointing at the last element */
  iterator end() const {
    return iterator(this, size());
  }
Minjie Wang's avatar
Minjie Wang committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

  // Modifiers
  // NOTE: The modifiers are not allowed for view.
  /*!
   * \brief Add element at the end.
   * \param val The value
   */
  void push_back(const ValueType& val) {
    CHECK(!is_view_);
    data_->push_back(val);
  }

  /*!
   * \brief Clear the vector.
   */
  void clear() {
    CHECK(!is_view_);
    data_ = std::make_shared<std::vector<ValueType> >();
  }

Minjie Wang's avatar
impl  
Minjie Wang committed
151
152
153
154
155
156
157
158
159
160
161
  /*! \brief Resize the vector */
  void resize(size_t size) {
    CHECK(!is_view_);
    data_->resize(size);
  }

  /*! \brief Resize the vector with init value */
  void resize(size_t size, const ValueType& val) {
    CHECK(!is_view_);
    data_->resize(size, val);
  }
Minjie Wang's avatar
Minjie Wang committed
162
163
164
165
166
167
168
169
170
171
172
173
174

 private:
  /*! \brief pointer to the underlying vector data */
  std::shared_ptr<std::vector<ValueType> > data_;
  /*! \brief index used to access the data vector */
  std::vector<uint64_t> index_;
  /*! \brief whether this is a view or a vector */
  bool is_view_{false};
};

}  // namespace dgl

#endif  // DGL_VECTOR_VIEW_H_