|
| 1 | +/* |
| 2 | + * Copyright 2023 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef wasm_support_bitset_h |
| 18 | +#define wasm_support_bitset_h |
| 19 | + |
| 20 | +#include <cassert> |
| 21 | +#include <cstddef> |
| 22 | +#include <iterator> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace wasm { |
| 26 | + |
| 27 | +// Represent a set of integers in [0, N) with a bitvector of size N, where a 1 |
| 28 | +// bit signifies set membership. This interface is intended to match that of |
| 29 | +// std::set or std::unordered_set as closely as possible. |
| 30 | +class BitSet : std::vector<bool> { |
| 31 | + std::vector<bool>& vec() noexcept { return *this; } |
| 32 | + const std::vector<bool>& vec() const noexcept { return *this; } |
| 33 | + |
| 34 | + void ensure(size_t value) noexcept { |
| 35 | + if (vec().size() <= value) { |
| 36 | + vec().resize(value + 1); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | +public: |
| 41 | + using key_type = size_t; |
| 42 | + using value_type = size_t; |
| 43 | + |
| 44 | + class iterator { |
| 45 | + const std::vector<bool>* parent = nullptr; |
| 46 | + size_t index = 0; |
| 47 | + |
| 48 | + iterator(const std::vector<bool>* parent) : parent(parent) { |
| 49 | + const auto size = parent->size(); |
| 50 | + while (index < size && !(*parent)[index]) { |
| 51 | + ++index; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + iterator(const std::vector<bool>* parent, size_t index) |
| 56 | + : parent(parent), index(index) {} |
| 57 | + |
| 58 | + friend BitSet; |
| 59 | + |
| 60 | + public: |
| 61 | + using difference_type = ptrdiff_t; |
| 62 | + using value_type = size_t; |
| 63 | + using pointer = size_t*; |
| 64 | + using reference = size_t&; |
| 65 | + using iterator_category = std::bidirectional_iterator_tag; |
| 66 | + |
| 67 | + iterator() = default; |
| 68 | + iterator(const iterator&) = default; |
| 69 | + iterator(iterator&&) = default; |
| 70 | + iterator& operator=(const iterator&) = default; |
| 71 | + iterator& operator=(iterator&&) = default; |
| 72 | + |
| 73 | + size_t operator*() const noexcept { return index; } |
| 74 | + |
| 75 | + iterator& operator++() noexcept { |
| 76 | + const auto size = parent->size(); |
| 77 | + do { |
| 78 | + ++index; |
| 79 | + } while (index < size && !(*parent)[index]); |
| 80 | + return *this; |
| 81 | + } |
| 82 | + |
| 83 | + iterator operator++(int) noexcept { |
| 84 | + iterator it = *this; |
| 85 | + ++(*this); |
| 86 | + return it; |
| 87 | + } |
| 88 | + |
| 89 | + iterator& operator--() noexcept { |
| 90 | + do { |
| 91 | + --index; |
| 92 | + assert(index != -1ull); |
| 93 | + } while (!(*parent)[index]); |
| 94 | + return *this; |
| 95 | + } |
| 96 | + |
| 97 | + iterator operator--(int) noexcept { |
| 98 | + iterator it = *this; |
| 99 | + --(*this); |
| 100 | + return it; |
| 101 | + } |
| 102 | + |
| 103 | + bool operator==(const iterator& other) const noexcept { |
| 104 | + assert(parent == other.parent); |
| 105 | + return index == other.index; |
| 106 | + } |
| 107 | + bool operator!=(const iterator& other) const noexcept { |
| 108 | + return !(*this == other); |
| 109 | + assert(parent == other.parent); |
| 110 | + return index == other.index; |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + BitSet() = default; |
| 115 | + BitSet(const BitSet&) = default; |
| 116 | + BitSet(BitSet&&) = default; |
| 117 | + |
| 118 | + template<typename It> BitSet(It begin, It end) { |
| 119 | + for (auto it = begin; it != end; ++it) { |
| 120 | + insert(*it); |
| 121 | + } |
| 122 | + } |
| 123 | + BitSet(const std::initializer_list<size_t>& vals) |
| 124 | + : BitSet(vals.begin(), vals.end()) {} |
| 125 | + |
| 126 | + BitSet& operator=(const BitSet&) = default; |
| 127 | + BitSet& operator=(BitSet&&) = default; |
| 128 | + |
| 129 | + iterator begin() const noexcept { return iterator({this}); } |
| 130 | + iterator end() const noexcept { return iterator({this, vec().size()}); } |
| 131 | + |
| 132 | + bool empty() const noexcept { return begin() == end(); } |
| 133 | + |
| 134 | + size_t size() const noexcept { |
| 135 | + size_t result = 0; |
| 136 | + for (auto it = begin(); it != end(); ++it, ++result) { |
| 137 | + } |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + void clear() noexcept { vec().clear(); } |
| 142 | + |
| 143 | + std::pair<iterator, bool> insert(size_t value) noexcept { |
| 144 | + ensure(value); |
| 145 | + bool didInsert = false; |
| 146 | + if (!(*this)[value]) { |
| 147 | + didInsert = true; |
| 148 | + (*this)[value] = true; |
| 149 | + } |
| 150 | + return {iterator({this, value}), didInsert}; |
| 151 | + } |
| 152 | + |
| 153 | + size_t erase(size_t key) noexcept { |
| 154 | + if (key < vec().size()) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + bool didErase = false; |
| 158 | + if ((*this)[key]) { |
| 159 | + didErase = true; |
| 160 | + (*this)[key] = false; |
| 161 | + } |
| 162 | + return didErase; |
| 163 | + } |
| 164 | + |
| 165 | + iterator erase(iterator it) noexcept { |
| 166 | + auto next = it; |
| 167 | + ++next; |
| 168 | + (*this)[it.index] = false; |
| 169 | + return next; |
| 170 | + } |
| 171 | + |
| 172 | + size_t count(size_t key) const noexcept { |
| 173 | + if (key >= vec().size()) { |
| 174 | + return 0; |
| 175 | + } |
| 176 | + return (*this)[key]; |
| 177 | + } |
| 178 | + |
| 179 | + bool operator==(const BitSet& other) const { |
| 180 | + auto thisSize = vec().size(); |
| 181 | + auto otherSize = other.vec().size(); |
| 182 | + const std::vector<bool>* shorter; |
| 183 | + const std::vector<bool>* longer; |
| 184 | + if (thisSize <= otherSize) { |
| 185 | + shorter = this; |
| 186 | + longer = &other; |
| 187 | + } else { |
| 188 | + shorter = &other; |
| 189 | + longer = this; |
| 190 | + } |
| 191 | + size_t i = 0; |
| 192 | + size_t shortSize = shorter->size(); |
| 193 | + size_t longSize = longer->size(); |
| 194 | + // All elements should match. |
| 195 | + for (; i < shortSize; ++i) { |
| 196 | + if ((*shorter)[i] != (*longer)[i]) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + } |
| 200 | + // The rest of the longer vector should be zeroes. |
| 201 | + for (; i < longSize; ++i) { |
| 202 | + if ((*longer)[i]) { |
| 203 | + return false; |
| 204 | + } |
| 205 | + } |
| 206 | + return true; |
| 207 | + } |
| 208 | +}; |
| 209 | + |
| 210 | +#if __cplusplus >= 202002L |
| 211 | +static_assert(std::bidirectional_iterator<typename BitSet::iterator>); |
| 212 | +#endif |
| 213 | + |
| 214 | +} // namespace wasm |
| 215 | + |
| 216 | +#endif // wasm_support_bitset_h |
0 commit comments