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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 EnumSet() {} | 120 EnumSet() {} |
121 | 121 |
122 // Recursively chain constructors. Base case is the empty pack. | 122 ~EnumSet() = default; |
123 | |
124 // Base case for recursive packing of a list of enum values. The uint64_t | |
125 // corresponding to an empty list is 0. | |
126 static constexpr uint64_t bitstring() { return 0ULL; } | |
127 | |
128 // Recursively pack a list of enum values into a uint64 so that we can | |
129 // constexpr construct our bitset. This packing is done by left shifting 1 by | |
skym
2017/05/05 23:15:41
Can you rework/remove the "This packing is done by
Patrick Noland
2017/05/08 20:29:19
Done.
| |
130 // the current enum value and bitwise or-ing with the bitstring representation | |
131 // of the rest of the values. | |
123 template <class... T> | 132 template <class... T> |
124 EnumSet(E head, T... tail) : EnumSet(tail...) { | 133 static constexpr uint64_t bitstring(E head, T... tail) { |
125 Put(head); | 134 return 1ULL << (ToIndex(head)) | bitstring(tail...); |
skym
2017/05/05 23:15:41
Not really understanding the precedence of operato
Patrick Noland
2017/05/08 20:29:19
Done.
| |
135 } | |
136 | |
137 template <class... T> | |
138 constexpr EnumSet(E head, T... tail) | |
139 : EnumSet(EnumBitSet(bitstring(head, tail...))) { | |
140 static_assert(kValueCount < 64, | |
skym
2017/05/05 23:15:41
Is this really the right place for the static asse
Patrick Noland
2017/05/08 20:29:19
Done.
| |
141 "Max number of enum values is 64 for constexpr " | |
142 "initialization. If your EnumSet is > 64 values, you can " | |
143 "fix this by removing the static_asserts and constexpr " | |
144 "qualifiers from this file."); | |
skym
2017/05/05 23:15:41
What impact will this have? Why is this bad?
Patrick Noland
2017/05/08 20:29:19
Done. I explained that it won't affect correctness
| |
126 } | 145 } |
127 | 146 |
128 // Returns an EnumSet with all possible values. | 147 // Returns an EnumSet with all possible values. |
129 static EnumSet All() { | 148 static constexpr EnumSet All() { |
130 EnumBitSet enums; | 149 static_assert(kValueCount < 64, |
131 enums.set(); | 150 "Max number of enum values is 64 for constexpr " |
skym
2017/05/05 23:15:41
Lets not duplicate quite so much. Maybe have a big
Patrick Noland
2017/05/08 20:29:19
Done.
| |
132 return EnumSet(enums); | 151 "initialization. If your EnumSet is > 64 values, you can " |
152 "fix this by removing the static_asserts and constexpr " | |
153 "qualifiers from this file."); | |
154 return EnumSet(EnumBitSet((1ULL << kValueCount) - 1)); | |
133 } | 155 } |
134 | 156 |
135 // Returns an EnumSet with all the values from start to end, inclusive. | 157 // Returns an EnumSet with all the values from start to end, inclusive. |
136 static EnumSet FromRange(E start, E end) { | 158 static constexpr EnumSet FromRange(E start, E end) { |
137 EnumSet set; | 159 static_assert(kValueCount < 64, |
138 set.PutRange(start, end); | 160 "Max number of enum values is 64 for constexpr " |
skym
2017/05/05 23:15:41
same
Patrick Noland
2017/05/08 20:29:19
Done.
| |
139 return set; | 161 "initialization. If your EnumSet is > 64 values, you can " |
162 "fix this by removing the static_asserts and constexpr " | |
163 "qualifiers from this file."); | |
164 return EnumSet( | |
165 EnumBitSet(((1ULL << ToIndex(end)) - (1ULL << ToIndex(start))) | | |
skym
2017/05/05 23:15:41
Omg this is so clever! Love it! What do you think
Patrick Noland
2017/05/08 20:29:19
Done.
| |
166 (1ULL << ToIndex(end)))); | |
140 } | 167 } |
141 | 168 |
142 ~EnumSet() {} | |
143 | |
144 // Copy constructor and assignment welcome. | 169 // Copy constructor and assignment welcome. |
145 | 170 |
146 // Set operations. Put, Retain, and Remove are basically | 171 // Set operations. Put, Retain, and Remove are basically |
147 // self-mutating versions of Union, Intersection, and Difference | 172 // self-mutating versions of Union, Intersection, and Difference |
148 // (defined below). | 173 // (defined below). |
149 | 174 |
150 // Adds the given value (which must be in range) to our set. | 175 // Adds the given value (which must be in range) to our set. |
151 void Put(E value) { enums_.set(ToIndex(value)); } | 176 void Put(E value) { enums_.set(ToIndex(value)); } |
152 | 177 |
153 // Adds all values in the given set to our set. | 178 // Adds all values in the given set to our set. |
(...skipping 21 matching lines...) Expand all Loading... | |
175 } | 200 } |
176 } | 201 } |
177 | 202 |
178 // Removes all values in the given set from our set. | 203 // Removes all values in the given set from our set. |
179 void RemoveAll(EnumSet other) { enums_ &= ~other.enums_; } | 204 void RemoveAll(EnumSet other) { enums_ &= ~other.enums_; } |
180 | 205 |
181 // Removes all values from our set. | 206 // Removes all values from our set. |
182 void Clear() { enums_.reset(); } | 207 void Clear() { enums_.reset(); } |
183 | 208 |
184 // Returns true iff the given value is in range and a member of our set. | 209 // Returns true iff the given value is in range and a member of our set. |
185 bool Has(E value) const { | 210 constexpr bool Has(E value) const { |
186 return InRange(value) && enums_.test(ToIndex(value)); | 211 return InRange(value) && enums_[ToIndex(value)]; |
187 } | 212 } |
188 | 213 |
189 // Returns true iff the given set is a subset of our set. | 214 // Returns true iff the given set is a subset of our set. |
190 bool HasAll(EnumSet other) const { | 215 bool HasAll(EnumSet other) const { |
191 return (enums_ & other.enums_) == other.enums_; | 216 return (enums_ & other.enums_) == other.enums_; |
192 } | 217 } |
193 | 218 |
194 // Returns true iff our set is empty. | 219 // Returns true iff our set is empty. |
195 bool Empty() const { return !enums_.any(); } | 220 bool Empty() const { return !enums_.any(); } |
196 | 221 |
(...skipping 11 matching lines...) Expand all Loading... | |
208 bool operator!=(const EnumSet& other) const { return enums_ != other.enums_; } | 233 bool operator!=(const EnumSet& other) const { return enums_ != other.enums_; } |
209 | 234 |
210 private: | 235 private: |
211 friend EnumSet Union<E, MinEnumValue, MaxEnumValue>(EnumSet set1, | 236 friend EnumSet Union<E, MinEnumValue, MaxEnumValue>(EnumSet set1, |
212 EnumSet set2); | 237 EnumSet set2); |
213 friend EnumSet Intersection<E, MinEnumValue, MaxEnumValue>(EnumSet set1, | 238 friend EnumSet Intersection<E, MinEnumValue, MaxEnumValue>(EnumSet set1, |
214 EnumSet set2); | 239 EnumSet set2); |
215 friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>(EnumSet set1, | 240 friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>(EnumSet set1, |
216 EnumSet set2); | 241 EnumSet set2); |
217 | 242 |
218 explicit EnumSet(EnumBitSet enums) : enums_(enums) {} | 243 explicit constexpr EnumSet(EnumBitSet enums) : enums_(enums) {} |
219 | 244 |
220 static bool InRange(E value) { | 245 static constexpr bool InRange(E value) { |
221 return (value >= MinEnumValue) && (value <= MaxEnumValue); | 246 return (value >= MinEnumValue) && (value <= MaxEnumValue); |
222 } | 247 } |
223 | 248 |
224 // Converts a value to/from an index into |enums_|. | 249 // Converts a value to/from an index into |enums_|. |
225 | 250 |
226 static size_t ToIndex(E value) { | 251 static constexpr size_t ToIndex(E value) { return value - MinEnumValue; } |
227 DCHECK_GE(value, MinEnumValue); | |
228 DCHECK_LE(value, MaxEnumValue); | |
229 return value - MinEnumValue; | |
230 } | |
231 | 252 |
232 static E FromIndex(size_t i) { | 253 static E FromIndex(size_t i) { |
233 DCHECK_LT(i, kValueCount); | 254 DCHECK_LT(i, kValueCount); |
234 return static_cast<E>(MinEnumValue + i); | 255 return static_cast<E>(MinEnumValue + i); |
235 } | 256 } |
236 | 257 |
237 EnumBitSet enums_; | 258 EnumBitSet enums_; |
238 }; | 259 }; |
239 | 260 |
240 template <typename E, E MinEnumValue, E MaxEnumValue> | 261 template <typename E, E MinEnumValue, E MaxEnumValue> |
(...skipping 21 matching lines...) Expand all Loading... | |
262 | 283 |
263 template <typename E, E Min, E Max> | 284 template <typename E, E Min, E Max> |
264 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1, | 285 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1, |
265 EnumSet<E, Min, Max> set2) { | 286 EnumSet<E, Min, Max> set2) { |
266 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_); | 287 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_); |
267 } | 288 } |
268 | 289 |
269 } // namespace syncer | 290 } // namespace syncer |
270 | 291 |
271 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_ | 292 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_ |
OLD | NEW |