Skip to content

Commit

Permalink
Refactored enum_set
Browse files Browse the repository at this point in the history
- removed forgotten file enum_set.cpp
- added IsEmpty and HasAnyOf
- hidden unsafe functions Add(uint32_t), Contains(uint32_t)
- added new tests
  • Loading branch information
Andrey Tuganov authored and dneto0 committed Mar 10, 2017
1 parent 12f5509 commit 1fb8c37
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 107 deletions.
65 changes: 0 additions & 65 deletions source/enum_set.cpp

This file was deleted.

64 changes: 46 additions & 18 deletions source/enum_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EnumSet {

public:
// Construct an empty set.
EnumSet() = default;
EnumSet() {}
// 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.
Expand Down Expand Up @@ -67,21 +67,61 @@ class EnumSet {

// Adds the given enum value to the set. This has no effect if the
// enum value is already in the set.
void Add(EnumType c) { Add(ToWord(c)); }
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;

if (mask_ & in_set.mask_)
return true;

if (!overflow_ || !in_set.overflow_)
return false;

for (uint32_t item : *in_set.overflow_) {
if (overflow_->find(item) != overflow_->end())
return true;
}

return false;
}

private:
// 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.
void Add(uint32_t word) {
void AddWord(uint32_t word) {
if (auto new_bits = AsMask(word)) {
mask_ |= new_bits;
} else {
Overflow().insert(word);
}
}

// Returns true if this enum value is in the set.
bool Contains(EnumType c) const { return Contains(ToWord(c)); }
// Returns true if the enum represented as a 32-bit word is in the set.
bool Contains(uint32_t word) const {
bool ContainsWord(uint32_t word) const {
// We shouldn't call Overflow() since this is a const method.
if (auto bits = AsMask(word)) {
return (mask_ & bits) != 0;
Expand All @@ -93,18 +133,6 @@ class EnumSet {
return false;
}

// 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));
}
}

private:
// Returns the enum value as a uint32_t.
uint32_t ToWord(EnumType value) const {
static_assert(sizeof(EnumType) <= sizeof(uint32_t),
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ set(TEST_SOURCES
binary_strnlen_s_test.cpp
binary_to_text_test.cpp
binary_to_text.literal_test.cpp
capability_set_test.cpp
comment_test.cpp
enum_set_test.cpp
ext_inst.glsl_test.cpp
ext_inst.opencl_test.cpp
fix_word_test.cpp
Expand Down
Loading

0 comments on commit 1fb8c37

Please sign in to comment.