enum_set.h 5.4 KB

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