OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SYNC_BASE_ENUM_SET_H_ | 5 #ifndef COMPONENTS_SYNC_BASE_ENUM_SET_H_ |
6 #define COMPONENTS_SYNC_BASE_ENUM_SET_H_ | 6 #define COMPONENTS_SYNC_BASE_ENUM_SET_H_ |
7 | 7 |
8 #include <bitset> | 8 #include <bitset> |
9 #include <cstddef> | 9 #include <cstddef> |
10 #include <string> | 10 #include <string> |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 while ((i < kValueCount) && !enums_->test(i)) { | 110 while ((i < kValueCount) && !enums_->test(i)) { |
111 ++i; | 111 ++i; |
112 } | 112 } |
113 return i; | 113 return i; |
114 } | 114 } |
115 | 115 |
116 const EnumBitSet* enums_; | 116 const EnumBitSet* enums_; |
117 size_t i_; | 117 size_t i_; |
118 }; | 118 }; |
119 | 119 |
120 // You can construct an EnumSet with 0, 1, 2, or 3 initial values. | |
121 | |
122 EnumSet() {} | 120 EnumSet() {} |
123 | 121 |
124 explicit EnumSet(E value) { Put(value); } | 122 // Recursively chain constructors. Base case is the empty pack. |
125 | 123 template <class... T> |
126 EnumSet(E value1, E value2) { | 124 EnumSet(E head, T... tail) : EnumSet(tail...) { |
127 Put(value1); | 125 Put(head); |
128 Put(value2); | |
129 } | |
130 | |
131 EnumSet(E value1, E value2, E value3) { | |
132 Put(value1); | |
133 Put(value2); | |
134 Put(value3); | |
135 } | 126 } |
136 | 127 |
137 // Returns an EnumSet with all possible values. | 128 // Returns an EnumSet with all possible values. |
138 static EnumSet All() { | 129 static EnumSet All() { |
139 EnumBitSet enums; | 130 EnumBitSet enums; |
140 enums.set(); | 131 enums.set(); |
141 return EnumSet(enums); | 132 return EnumSet(enums); |
142 } | 133 } |
143 | 134 |
| 135 // Returns an EnumSet with all the values from start to end, inclusive. |
| 136 static EnumSet FromRange(E start, E end) { |
| 137 EnumSet set; |
| 138 set.PutRange(start, end); |
| 139 return set; |
| 140 } |
| 141 |
144 ~EnumSet() {} | 142 ~EnumSet() {} |
145 | 143 |
146 // Copy constructor and assignment welcome. | 144 // Copy constructor and assignment welcome. |
147 | 145 |
148 // Set operations. Put, Retain, and Remove are basically | 146 // Set operations. Put, Retain, and Remove are basically |
149 // self-mutating versions of Union, Intersection, and Difference | 147 // self-mutating versions of Union, Intersection, and Difference |
150 // (defined below). | 148 // (defined below). |
151 | 149 |
152 // Adds the given value (which must be in range) to our set. | 150 // Adds the given value (which must be in range) to our set. |
153 void Put(E value) { enums_.set(ToIndex(value)); } | 151 void Put(E value) { enums_.set(ToIndex(value)); } |
154 | 152 |
155 // Adds all values in the given set to our set. | 153 // Adds all values in the given set to our set. |
156 void PutAll(EnumSet other) { enums_ |= other.enums_; } | 154 void PutAll(EnumSet other) { enums_ |= other.enums_; } |
157 | 155 |
| 156 // Adds all values in the given range to our set, inclusive. |
| 157 void PutRange(E start, E end) { |
| 158 size_t endIndexInclusive = ToIndex(end); |
| 159 DCHECK_LE(ToIndex(start), endIndexInclusive); |
| 160 for (size_t current = ToIndex(start); current <= endIndexInclusive; |
| 161 ++current) { |
| 162 enums_.set(current); |
| 163 } |
| 164 } |
| 165 |
158 // There's no real need for a Retain(E) member function. | 166 // There's no real need for a Retain(E) member function. |
159 | 167 |
160 // Removes all values not in the given set from our set. | 168 // Removes all values not in the given set from our set. |
161 void RetainAll(EnumSet other) { enums_ &= other.enums_; } | 169 void RetainAll(EnumSet other) { enums_ &= other.enums_; } |
162 | 170 |
163 // If the given value is in range, removes it from our set. | 171 // If the given value is in range, removes it from our set. |
164 void Remove(E value) { | 172 void Remove(E value) { |
165 if (InRange(value)) { | 173 if (InRange(value)) { |
166 enums_.reset(ToIndex(value)); | 174 enums_.reset(ToIndex(value)); |
167 } | 175 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 | 262 |
255 template <typename E, E Min, E Max> | 263 template <typename E, E Min, E Max> |
256 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1, | 264 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1, |
257 EnumSet<E, Min, Max> set2) { | 265 EnumSet<E, Min, Max> set2) { |
258 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_); | 266 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_); |
259 } | 267 } |
260 | 268 |
261 } // namespace syncer | 269 } // namespace syncer |
262 | 270 |
263 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_ | 271 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_ |
OLD | NEW |