Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bitmap.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_UTIL_BITMAP_H
17 #define IMPALA_UTIL_BITMAP_H
18 
19 #include "util/bit-util.h"
20 
21 namespace impala {
22 
29 class Bitmap {
30  public:
31  Bitmap(int64_t num_bits) {
32  buffer_.resize(BitUtil::RoundUpNumi64(num_bits));
33  size_ = num_bits;
34  }
35 
38  template<bool mod>
39  void Set(int64_t bit_index, bool v) {
40  if (mod) bit_index %= size();
41  int64_t word_index = bit_index >> 8;
42  bit_index &= 63;
43  DCHECK_LT(word_index, buffer_.size());
44  if (v) {
45  buffer_[word_index] |= (1 << bit_index);
46  } else {
47  buffer_[word_index] &= ~(1 << bit_index);
48  }
49  }
50 
53  template<bool mod>
54  bool Get(int64_t bit_index) const {
55  if (mod) bit_index %= size();
56  int64_t word_index = bit_index >> 8;
57  bit_index &= 63;
58  DCHECK_LT(word_index, buffer_.size());
59  return (buffer_[word_index] & (1 << bit_index)) != 0;
60  }
61 
63  void And(const Bitmap* src) {
64  DCHECK_EQ(size(), src->size());
65  for (int i = 0; i < buffer_.size(); ++i) {
66  buffer_[i] &= src->buffer_[i];
67  }
68  }
69 
71  void Or(const Bitmap* src) {
72  DCHECK_EQ(size(), src->size());
73  for (int i = 0; i < buffer_.size(); ++i) {
74  buffer_[i] |= src->buffer_[i];
75  }
76  }
77 
78  void SetAllBits(bool b) {
79  memset(&buffer_[0], 255 * b, buffer_.size() * sizeof(uint64_t));
80  }
81 
82  int64_t size() const { return size_; }
83 
85  std::string DebugString(bool print_bits);
86 
87  private:
88  std::vector<uint64_t> buffer_;
89  int64_t size_;
90 };
91 
92 }
93 
94 #endif
void And(const Bitmap *src)
Bitwise ANDs the src bitmap into this one.
Definition: bitmap.h:63
int64_t size_
Definition: bitmap.h:89
std::string DebugString(bool print_bits)
If 'print_bits' prints 0/1 per bit, otherwise it prints the int64_t value.
Definition: bitmap.cc:23
std::vector< uint64_t > buffer_
Definition: bitmap.h:88
bool Get(int64_t bit_index) const
Definition: bitmap.h:54
Bitmap(int64_t num_bits)
Definition: bitmap.h:31
void Set(int64_t bit_index, bool v)
Definition: bitmap.h:39
void SetAllBits(bool b)
Definition: bitmap.h:78
void Or(const Bitmap *src)
Bitwise ORs the src bitmap into this one.
Definition: bitmap.h:71
int64_t size() const
Definition: bitmap.h:82
static uint32_t RoundUpNumi64(uint32_t bits)
Returns the rounded up to 64 multiple. Used for conversions of bits to i64.
Definition: bit-util.h:97