enum_set.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // Returns true if this enum value is in the set.
  84. bool Contains(EnumType c) const { return ContainsWord(ToWord(c)); }
  85. // Applies f to each enum in the set, in order from smallest enum
  86. // value to largest.
  87. void ForEach(std::function<void(EnumType)> f) const {
  88. for (uint32_t i = 0; i < 64; ++i) {
  89. if (mask_ & AsMask(i)) f(static_cast<EnumType>(i));
  90. }
  91. if (overflow_) {
  92. for (uint32_t c : *overflow_) f(static_cast<EnumType>(c));
  93. }
  94. }
  95. // Returns true if the set is empty.
  96. bool IsEmpty() const {
  97. if (mask_) return false;
  98. if (overflow_ && !overflow_->empty()) return false;
  99. return true;
  100. }
  101. // Returns true if the set contains ANY of the elements of |in_set|,
  102. // or if |in_set| is empty.
  103. bool HasAnyOf(const EnumSet<EnumType>& in_set) const {
  104. if (in_set.IsEmpty()) return true;
  105. if (mask_ & in_set.mask_) return true;
  106. if (!overflow_ || !in_set.overflow_) return false;
  107. for (uint32_t item : *in_set.overflow_) {
  108. if (overflow_->find(item) != overflow_->end()) return true;
  109. }
  110. return false;
  111. }
  112. private:
  113. // Adds the given enum value (as a 32-bit word) to the set. This has no
  114. // effect if the enum value is already in the set.
  115. void AddWord(uint32_t word) {
  116. if (auto new_bits = AsMask(word)) {
  117. mask_ |= new_bits;
  118. } else {
  119. Overflow().insert(word);
  120. }
  121. }
  122. // Returns true if the enum represented as a 32-bit word is in the set.
  123. bool ContainsWord(uint32_t word) const {
  124. // We shouldn't call Overflow() since this is a const method.
  125. if (auto bits = AsMask(word)) {
  126. return (mask_ & bits) != 0;
  127. } else if (auto overflow = overflow_.get()) {
  128. return overflow->find(word) != overflow->end();
  129. }
  130. // The word is large, but the set doesn't have large members, so
  131. // it doesn't have an overflow set.
  132. return false;
  133. }
  134. // Returns the enum value as a uint32_t.
  135. uint32_t ToWord(EnumType value) const {
  136. static_assert(sizeof(EnumType) <= sizeof(uint32_t),
  137. "EnumType must statically castable to uint32_t");
  138. return static_cast<uint32_t>(value);
  139. }
  140. // Determines whether the given enum value can be represented
  141. // as a bit in a uint64_t mask. If so, then returns that mask bit.
  142. // Otherwise, returns 0.
  143. uint64_t AsMask(uint32_t word) const {
  144. if (word > 63) return 0;
  145. return uint64_t(1) << word;
  146. }
  147. // Ensures that overflow_set_ references a set. A new empty set is
  148. // allocated if one doesn't exist yet. Returns overflow_set_.
  149. OverflowSetType& Overflow() {
  150. if (overflow_.get() == nullptr) {
  151. overflow_ = MakeUnique<OverflowSetType>();
  152. }
  153. return *overflow_;
  154. }
  155. // Enums with values up to 63 are stored as bits in this mask.
  156. uint64_t mask_ = 0;
  157. // Enums with values larger than 63 are stored in this set.
  158. // This set should normally be empty or very small.
  159. std::unique_ptr<OverflowSetType> overflow_ = {};
  160. };
  161. // A set of SpvCapability, optimized for small capability values.
  162. using CapabilitySet = EnumSet<SpvCapability>;
  163. } // namespace spvtools
  164. #endif // SOURCE_ENUM_SET_H_