Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: components/sync/base/enum_set.h

Issue 2842373004: [sync] Purge directory data when migrating to USS (Closed)
Patch Set: [sync] Add constexpr to EnumSet Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/sync/base/DEPS ('k') | components/sync/base/enum_set_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 } 94 }
95 95
96 // Moves the iterator to the next value in the EnumSet. Good() 96 // Moves the iterator to the next value in the EnumSet. Good()
97 // must hold. Takes linear time. 97 // must hold. Takes linear time.
98 void Inc() { 98 void Inc() {
99 CHECK(Good()); 99 CHECK(Good());
100 i_ = FindNext(i_ + 1); 100 i_ = FindNext(i_ + 1);
101 } 101 }
102 102
103 private: 103 private:
104 friend Iterator EnumSet::First() const; 104 friend constexpr Iterator EnumSet::First() const;
105 105
106 explicit Iterator(const EnumBitSet& enums) 106 explicit Iterator(const EnumBitSet& enums)
107 : enums_(&enums), i_(FindNext(0)) {} 107 : enums_(&enums), i_(FindNext(0)) {}
108 108
109 size_t FindNext(size_t i) { 109 size_t FindNext(size_t i) {
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 EnumSet() {} 120 EnumSet() = default;
121
122 ~EnumSet() = default;
121 123
122 // Recursively chain constructors. Base case is the empty pack. 124 // Recursively chain constructors. Base case is the empty pack.
123 template <class... T> 125 template <class... T>
124 EnumSet(E head, T... tail) : EnumSet(tail...) { 126 EnumSet(E head, T... tail) : EnumSet(tail...) {
125 Put(head); 127 Put(head);
126 } 128 }
127 129
128 // Returns an EnumSet with all possible values. 130 // For a given list of enum values, calculates the equivalent 64 bit bitstring
129 static EnumSet All() { 131 // which is represented as a uint64_t.
130 EnumBitSet enums; 132 constexpr uint64_t bitstring(
131 enums.set(); 133 typename std::initializer_list<E>::const_iterator const& first,
132 return EnumSet(enums); 134 typename std::initializer_list<E>::const_iterator const& last) const {
135 static_assert(
136 kValueCount < 64,
137 "Max number of enum values is 64 for constexpr initialization");
138 return first != last ? ((1ULL << unsigned(ToIndex(*first))) |
139 bitstring(first + 1, last))
140 : 0;
133 } 141 }
134 142
143 constexpr EnumSet(const std::initializer_list<E>& enum_list)
144 : EnumSet(EnumBitSet(bitstring(begin(enum_list), end(enum_list)))) {}
145
146 // Returns an EnumSet with all possible values.
147 static constexpr EnumSet All() { return EnumSet((1ULL << kValueCount) - 1); }
148
135 // Returns an EnumSet with all the values from start to end, inclusive. 149 // Returns an EnumSet with all the values from start to end, inclusive.
136 static EnumSet FromRange(E start, E end) { 150 static constexpr EnumSet FromRange(E start, E end) {
137 EnumSet set; 151 static_assert(
138 set.PutRange(start, end); 152 kValueCount < 64,
139 return set; 153 "Max number of enum values is 64 for constexpr initialization");
154 return EnumSet(
155 EnumBitSet(((1ULL << ToIndex(end)) - (1ULL << ToIndex(start))) |
156 (1ULL << ToIndex(end))));
140 } 157 }
141 158
142 ~EnumSet() {}
143
144 // Copy constructor and assignment welcome. 159 // Copy constructor and assignment welcome.
145 160
146 // Set operations. Put, Retain, and Remove are basically 161 // Set operations. Put, Retain, and Remove are basically
147 // self-mutating versions of Union, Intersection, and Difference 162 // self-mutating versions of Union, Intersection, and Difference
148 // (defined below). 163 // (defined below).
149 164
150 // Adds the given value (which must be in range) to our set. 165 // Adds the given value (which must be in range) to our set.
151 void Put(E value) { enums_.set(ToIndex(value)); } 166 void Put(E value) { enums_.set(ToIndex(value)); }
152 167
153 // Adds all values in the given set to our set. 168 // Adds all values in the given set to our set.
(...skipping 21 matching lines...) Expand all
175 } 190 }
176 } 191 }
177 192
178 // Removes all values in the given set from our set. 193 // Removes all values in the given set from our set.
179 void RemoveAll(EnumSet other) { enums_ &= ~other.enums_; } 194 void RemoveAll(EnumSet other) { enums_ &= ~other.enums_; }
180 195
181 // Removes all values from our set. 196 // Removes all values from our set.
182 void Clear() { enums_.reset(); } 197 void Clear() { enums_.reset(); }
183 198
184 // Returns true iff the given value is in range and a member of our set. 199 // Returns true iff the given value is in range and a member of our set.
185 bool Has(E value) const { 200 constexpr bool Has(E value) const {
186 return InRange(value) && enums_.test(ToIndex(value)); 201 return InRange(value) && enums_[ToIndex(value)];
187 } 202 }
188 203
189 // Returns true iff the given set is a subset of our set. 204 // Returns true iff the given set is a subset of our set.
190 bool HasAll(EnumSet other) const { 205 bool HasAll(EnumSet other) const {
191 return (enums_ & other.enums_) == other.enums_; 206 return (enums_ & other.enums_) == other.enums_;
192 } 207 }
193 208
194 // Returns true iff our set is empty. 209 // Returns true iff our set is empty.
195 bool Empty() const { return !enums_.any(); } 210 bool Empty() const { return !enums_.any(); }
196 211
197 // Returns how many values our set has. 212 // Returns how many values our set has.
198 size_t Size() const { return enums_.count(); } 213 size_t Size() const { return enums_.count(); }
199 214
200 // Returns an iterator pointing to the first element (if any). 215 // Returns an iterator pointing to the first element (if any).
201 Iterator First() const { return Iterator(enums_); } 216 constexpr Iterator First() const { return Iterator(enums_); }
202 217
203 // Returns true iff our set and the given set contain exactly the same values. 218 // Returns true iff our set and the given set contain exactly the same values.
204 bool operator==(const EnumSet& other) const { return enums_ == other.enums_; } 219 bool operator==(const EnumSet& other) const { return enums_ == other.enums_; }
205 220
206 // Returns true iff our set and the given set do not contain exactly the same 221 // Returns true iff our set and the given set do not contain exactly the same
207 // values. 222 // values.
208 bool operator!=(const EnumSet& other) const { return enums_ != other.enums_; } 223 bool operator!=(const EnumSet& other) const { return enums_ != other.enums_; }
209 224
210 private: 225 private:
211 friend EnumSet Union<E, MinEnumValue, MaxEnumValue>(EnumSet set1, 226 friend EnumSet Union<E, MinEnumValue, MaxEnumValue>(EnumSet set1,
212 EnumSet set2); 227 EnumSet set2);
213 friend EnumSet Intersection<E, MinEnumValue, MaxEnumValue>(EnumSet set1, 228 friend EnumSet Intersection<E, MinEnumValue, MaxEnumValue>(EnumSet set1,
214 EnumSet set2); 229 EnumSet set2);
215 friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>(EnumSet set1, 230 friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>(EnumSet set1,
216 EnumSet set2); 231 EnumSet set2);
217 232
218 explicit EnumSet(EnumBitSet enums) : enums_(enums) {} 233 explicit constexpr EnumSet(EnumBitSet enums) : enums_(enums) {}
219 234
220 static bool InRange(E value) { 235 static constexpr bool InRange(E value) {
221 return (value >= MinEnumValue) && (value <= MaxEnumValue); 236 return (value >= MinEnumValue) && (value <= MaxEnumValue);
222 } 237 }
223 238
224 // Converts a value to/from an index into |enums_|. 239 // Converts a value to/from an index into |enums_|.
225 240
226 static size_t ToIndex(E value) { 241 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 242
232 static E FromIndex(size_t i) { 243 static E FromIndex(size_t i) {
233 DCHECK_LT(i, kValueCount); 244 DCHECK_LT(i, kValueCount);
234 return static_cast<E>(MinEnumValue + i); 245 return static_cast<E>(MinEnumValue + i);
235 } 246 }
236 247
237 EnumBitSet enums_; 248 EnumBitSet enums_;
238 }; 249 };
239 250
240 template <typename E, E MinEnumValue, E MaxEnumValue> 251 template <typename E, E MinEnumValue, E MaxEnumValue>
(...skipping 21 matching lines...) Expand all
262 273
263 template <typename E, E Min, E Max> 274 template <typename E, E Min, E Max>
264 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1, 275 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1,
265 EnumSet<E, Min, Max> set2) { 276 EnumSet<E, Min, Max> set2) {
266 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_); 277 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_);
267 } 278 }
268 279
269 } // namespace syncer 280 } // namespace syncer
270 281
271 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_ 282 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_
OLDNEW
« no previous file with comments | « components/sync/base/DEPS ('k') | components/sync/base/enum_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698