enum_set.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2016 Google Inc.
  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. #ifndef SOURCE_ENUM_SET_H_
  15. #define SOURCE_ENUM_SET_H_
  16. #include <cstdint>
  17. #include <functional>
  18. #include <memory>
  19. #include <set>
  20. #include <utility>
  21. #include "source/latest_version_spirv_header.h"
  22. #include "source/util/make_unique.h"
  23. namespace spvtools {
  24. // A set of values of a 32-bit enum type.
  25. // It is fast and compact for the common case, where enum values
  26. // are at most 63. But it can represent enums with larger values,
  27. // as may appear in extensions.
  28. template <typename EnumType>
  29. class EnumSet {
  30. private:
  31. // The ForEach method will call the functor on enum values in
  32. // enum value order (lowest to highest). To make that easier, use
  33. // an ordered set for the overflow values.
  34. using OverflowSetType = std::set<uint32_t>;
  35. public:
  36. // Construct an empty set.
  37. EnumSet() {}
  38. // Construct an set with just the given enum value.
  39. explicit EnumSet(EnumType c) { Add(c); }
  40. // Construct an set from an initializer list of enum values.
  41. EnumSet(std::initializer_list<EnumType> cs) {
  42. for (auto c : cs) Add(c);
  43. }
  44. EnumSet(uint32_t count, const EnumType* ptr) {
  45. for (uint32_t i = 0; i < count; ++i) Add(ptr[i]);
  46. }
  47. // Copy constructor.
  48. EnumSet(const EnumSet& other) { *this = other; }
  49. // Move constructor. The moved-from set is emptied.
  50. EnumSet(EnumSet&& other) {
  51. mask_ = other.mask_;
  52. overflow_ = std::move(other.overflow_);
  53. other.mask_ = 0;
  54. other.overflow_.reset(nullptr);
  55. }
  56. // Assignment operator.
  57. EnumSet& operator=(const EnumSet& other) {
  58. if (&other != this) {
  59. mask_ = other.mask_;
  60. overflow_.reset(other.overflow_ ? new OverflowSetType(*other.overflow_)
  61. : nullptr);
  62. }
  63. return *this;
  64. }
  65. friend bool operator==(const EnumSet& a, const EnumSet& b) {
  66. if (a.mask_ != b.mask_) {
  67. return false;
  68. }
  69. if (a.overflow_ == nullptr && b.overflow_ == nullptr) {
  70. return true;
  71. }
  72. if (a.overflow_ == nullptr || b.overflow_ == nullptr) {
  73. return false;
  74. }
  75. return *a.overflow_ == *b.overflow_;
  76. }
  77. friend bool operator!=(const EnumSet& a, const EnumSet& b) {
  78. return !(a == b);
  79. }
  80. // Adds the given enum value to the set. This has no effect if the
  81. // enum value is already in the set.
  82. void Add(EnumType c) { AddWord(ToWord(c)); }
  83. // Removes the given enum value from the set. This has no effect if the
  84. // enum value is not in the set.
  85. void Remove(EnumType c) { RemoveWord(ToWord(c)); }
  86. // Returns true if this enum value is in the set.
  87. bool Contains(EnumType c) const { return ContainsWord(ToWord(c)); }
  88. // Applies f to each enum in the set, in order from smallest enum
  89. // value to largest.
  90. void ForEach(std::function<void(EnumType)> f) const {
  91. for (uint32_t i = 0; i < 64; ++i) {
  92. if (mask_ & AsMask(i)) f(static_cast<EnumType>(i));
  93. }
  94. if (overflow_) {
  95. for (uint32_t c : *overflow_) f(static_cast<EnumType>(c));
  96. }
  97. }
  98. // Returns true if the set is empty.
  99. bool IsEmpty() const {
  100. if (mask_) return false;
  101. if (overflow_ && !overflow_->empty()) return false;
  102. return true;
  103. }
  104. // Returns true if the set contains ANY of the elements of |in_set|,
  105. // or if |in_set| is empty.
  106. bool HasAnyOf(const EnumSet<EnumType>& in_set) const {
  107. if (in_set.IsEmpty()) return true;
  108. if (mask_ & in_set.mask_) return true;
  109. if (!overflow_ || !in_set.overflow_) return false;
  110. for (uint32_t item : *in_set.overflow_) {
  111. if (overflow_->find(item) != overflow_->end()) return true;
  112. }
  113. return false;
  114. }
  115. private:
  116. // Adds the given enum value (as a 32-bit word) to the set. This has no
  117. // effect if the enum value is already in the set.
  118. void AddWord(uint32_t word) {
  119. if (auto new_bits = AsMask(word)) {
  120. mask_ |= new_bits;
  121. } else {
  122. Overflow().insert(word);
  123. }
  124. }
  125. // Removes the given enum value (as a 32-bit word) from the set. This has no
  126. // effect if the enum value is not in the set.
  127. void RemoveWord(uint32_t word) {
  128. if (auto new_bits = AsMask(word)) {
  129. mask_ &= ~new_bits;
  130. } else {
  131. auto itr = Overflow().find(word);
  132. if (itr != Overflow().end()) Overflow().erase(itr);
  133. }
  134. }
  135. // Returns true if the enum represented as a 32-bit word is in the set.
  136. bool ContainsWord(uint32_t word) const {
  137. // We shouldn't call Overflow() since this is a const method.
  138. if (auto bits = AsMask(word)) {
  139. return (mask_ & bits) != 0;
  140. } else if (auto overflow = overflow_.get()) {
  141. return overflow->find(word) != overflow->end();
  142. }
  143. // The word is large, but the set doesn't have large members, so
  144. // it doesn't have an overflow set.
  145. return false;
  146. }
  147. // Returns the enum value as a uint32_t.
  148. uint32_t ToWord(EnumType value) const {
  149. static_assert(sizeof(EnumType) <= sizeof(uint32_t),
  150. "EnumType must statically castable to uint32_t");
  151. return static_cast<uint32_t>(value);
  152. }
  153. // Determines whether the given enum value can be represented
  154. // as a bit in a uint64_t mask. If so, then returns that mask bit.
  155. // Otherwise, returns 0.
  156. uint64_t AsMask(uint32_t word) const {
  157. if (word > 63) return 0;
  158. return uint64_t(1) << word;
  159. }
  160. // Ensures that overflow_set_ references a set. A new empty set is
  161. // allocated if one doesn't exist yet. Returns overflow_set_.
  162. OverflowSetType& Overflow() {
  163. if (overflow_.get() == nullptr) {
  164. overflow_ = MakeUnique<OverflowSetType>();
  165. }
  166. return *overflow_;
  167. }
  168. // Enums with values up to 63 are stored as bits in this mask.
  169. uint64_t mask_ = 0;
  170. // Enums with values larger than 63 are stored in this set.
  171. // This set should normally be empty or very small.
  172. std::unique_ptr<OverflowSetType> overflow_ = {};
  173. };
  174. // A set of SpvCapability, optimized for small capability values.
  175. using CapabilitySet = EnumSet<SpvCapability>;
  176. } // namespace spvtools
  177. #endif // SOURCE_ENUM_SET_H_