Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Dawn & Tint Authors |
Ben Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ben Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ben Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 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 Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 27 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 28 | #ifndef SRC_TINT_UTILS_CONTAINERS_BITSET_H_ |
| 29 | #define SRC_TINT_UTILS_CONTAINERS_BITSET_H_ |
Ben Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 30 | |
| 31 | #include <stdint.h> |
| 32 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 33 | #include "src/tint/utils/containers/vector.h" |
Ben Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 34 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 35 | namespace tint { |
Ben Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 36 | |
| 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. |
| 41 | template <size_t N = 0> |
| 42 | class 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 | |
shrekshao | 7f760cb | 2022-11-22 21:30:10 +0000 | [diff] [blame] | 108 | /// 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 Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 127 | private: |
| 128 | Vector<size_t, NumWords(N)> vec_; |
| 129 | size_t len_ = 0; |
| 130 | }; |
| 131 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 132 | } // namespace tint |
Ben Clayton | e43034b | 2022-07-21 23:32:24 +0000 | [diff] [blame] | 133 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 134 | #endif // SRC_TINT_UTILS_CONTAINERS_BITSET_H_ |