enum_set.h 5.4 KB

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