Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
bitmap.h
Go to the documentation of this file.
1// Copyright 2010-2024 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 <cstdint>
19#include <cstring>
20
21namespace operations_research {
22namespace internal {
23inline uint64_t OneBit64(int pos) { return uint64_t{1} << pos; }
24inline uint64_t BitPos64(uint64_t pos) { return (pos & 63); }
25inline uint64_t BitOffset64(uint64_t pos) { return (pos >> 6); }
26inline uint64_t BitLength64(uint64_t size) { return ((size + 63) >> 6); }
27inline bool IsBitSet64(const uint64_t* const bitset, uint64_t pos) {
28 return (bitset[BitOffset64(pos)] & OneBit64(BitPos64(pos)));
29}
30inline void SetBit64(uint64_t* const bitset, uint64_t pos) {
31 bitset[BitOffset64(pos)] |= OneBit64(BitPos64(pos));
32}
33inline void ClearBit64(uint64_t* const bitset, uint64_t pos) {
34 bitset[BitOffset64(pos)] &= ~OneBit64(BitPos64(pos));
35}
36} // namespace internal
37
38class Bitmap {
39 public:
40 // Constructor : This allocates on a uint32_t boundary.
41 // fill: true = initialize with 1's, false = initialize with 0's.
42 explicit Bitmap(uint32_t size, bool fill = false)
43 : max_size_(size),
44 array_size_(internal::BitLength64(size)),
45 map_(new uint64_t[array_size_]) {
46 // initialize all of the bits
47 SetAll(fill);
48 }
49
50 // Destructor: clean up.
51 ~Bitmap() { delete[] map_; }
52
53 // Resizes the bitmap.
54 // If size < bits(), the extra bits will be discarded.
55 // If size > bits(), the extra bits will be filled with the fill value.
56 void Resize(uint32_t size, bool fill = false);
57
58 bool Get(uint32_t index) const {
59 assert(max_size_ == 0 || index < max_size_);
60 return internal::IsBitSet64(map_, index);
61 }
62 void Set(uint32_t index, bool value) {
63 assert(max_size_ == 0 || index < max_size_);
64 if (value) {
66 } else {
68 }
69 }
70
71 // Sets all the bits to true or false
72 void SetAll(bool value) {
73 memset(map_, (value ? 0xFF : 0x00), array_size_ * sizeof(*map_));
74 }
75
76 // Clears all bits in the bitmap
77 void Clear() { SetAll(false); }
78
79 private:
80 uint32_t max_size_; // the upper bound of the bitmap
81 uint32_t array_size_;
82 uint64_t* map_; // the bitmap
83};
84
85} // namespace operations_research
86
87#endif // OR_TOOLS_BASE_BITMAP_H_
IntegerValue size
void Resize(uint32_t size, bool fill=false)
Definition bitmap.cc:20
~Bitmap()
Destructor: clean up.
Definition bitmap.h:51
bool Get(uint32_t index) const
Definition bitmap.h:58
void Set(uint32_t index, bool value)
Definition bitmap.h:62
Bitmap(uint32_t size, bool fill=false)
Definition bitmap.h:42
void Clear()
Clears all bits in the bitmap.
Definition bitmap.h:77
void SetAll(bool value)
Sets all the bits to true or false.
Definition bitmap.h:72
int64_t value
int index
void SetBit64(uint64_t *const bitset, uint64_t pos)
Definition bitmap.h:30
uint64_t BitLength64(uint64_t size)
Definition bitmap.h:26
uint64_t OneBit64(int pos)
Definition bitmap.h:23
uint64_t BitPos64(uint64_t pos)
Definition bitmap.h:24
bool IsBitSet64(const uint64_t *const bitset, uint64_t pos)
Definition bitmap.h:27
void ClearBit64(uint64_t *const bitset, uint64_t pos)
Definition bitmap.h:33
uint64_t BitOffset64(uint64_t pos)
Definition bitmap.h:25
In SWIG mode, we don't want anything besides these top-level includes.
uint64_t BitLength64(uint64_t size)
Returns the number of words needed to store size bits.
Definition bitset.h:342