blob: e4ef297cd77cd0d38c9034a5699382b55776eaba [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
72 // Adds the given enum value to the set. This has no effect if the
73 // enum value is already in the set.
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050074 void Add(EnumType c) { AddWord(ToWord(c)); }
David Neto909d7f92016-08-31 14:35:58 -040075
76 // Returns true if this enum value is in the set.
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050077 bool Contains(EnumType c) const { return ContainsWord(ToWord(c)); }
David Neto909d7f92016-08-31 14:35:58 -040078
79 // Applies f to each enum in the set, in order from smallest enum
80 // value to largest.
81 void ForEach(std::function<void(EnumType)> f) const {
82 for (uint32_t i = 0; i < 64; ++i) {
83 if (mask_ & AsMask(i)) f(static_cast<EnumType>(i));
84 }
85 if (overflow_) {
86 for (uint32_t c : *overflow_) f(static_cast<EnumType>(c));
87 }
88 }
89
Andrey Tuganov1fb8c372017-03-09 18:24:35 -050090 // Returns true if the set is empty.
91 bool IsEmpty() const {
92 if (mask_) return false;
93 if (overflow_ && !overflow_->empty()) return false;
94 return true;
95 }
96
97 // Returns true if the set contains ANY of the elements of |in_set|,
98 // or if |in_set| is empty.
99 bool HasAnyOf(const EnumSet<EnumType>& in_set) const {
100 if (in_set.IsEmpty()) return true;
101
Lei Zhang063dbea2017-10-25 12:15:51 -0400102 if (mask_ & in_set.mask_) return true;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500103
Lei Zhang063dbea2017-10-25 12:15:51 -0400104 if (!overflow_ || !in_set.overflow_) return false;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500105
106 for (uint32_t item : *in_set.overflow_) {
Lei Zhang063dbea2017-10-25 12:15:51 -0400107 if (overflow_->find(item) != overflow_->end()) return true;
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500108 }
109
110 return false;
111 }
112
David Neto909d7f92016-08-31 14:35:58 -0400113 private:
Andrey Tuganov1fb8c372017-03-09 18:24:35 -0500114 // Adds the given enum value (as a 32-bit word) to the set. This has no
115 // effect if the enum value is already in the set.
116 void AddWord(uint32_t word) {
117 if (auto new_bits = AsMask(word)) {
118 mask_ |= new_bits;
119 } else {
120 Overflow().insert(word);
121 }
122 }
123
124 // Returns true if the enum represented as a 32-bit word is in the set.
125 bool ContainsWord(uint32_t word) const {
126 // We shouldn't call Overflow() since this is a const method.
127 if (auto bits = AsMask(word)) {
128 return (mask_ & bits) != 0;
129 } else if (auto overflow = overflow_.get()) {
130 return overflow->find(word) != overflow->end();
131 }
132 // The word is large, but the set doesn't have large members, so
133 // it doesn't have an overflow set.
134 return false;
135 }
136
David Neto909d7f92016-08-31 14:35:58 -0400137 // Returns the enum value as a uint32_t.
138 uint32_t ToWord(EnumType value) const {
139 static_assert(sizeof(EnumType) <= sizeof(uint32_t),
140 "EnumType must statically castable to uint32_t");
141 return static_cast<uint32_t>(value);
142 }
143
144 // Determines whether the given enum value can be represented
145 // as a bit in a uint64_t mask. If so, then returns that mask bit.
146 // Otherwise, returns 0.
147 uint64_t AsMask(uint32_t word) const {
148 if (word > 63) return 0;
149 return uint64_t(1) << word;
150 }
151
152 // Ensures that overflow_set_ references a set. A new empty set is
153 // allocated if one doesn't exist yet. Returns overflow_set_.
154 OverflowSetType& Overflow() {
155 if (overflow_.get() == nullptr) {
dan sinclair1963a2d2018-08-14 15:01:50 -0400156 overflow_ = MakeUnique<OverflowSetType>();
David Neto909d7f92016-08-31 14:35:58 -0400157 }
158 return *overflow_;
159 }
160
161 // Enums with values up to 63 are stored as bits in this mask.
162 uint64_t mask_ = 0;
163 // Enums with values larger than 63 are stored in this set.
164 // This set should normally be empty or very small.
165 std::unique_ptr<OverflowSetType> overflow_ = {};
166};
167
168// A set of SpvCapability, optimized for small capability values.
169using CapabilitySet = EnumSet<SpvCapability>;
170
dan sinclair3dad1cd2018-07-07 09:38:00 -0400171} // namespace spvtools
David Neto909d7f92016-08-31 14:35:58 -0400172
dan sinclair58a68762018-08-03 08:05:33 -0400173#endif // SOURCE_ENUM_SET_H_