xgboost
row_set.h
Go to the documentation of this file.
1 
7 #ifndef XGBOOST_COMMON_ROW_SET_H_
8 #define XGBOOST_COMMON_ROW_SET_H_
9 
10 #include <xgboost/data.h>
11 #include <algorithm>
12 #include <vector>
13 #include <utility>
14 #include <memory>
15 
16 namespace xgboost {
17 namespace common {
18 
21  public:
25  struct Elem {
26  const size_t* begin{nullptr};
27  const size_t* end{nullptr};
29  // id of node associated with this instance set; -1 means uninitialized
30  Elem()
31  = default;
32  Elem(const size_t* begin,
33  const size_t* end,
34  bst_node_t node_id = -1)
35  : begin(begin), end(end), node_id(node_id) {}
36 
37  inline size_t Size() const {
38  return end - begin;
39  }
40  };
41  /* \brief specifies how to split a rowset into two */
42  struct Split {
43  std::vector<size_t> left;
44  std::vector<size_t> right;
45  };
46 
47  inline std::vector<Elem>::const_iterator begin() const { // NOLINT
48  return elem_of_each_node_.begin();
49  }
50 
51  inline std::vector<Elem>::const_iterator end() const { // NOLINT
52  return elem_of_each_node_.end();
53  }
54 
56  inline const Elem& operator[](unsigned node_id) const {
57  const Elem& e = elem_of_each_node_[node_id];
58  CHECK(e.begin != nullptr)
59  << "access element that is not in the set";
60  return e;
61  }
62 
64  inline Elem& operator[](unsigned node_id) {
65  Elem& e = elem_of_each_node_[node_id];
66  return e;
67  }
68 
69  // clear up things
70  inline void Clear() {
71  elem_of_each_node_.clear();
72  }
73  // initialize node id 0->everything
74  inline void Init() {
75  CHECK_EQ(elem_of_each_node_.size(), 0U);
76 
77  if (row_indices_.empty()) { // edge case: empty instance set
78  // assign arbitrary address here, to bypass nullptr check
79  // (nullptr usually indicates a nonexistent rowset, but we want to
80  // indicate a valid rowset that happens to have zero length and occupies
81  // the whole instance set)
82  // this is okay, as BuildHist will compute (end-begin) as the set size
83  const size_t* begin = reinterpret_cast<size_t*>(20);
84  const size_t* end = begin;
85  elem_of_each_node_.emplace_back(Elem(begin, end, 0));
86  return;
87  }
88 
89  const size_t* begin = dmlc::BeginPtr(row_indices_);
90  const size_t* end = dmlc::BeginPtr(row_indices_) + row_indices_.size();
91  elem_of_each_node_.emplace_back(Elem(begin, end, 0));
92  }
93 
94  std::vector<size_t>* Data() { return &row_indices_; }
95  // split rowset into two
96  inline void AddSplit(unsigned node_id,
97  unsigned left_node_id,
98  unsigned right_node_id,
99  size_t n_left,
100  size_t n_right) {
101  const Elem e = elem_of_each_node_[node_id];
102  CHECK(e.begin != nullptr);
103  size_t* all_begin = dmlc::BeginPtr(row_indices_);
104  size_t* begin = all_begin + (e.begin - all_begin);
105 
106  CHECK_EQ(n_left + n_right, e.Size());
107  CHECK_LE(begin + n_left, e.end);
108  CHECK_EQ(begin + n_left + n_right, e.end);
109 
110  if (left_node_id >= elem_of_each_node_.size()) {
111  elem_of_each_node_.resize(left_node_id + 1, Elem(nullptr, nullptr, -1));
112  }
113  if (right_node_id >= elem_of_each_node_.size()) {
114  elem_of_each_node_.resize(right_node_id + 1, Elem(nullptr, nullptr, -1));
115  }
116 
117  elem_of_each_node_[left_node_id] = Elem(begin, begin + n_left, left_node_id);
118  elem_of_each_node_[right_node_id] = Elem(begin + n_left, e.end, right_node_id);
119  elem_of_each_node_[node_id] = Elem(nullptr, nullptr, -1);
120  }
121 
122  private:
123  // stores the row indexes in the set
124  std::vector<size_t> row_indices_;
125  // vector: node_id -> elements
126  std::vector<Elem> elem_of_each_node_;
127 };
128 
129 } // namespace common
130 } // namespace xgboost
131 
132 #endif // XGBOOST_COMMON_ROW_SET_H_
xgboost::common::RowSetCollection::Split::right
std::vector< size_t > right
Definition: row_set.h:44
xgboost::common::RowSetCollection::end
std::vector< Elem >::const_iterator end() const
Definition: row_set.h:51
xgboost::common::RowSetCollection::operator[]
const Elem & operator[](unsigned node_id) const
return corresponding element set given the node_id
Definition: row_set.h:56
xgboost::common::RowSetCollection::Elem::Size
size_t Size() const
Definition: row_set.h:37
xgboost::common::RowSetCollection::Split
Definition: row_set.h:42
xgboost::common::RowSetCollection::Elem::Elem
Elem()=default
xgboost::common::RowSetCollection
collection of rowset
Definition: row_set.h:20
xgboost::common::RowSetCollection::begin
std::vector< Elem >::const_iterator begin() const
Definition: row_set.h:47
xgboost::common::RowSetCollection::Split::left
std::vector< size_t > left
Definition: row_set.h:43
xgboost::bst_node_t
int32_t bst_node_t
Type for tree node index.
Definition: base.h:132
xgboost::common::RowSetCollection::operator[]
Elem & operator[](unsigned node_id)
return corresponding element set given the node_id
Definition: row_set.h:64
xgboost::common::RowSetCollection::Elem::node_id
bst_node_t node_id
Definition: row_set.h:28
xgboost::common::RowSetCollection::Data
std::vector< size_t > * Data()
Definition: row_set.h:94
xgboost::common::RowSetCollection::AddSplit
void AddSplit(unsigned node_id, unsigned left_node_id, unsigned right_node_id, size_t n_left, size_t n_right)
Definition: row_set.h:96
xgboost::common::RowSetCollection::Elem::end
const size_t * end
Definition: row_set.h:27
data.h
The input data structure of xgboost.
xgboost::common::RowSetCollection::Clear
void Clear()
Definition: row_set.h:70
xgboost::common::RowSetCollection::Elem::begin
const size_t * begin
Definition: row_set.h:26
xgboost::common::RowSetCollection::Elem::Elem
Elem(const size_t *begin, const size_t *end, bst_node_t node_id=-1)
Definition: row_set.h:32
xgboost::common::RowSetCollection::Init
void Init()
Definition: row_set.h:74
xgboost::common::RowSetCollection::Elem
data structure to store an instance set, a subset of rows (instances) associated with a particular no...
Definition: row_set.h:25
xgboost
namespace of xgboost
Definition: base.h:110