2016-08-31 21:35:58 +03:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 22:33:59 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-08-31 21:35:58 +03:00
|
|
|
//
|
2016-09-01 22:33:59 +03:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-31 21:35:58 +03:00
|
|
|
//
|
2016-09-01 22:33:59 +03:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-08-31 21:35:58 +03:00
|
|
|
|
2018-08-03 15:05:33 +03:00
|
|
|
#ifndef SOURCE_ENUM_SET_H_
|
|
|
|
#define SOURCE_ENUM_SET_H_
|
2016-08-31 21:35:58 +03:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-08-03 22:06:09 +03:00
|
|
|
#include "source/latest_version_spirv_header.h"
|
2018-08-14 22:01:50 +03:00
|
|
|
#include "source/util/make_unique.h"
|
2016-08-31 21:35:58 +03:00
|
|
|
|
2018-07-07 16:38:00 +03:00
|
|
|
namespace spvtools {
|
2016-08-31 21:35:58 +03:00
|
|
|
|
|
|
|
// A set of values of a 32-bit enum type.
|
|
|
|
// It is fast and compact for the common case, where enum values
|
|
|
|
// are at most 63. But it can represent enums with larger values,
|
|
|
|
// as may appear in extensions.
|
|
|
|
template <typename EnumType>
|
|
|
|
class EnumSet {
|
|
|
|
private:
|
|
|
|
// The ForEach method will call the functor on enum values in
|
|
|
|
// enum value order (lowest to highest). To make that easier, use
|
|
|
|
// an ordered set for the overflow values.
|
|
|
|
using OverflowSetType = std::set<uint32_t>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Construct an empty set.
|
2017-03-10 02:24:35 +03:00
|
|
|
EnumSet() {}
|
2016-08-31 21:35:58 +03:00
|
|
|
// Construct an set with just the given enum value.
|
|
|
|
explicit EnumSet(EnumType c) { Add(c); }
|
|
|
|
// Construct an set from an initializer list of enum values.
|
|
|
|
EnumSet(std::initializer_list<EnumType> cs) {
|
|
|
|
for (auto c : cs) Add(c);
|
|
|
|
}
|
2017-10-25 19:15:51 +03:00
|
|
|
EnumSet(uint32_t count, const EnumType* ptr) {
|
|
|
|
for (uint32_t i = 0; i < count; ++i) Add(ptr[i]);
|
|
|
|
}
|
2016-08-31 21:35:58 +03:00
|
|
|
// Copy constructor.
|
|
|
|
EnumSet(const EnumSet& other) { *this = other; }
|
|
|
|
// Move constructor. The moved-from set is emptied.
|
|
|
|
EnumSet(EnumSet&& other) {
|
|
|
|
mask_ = other.mask_;
|
|
|
|
overflow_ = std::move(other.overflow_);
|
|
|
|
other.mask_ = 0;
|
|
|
|
other.overflow_.reset(nullptr);
|
|
|
|
}
|
|
|
|
// Assignment operator.
|
|
|
|
EnumSet& operator=(const EnumSet& other) {
|
|
|
|
if (&other != this) {
|
|
|
|
mask_ = other.mask_;
|
|
|
|
overflow_.reset(other.overflow_ ? new OverflowSetType(*other.overflow_)
|
|
|
|
: nullptr);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds the given enum value to the set. This has no effect if the
|
|
|
|
// enum value is already in the set.
|
2017-03-10 02:24:35 +03:00
|
|
|
void Add(EnumType c) { AddWord(ToWord(c)); }
|
|
|
|
|
|
|
|
// Returns true if this enum value is in the set.
|
|
|
|
bool Contains(EnumType c) const { return ContainsWord(ToWord(c)); }
|
|
|
|
|
|
|
|
// Applies f to each enum in the set, in order from smallest enum
|
|
|
|
// value to largest.
|
|
|
|
void ForEach(std::function<void(EnumType)> f) const {
|
|
|
|
for (uint32_t i = 0; i < 64; ++i) {
|
|
|
|
if (mask_ & AsMask(i)) f(static_cast<EnumType>(i));
|
|
|
|
}
|
|
|
|
if (overflow_) {
|
|
|
|
for (uint32_t c : *overflow_) f(static_cast<EnumType>(c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the set is empty.
|
|
|
|
bool IsEmpty() const {
|
|
|
|
if (mask_) return false;
|
|
|
|
if (overflow_ && !overflow_->empty()) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the set contains ANY of the elements of |in_set|,
|
|
|
|
// or if |in_set| is empty.
|
|
|
|
bool HasAnyOf(const EnumSet<EnumType>& in_set) const {
|
|
|
|
if (in_set.IsEmpty()) return true;
|
|
|
|
|
2017-10-25 19:15:51 +03:00
|
|
|
if (mask_ & in_set.mask_) return true;
|
2017-03-10 02:24:35 +03:00
|
|
|
|
2017-10-25 19:15:51 +03:00
|
|
|
if (!overflow_ || !in_set.overflow_) return false;
|
2017-03-10 02:24:35 +03:00
|
|
|
|
|
|
|
for (uint32_t item : *in_set.overflow_) {
|
2017-10-25 19:15:51 +03:00
|
|
|
if (overflow_->find(item) != overflow_->end()) return true;
|
2017-03-10 02:24:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-08-31 21:35:58 +03:00
|
|
|
// Adds the given enum value (as a 32-bit word) to the set. This has no
|
|
|
|
// effect if the enum value is already in the set.
|
2017-03-10 02:24:35 +03:00
|
|
|
void AddWord(uint32_t word) {
|
2016-08-31 21:35:58 +03:00
|
|
|
if (auto new_bits = AsMask(word)) {
|
|
|
|
mask_ |= new_bits;
|
|
|
|
} else {
|
|
|
|
Overflow().insert(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the enum represented as a 32-bit word is in the set.
|
2017-03-10 02:24:35 +03:00
|
|
|
bool ContainsWord(uint32_t word) const {
|
2016-08-31 21:35:58 +03:00
|
|
|
// We shouldn't call Overflow() since this is a const method.
|
|
|
|
if (auto bits = AsMask(word)) {
|
2016-10-27 23:09:06 +03:00
|
|
|
return (mask_ & bits) != 0;
|
2016-08-31 21:35:58 +03:00
|
|
|
} else if (auto overflow = overflow_.get()) {
|
|
|
|
return overflow->find(word) != overflow->end();
|
|
|
|
}
|
|
|
|
// The word is large, but the set doesn't have large members, so
|
|
|
|
// it doesn't have an overflow set.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the enum value as a uint32_t.
|
|
|
|
uint32_t ToWord(EnumType value) const {
|
|
|
|
static_assert(sizeof(EnumType) <= sizeof(uint32_t),
|
|
|
|
"EnumType must statically castable to uint32_t");
|
|
|
|
return static_cast<uint32_t>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determines whether the given enum value can be represented
|
|
|
|
// as a bit in a uint64_t mask. If so, then returns that mask bit.
|
|
|
|
// Otherwise, returns 0.
|
|
|
|
uint64_t AsMask(uint32_t word) const {
|
|
|
|
if (word > 63) return 0;
|
|
|
|
return uint64_t(1) << word;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures that overflow_set_ references a set. A new empty set is
|
|
|
|
// allocated if one doesn't exist yet. Returns overflow_set_.
|
|
|
|
OverflowSetType& Overflow() {
|
|
|
|
if (overflow_.get() == nullptr) {
|
2018-08-14 22:01:50 +03:00
|
|
|
overflow_ = MakeUnique<OverflowSetType>();
|
2016-08-31 21:35:58 +03:00
|
|
|
}
|
|
|
|
return *overflow_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enums with values up to 63 are stored as bits in this mask.
|
|
|
|
uint64_t mask_ = 0;
|
|
|
|
// Enums with values larger than 63 are stored in this set.
|
|
|
|
// This set should normally be empty or very small.
|
|
|
|
std::unique_ptr<OverflowSetType> overflow_ = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
// A set of SpvCapability, optimized for small capability values.
|
|
|
|
using CapabilitySet = EnumSet<SpvCapability>;
|
|
|
|
|
2018-07-07 16:38:00 +03:00
|
|
|
} // namespace spvtools
|
2016-08-31 21:35:58 +03:00
|
|
|
|
2018-08-03 15:05:33 +03:00
|
|
|
#endif // SOURCE_ENUM_SET_H_
|