1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
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 TENSORFLOW_CORE_LIB_CORE_BITMAP_H_
17 #define TENSORFLOW_CORE_LIB_CORE_BITMAP_H_
18
19 #include "tensorflow/core/platform/logging.h"
20 #include "tensorflow/core/platform/types.h"
21
22 namespace tensorflow {
23 namespace core {
24
25 class Bitmap {
26 public:
27 // Create a bitmap that holds 0 bits.
28 Bitmap();
29
30 // Create a bitmap that holds n bits, all initially zero.
31 explicit Bitmap(size_t n);
32
33 ~Bitmap();
34
35 Bitmap(const Bitmap&) = delete;
36 Bitmap& operator=(const Bitmap&) = delete;
37
38 // Return the number of bits that the bitmap can hold.
39 size_t bits() const;
40
41 // Replace contents of *this with a bitmap of n bits, all set to zero.
42 void Reset(size_t n);
43
44 // Return the contents of the ith bit.
45 // REQUIRES: i < bits()
46 bool get(size_t i) const;
47
48 // Set the contents of the ith bit to true.
49 // REQUIRES: i < bits()
50 void set(size_t i);
51
52 // Set the contents of the ith bit to false.
53 // REQUIRES: i < bits()
54 void clear(size_t i);
55
56 // Return the smallest i such that i >= start and !get(i).
57 // Returns bits() if no such i exists.
58 size_t FirstUnset(size_t start) const;
59
60 // Returns the bitmap as an ascii string of '0' and '1' characters, bits()
61 // characters in length.
62 string ToString() const;
63
64 private:
65 typedef uint32 Word;
66 static constexpr size_t kBits = 32;
67
68 // Return the number of words needed to store n bits.
NumWords(size_t n)69 static size_t NumWords(size_t n) { return (n + kBits - 1) / kBits; }
70
71 // Return the mask to use for the ith bit in a word.
Mask(size_t i)72 static Word Mask(size_t i) { return 1ull << i; }
73
74 size_t nbits_; // Length of bitmap in bits.
75 Word* word_;
76 };
77
78 // Implementation details follow. Clients should ignore.
79
Bitmap()80 inline Bitmap::Bitmap() : nbits_(0), word_(nullptr) {}
81
Bitmap(size_t n)82 inline Bitmap::Bitmap(size_t n) : Bitmap() { Reset(n); }
83
~Bitmap()84 inline Bitmap::~Bitmap() { delete[] word_; }
85
bits()86 inline size_t Bitmap::bits() const { return nbits_; }
87
get(size_t i)88 inline bool Bitmap::get(size_t i) const {
89 DCHECK_LT(i, nbits_);
90 return word_[i / kBits] & Mask(i % kBits);
91 }
92
set(size_t i)93 inline void Bitmap::set(size_t i) {
94 DCHECK_LT(i, nbits_);
95 word_[i / kBits] |= Mask(i % kBits);
96 }
97
clear(size_t i)98 inline void Bitmap::clear(size_t i) {
99 DCHECK_LT(i, nbits_);
100 word_[i / kBits] &= ~Mask(i % kBits);
101 }
102
103 } // namespace core
104 } // namespace tensorflow
105
106 #endif // TENSORFLOW_CORE_LIB_CORE_BITMAP_H_
107