blob: 2e7046d4ece64fe3fbb8009f48c6a690cc999ab5 [file] [log] [blame]
David Neto909d7f92016-08-31 14:35:58 -04001// Copyright (c) 2016 Google Inc.
2//
David Neto9fc86582016-09-01 15:33:59 -04003// 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
David Neto909d7f92016-08-31 14:35:58 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
David Neto909d7f92016-08-31 14:35:58 -04008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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.
David Neto909d7f92016-08-31 14:35:58 -040014
dan sinclair58a68762018-08-03 08:05:33 -040015#ifndef SOURCE_ENUM_SET_H_
16#define SOURCE_ENUM_SET_H_
David Neto909d7f92016-08-31 14:35:58 -040017
18#include <cstdint>
19#include <functional>
20#include <memory>
21#include <set>
22#include <utility>
23
dan sinclaireda2cfb2018-08-03 15:06:09 -040024#include "source/latest_version_spirv_header.h"
dan sinclair1963a2d2018-08-14 15:01:50 -040025#include "source/util/make_unique.h"
David Neto909d7f92016-08-31 14:35:58 -040026
dan sinclair3dad1cd2018-07-07 09:38:00 -040027namespace spvtools {
David Neto909d7f92016-08-31 14:35:58 -040028
29// A set of values of a 32-bit enum type.
30// It is fast and compact for the common case, where enum values
31// are at most 63. But it can represent enums with larger values,
32// as may appear in extensions.
33template <typename EnumType>
34class EnumSet {
35 private:
36 // The ForEach method will call the functor on enum values in
37 // enum value order (lowest to highest). To make that easier, use
38 // an ordered set for the overflow values.
39 using OverflowSetType = std::set<uint32_t>;
40
41 public:
42 // Construct an empty set.
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050043 EnumSet() {}
David Neto909d7f92016-08-31 14:35:58 -040044 // Construct an set with just the given enum value.
45 explicit EnumSet(EnumType c) { Add(c); }
46 // Construct an set from an initializer list of enum values.
47 EnumSet(std::initializer_list<EnumType> cs) {
48 for (auto c : cs) Add(c);
49 }
Lei Zhang063dbea2017-10-25 12:15:51 -040050 EnumSet(uint32_t count, const EnumType* ptr) {
51 for (uint32_t i = 0; i < count; ++i) Add(ptr[i]);
52 }
David Neto909d7f92016-08-31 14:35:58 -040053 // Copy constructor.
54 EnumSet(const EnumSet& other) { *this = other; }
55 // Move constructor. The moved-from set is emptied.
56 EnumSet(EnumSet&& other) {
57 mask_ = other.mask_;
58 overflow_ = std::move(other.overflow_);
59 other.mask_ = 0;
60 other.overflow_.reset(nullptr);
61 }
62 // Assignment operator.
63 EnumSet& operator=(const EnumSet& other) {
64 if (&other != this) {
65 mask_ = other.mask_;
66 overflow_.reset(other.overflow_ ? new OverflowSetType(*other.overflow_)
67 : nullptr);
68 }
69 return *this;
70 }
71
Steven Perron73422a02019-08-28 11:49:16 -040072 friend bool operator==(const EnumSet& a, const EnumSet& b) {
73 if (a.mask_ != b.mask_) {
74 return false;
75 }
76
77 if (a.overflow_ == nullptr && b.overflow_ == nullptr) {
78 return true;
79 }
80
81 if (a.overflow_ == nullptr || b.overflow_ == nullptr) {
82 return false;
83 }
84
85 return *a.overflow_ == *b.overflow_;
86 }
87
88 friend bool operator!=(const EnumSet& a, const EnumSet& b) {
89 return !(a == b);
90 }
91
David Neto909d7f92016-08-31 14:35:58 -040092 // Adds the given enum value to the set. This has no effect if the
93 // enum value is already in the set.
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050094 void Add(EnumType c) { AddWord(ToWord(c)); }
David Neto909d7f92016-08-31 14:35:58 -040095
96 // Returns true if this enum value is in the set.
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050097 bool Contains(EnumType c) const { return ContainsWord(ToWord(c)); }
David Neto909d7f92016-08-31 14:35:58 -040098
99 // Applies f to each enum in the set, in order from smallest enum
100 // value to largest.
101 void ForEach(std::function<void(EnumType)> f) const {
102 for (uint32_t i = 0; i < 64; ++i) {
103 if (mask_ & AsMask(i)) f(static_cast<EnumType>(i));
104 }
105 if (overflow_) {
106 for (uint32_t c : *overflow_) f(static_cast<EnumType>(c));
107 }
108 }
109
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500110 // Returns true if the set is empty.
111 bool IsEmpty() const {
112 if (mask_) return false;
113 if (overflow_ && !overflow_->empty()) return false;
114 return true;
115 }
116
117 // Returns true if the set contains ANY of the elements of |in_set|,
118 // or if |in_set| is empty.
119 bool HasAnyOf(const EnumSet<EnumType>& in_set) const {
120 if (in_set.IsEmpty()) return true;
121
Lei Zhang063dbea2017-10-25 12:15:51 -0400122 if (mask_ & in_set.mask_) return true;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500123
Lei Zhang063dbea2017-10-25 12:15:51 -0400124 if (!overflow_ || !in_set.overflow_) return false;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500125
126 for (uint32_t item : *in_set.overflow_) {
Lei Zhang063dbea2017-10-25 12:15:51 -0400127 if (overflow_->find(item) != overflow_->end()) return true;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500128 }
129
130 return false;
131 }
132
David Neto909d7f92016-08-31 14:35:58 -0400133 private:
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500134 // Adds the given enum value (as a 32-bit word) to the set. This has no
135 // effect if the enum value is already in the set.
136 void AddWord(uint32_t word) {
137 if (auto new_bits = AsMask(word)) {
138 mask_ |= new_bits;
139 } else {
140 Overflow().insert(word);
141 }
142 }
143
144 // Returns true if the enum represented as a 32-bit word is in the set.
145 bool ContainsWord(uint32_t word) const {
146 // We shouldn't call Overflow() since this is a const method.
147 if (auto bits = AsMask(word)) {
148 return (mask_ & bits) != 0;
149 } else if (auto overflow = overflow_.get()) {
150 return overflow->find(word) != overflow->end();
151 }
152 // The word is large, but the set doesn't have large members, so
153 // it doesn't have an overflow set.
154 return false;
155 }
156
David Neto909d7f92016-08-31 14:35:58 -0400157 // Returns the enum value as a uint32_t.
158 uint32_t ToWord(EnumType value) const {
159 static_assert(sizeof(EnumType) <= sizeof(uint32_t),
160 "EnumType must statically castable to uint32_t");
161 return static_cast<uint32_t>(value);
162 }
163
164 // Determines whether the given enum value can be represented
165 // as a bit in a uint64_t mask. If so, then returns that mask bit.
166 // Otherwise, returns 0.
167 uint64_t AsMask(uint32_t word) const {
168 if (word > 63) return 0;
169 return uint64_t(1) << word;
170 }
171
172 // Ensures that overflow_set_ references a set. A new empty set is
173 // allocated if one doesn't exist yet. Returns overflow_set_.
174 OverflowSetType& Overflow() {
175 if (overflow_.get() == nullptr) {
dan sinclair1963a2d2018-08-14 15:01:50 -0400176 overflow_ = MakeUnique<OverflowSetType>();
David Neto909d7f92016-08-31 14:35:58 -0400177 }
178 return *overflow_;
179 }
180
181 // Enums with values up to 63 are stored as bits in this mask.
182 uint64_t mask_ = 0;
183 // Enums with values larger than 63 are stored in this set.
184 // This set should normally be empty or very small.
185 std::unique_ptr<OverflowSetType> overflow_ = {};
186};
187
188// A set of SpvCapability, optimized for small capability values.
189using CapabilitySet = EnumSet<SpvCapability>;
190
dan sinclair3dad1cd2018-07-07 09:38:00 -0400191} // namespace spvtools
David Neto909d7f92016-08-31 14:35:58 -0400192
dan sinclair58a68762018-08-03 08:05:33 -0400193#endif // SOURCE_ENUM_SET_H_