OR-Tools  9.6
bitmap.h
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_BASE_BITMAP_H_
15 #define OR_TOOLS_BASE_BITMAP_H_
16 
17 #include <cassert>
18 #include <cstring>
19 
21 
22 namespace operations_research {
23 namespace internal {
24 inline uint64_t OneBit64(int pos) { return uint64_t{1} << pos; }
25 inline uint64_t BitPos64(uint64_t pos) { return (pos & 63); }
26 inline uint64_t BitOffset64(uint64_t pos) { return (pos >> 6); }
27 inline uint64_t BitLength64(uint64_t size) { return ((size + 63) >> 6); }
28 inline bool IsBitSet64(const uint64_t* const bitset, uint64_t pos) {
29  return (bitset[BitOffset64(pos)] & OneBit64(BitPos64(pos)));
30 }
31 inline void SetBit64(uint64_t* const bitset, uint64_t pos) {
32  bitset[BitOffset64(pos)] |= OneBit64(BitPos64(pos));
33 }
34 inline void ClearBit64(uint64_t* const bitset, uint64_t pos) {
35  bitset[BitOffset64(pos)] &= ~OneBit64(BitPos64(pos));
36 }
37 } // namespace internal
38 
39 class Bitmap {
40  public:
41  // Constructor : This allocates on a uint32_t boundary.
42  // fill: true = initialize with 1's, false = initialize with 0's.
43  explicit Bitmap(uint32_t size, bool fill = false)
44  : max_size_(size),
45  array_size_(internal::BitLength64(size)),
46  map_(new uint64_t[array_size_]) {
47  // initialize all of the bits
48  SetAll(fill);
49  }
50 
51  // Destructor: clean up.
52  ~Bitmap() { delete[] map_; }
53 
54  // Resizes the bitmap.
55  // If size < bits(), the extra bits will be discarded.
56  // If size > bits(), the extra bits will be filled with the fill value.
57  void Resize(uint32_t size, bool fill = false);
58 
59  bool Get(uint32_t index) const {
60  assert(max_size_ == 0 || index < max_size_);
61  return internal::IsBitSet64(map_, index);
62  }
63  void Set(uint32_t index, bool value) {
64  assert(max_size_ == 0 || index < max_size_);
65  if (value) {
67  } else {
69  }
70  }
71 
72  // Sets all the bits to true or false
73  void SetAll(bool value) {
74  memset(map_, (value ? 0xFF : 0x00), array_size_ * sizeof(*map_));
75  }
76 
77  // Clears all bits in the bitmap
78  void Clear() { SetAll(false); }
79 
80  private:
81  uint32_t max_size_; // the upper bound of the bitmap
82  uint32_t array_size_;
83  uint64_t* map_; // the bitmap
84 };
85 
86 } // namespace operations_research
87 
88 #endif // OR_TOOLS_BASE_BITMAP_H_
void Set(uint32_t index, bool value)
Definition: bitmap.h:63
void SetAll(bool value)
Definition: bitmap.h:73
bool Get(uint32_t index) const
Definition: bitmap.h:59
Bitmap(uint32_t size, bool fill=false)
Definition: bitmap.h:43
void Resize(uint32_t size, bool fill=false)
Definition: bitmap.cc:22
int64_t value
int index
void SetBit64(uint64_t *const bitset, uint64_t pos)
Definition: bitmap.h:31
uint64_t BitLength64(uint64_t size)
Definition: bitmap.h:27
uint64_t OneBit64(int pos)
Definition: bitmap.h:24
uint64_t BitPos64(uint64_t pos)
Definition: bitmap.h:25
bool IsBitSet64(const uint64_t *const bitset, uint64_t pos)
Definition: bitmap.h:28
void ClearBit64(uint64_t *const bitset, uint64_t pos)
Definition: bitmap.h:34
uint64_t BitOffset64(uint64_t pos)
Definition: bitmap.h:26
Collection of objects used to extend the Constraint Solver library.
uint64_t BitLength64(uint64_t size)
Definition: bitset.h:339