2
* Copyright 2013 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
#ifndef PAGESPEED_KERNEL_BASE_ENUM_SET_H_
20
#define PAGESPEED_KERNEL_BASE_ENUM_SET_H_
25
namespace net_instaweb {
27
// Represents a set of values -- implemented via a bitset.
28
template<typename EnumType, size_t NumEnums> class EnumSet {
30
bool IsSet(EnumType value) const {
31
return bits_.test(static_cast<size_t>(value));
34
// Inserts a new value, returning true if a change was made.
35
bool Insert(EnumType value) {
36
bool result = !IsSet(value);
41
// Inserts a value; no return value.
43
// TODO(jmarantz): change call-sites to Insert and remove this one.
44
void insert(EnumType value) {
45
bits_.set(static_cast<size_t>(value));
48
// Returns true if a change was made.
49
bool Erase(EnumType value) {
50
bool result = IsSet(value);
51
bits_.reset(static_cast<size_t>(value));
55
// Mergess src into this, returning whether this resulted in a change.
56
bool Merge(const EnumSet& src) {
57
// We save the current version of the set in order to see whether
58
// the merge resulted in a change. Note that copying and comparing
59
// the bits is very cheap; probably cheaper than calling count().
62
return bits_ != save.bits_;
65
// Merges the entries *not* set in src into this, returning whether this
66
// resulted in a change.
67
bool MergeInverted(const EnumSet& src) {
70
return bits_ != save.bits_;
73
void EraseSet(const EnumSet& src) {
77
// Sets all the entries to true.
82
// Standard STL-like methods.
83
void clear() { bits_.reset(); }
84
size_t size() const { return bits_.count(); }
85
bool empty() const { return bits_.none(); }
87
// This overload is required for use in EXPECT_EQ in tests.
88
bool operator==(const EnumSet& that) const {
89
return bits_ == that.bits_;
93
typedef std::bitset<NumEnums> BitSet;
96
// Implicit copy and assign will work perfectly and are required.
99
} // namespace net_instaweb
101
#endif // PAGESPEED_KERNEL_BASE_ENUM_SET_H_