xgboost
bitmap.h
Go to the documentation of this file.
1 
8 #ifndef XGBOOST_COMMON_BITMAP_H_
9 #define XGBOOST_COMMON_BITMAP_H_
10 
11 #include <dmlc/omp.h>
12 #include <vector>
13 
14 namespace xgboost {
15 namespace common {
17 struct BitMap {
19  std::vector<uint32_t> data;
24  inline void Resize(size_t size) {
25  data.resize((size + 31U) >> 5, 0);
26  }
31  inline bool Get(size_t i) const {
32  return (data[i >> 5] >> (i & 31U)) & 1U;
33  }
38  inline void SetTrue(size_t i) {
39  data[i >> 5] |= (1 << (i & 31U));
40  }
42  inline void InitFromBool(const std::vector<int>& vec) {
43  this->Resize(vec.size());
44  // parallel over the full cases
45  auto nsize = static_cast<bst_omp_uint>(vec.size() / 32);
46  #pragma omp parallel for schedule(static)
47  for (bst_omp_uint i = 0; i < nsize; ++i) {
48  uint32_t res = 0;
49  for (int k = 0; k < 32; ++k) {
50  uint32_t bit = vec[(i << 5) | k];
51  res |= (bit << k);
52  }
53  data[i] = res;
54  }
55  if (nsize != vec.size()) data.back() = 0;
56  for (size_t i = nsize; i < vec.size(); ++i) {
57  if (vec[i]) this->SetTrue(i);
58  }
59  }
61  inline void Clear() {
62  std::fill(data.begin(), data.end(), 0U);
63  }
64 };
65 } // namespace common
66 } // namespace xgboost
67 #endif // XGBOOST_COMMON_BITMAP_H_
dmlc::omp_uint bst_omp_uint
define unsigned int for openmp loop
Definition: base.h:246
void Clear()
clear the bitmap, set all places to false
Definition: bitmap.h:61
namespace of xgboost
Definition: base.h:102
bool Get(size_t i) const
query the i-th position of bitmap
Definition: bitmap.h:31
std::vector< uint32_t > data
internal data structure
Definition: bitmap.h:19
void SetTrue(size_t i)
set i-th position to true
Definition: bitmap.h:38
void Resize(size_t size)
resize the bitmap to be certain size
Definition: bitmap.h:24
bit map that contains set of bit indicators
Definition: bitmap.h:17
void InitFromBool(const std::vector< int > &vec)
initialize the value of bit map from vector of bool
Definition: bitmap.h:42