blob: a53622ebd6dbb5a8904b51cf78d82656d4982b2e [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2022 The Dawn & Tint Authors
Ben Claytone43034b2022-07-21 23:32:24 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ben Claytone43034b2022-07-21 23:32:24 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ben Claytone43034b2022-07-21 23:32:24 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ben Claytone43034b2022-07-21 23:32:24 +000027
dan sinclair22b4dd22023-07-21 00:40:07 +000028#ifndef SRC_TINT_UTILS_CONTAINERS_BITSET_H_
29#define SRC_TINT_UTILS_CONTAINERS_BITSET_H_
Ben Claytone43034b2022-07-21 23:32:24 +000030
31#include <stdint.h>
32
dan sinclair22b4dd22023-07-21 00:40:07 +000033#include "src/tint/utils/containers/vector.h"
Ben Claytone43034b2022-07-21 23:32:24 +000034
dan sinclairbae54e72023-07-28 15:01:54 +000035namespace tint {
Ben Claytone43034b2022-07-21 23:32:24 +000036
37/// Bitset is a dynamically sized, vector of bits, packed into integer words.
38/// Bits can be individually read and written using the index operator.
39///
40/// Bitset will fit at least `N` bits internally before spilling to heap allocations.
41template <size_t N = 0>
42class Bitset {
43 /// The integer word type used to hold the bits
44 using Word = size_t;
45 /// Number of bits per word
46 static constexpr size_t kWordBits = sizeof(Word) * 8;
47
48 /// Number of words required to hold the number of bits
49 static constexpr size_t NumWords(size_t num_bits) {
50 return ((num_bits + kWordBits - 1) / kWordBits);
51 }
52
53 public:
54 /// Constructor
55 Bitset() = default;
56
57 /// Destructor
58 ~Bitset() = default;
59
60 /// Accessor for a single bit
61 struct Bit {
62 /// The word that contains the bit
63 Word& word;
64 /// A word with a single bit set, which masks the targetted bit
65 Word const mask;
66
67 /// Assignment operator
68 /// @param value the new value for the bit
69 /// @returns this Bit so calls can be chained
70 const Bit& operator=(bool value) const {
71 if (value) {
72 word = word | mask;
73 } else {
74 word = word & ~mask;
75 }
76 return *this;
77 }
78
79 /// Conversion operator
80 /// @returns the bit value
81 operator bool() const { return (word & mask) != 0; }
82 };
83
84 /// @param new_len the new size of the bitmap, in bits.
85 void Resize(size_t new_len) {
86 vec_.Resize(NumWords(new_len));
87
88 // Clear any potentially set bits that are in the top part of the word
89 if (size_t high_bit = new_len % kWordBits; high_bit > 0) {
90 vec_.Back() &= (static_cast<Word>(1) << high_bit) - 1;
91 }
92
93 len_ = new_len;
94 }
95
96 /// @return the number of bits in the bitset.
97 size_t Length() const { return len_; }
98
99 /// Index operator
100 /// @param index the index of the bit to access
101 /// @return the accessor for the indexed bit
102 Bit operator[](size_t index) {
103 auto& word = vec_[index / kWordBits];
104 auto mask = static_cast<Word>(1) << (index % kWordBits);
105 return Bit{word, mask};
106 }
107
shrekshao7f760cb2022-11-22 21:30:10 +0000108 /// Const index operator
109 /// @param index the index of the bit to access
110 /// @return bool value of the indexed bit
111 bool operator[](size_t index) const {
112 const auto& word = vec_[index / kWordBits];
113 auto mask = static_cast<Word>(1) << (index % kWordBits);
114 return word & mask;
115 }
116
117 /// @returns true iff the all bits are unset (0)
118 bool AllBitsZero() const {
119 for (auto word : vec_) {
120 if (word) {
121 return false;
122 }
123 }
124 return true;
125 }
126
Ben Claytone43034b2022-07-21 23:32:24 +0000127 private:
128 Vector<size_t, NumWords(N)> vec_;
129 size_t len_ = 0;
130};
131
dan sinclairbae54e72023-07-28 15:01:54 +0000132} // namespace tint
Ben Claytone43034b2022-07-21 23:32:24 +0000133
dan sinclair22b4dd22023-07-21 00:40:07 +0000134#endif // SRC_TINT_UTILS_CONTAINERS_BITSET_H_