| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart2js.util.enumset; | 5 library dart2js.util.enumset; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 /// A set of enum values based on a bit mask of the shifted enum indices. | 9 /// A set of enum values based on a bit mask of the shifted enum indices. |
| 10 abstract class EnumSet<E> { | 10 abstract class EnumSet<E> { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 } | 28 } |
| 29 | 29 |
| 30 const EnumSet._(); | 30 const EnumSet._(); |
| 31 | 31 |
| 32 /// The bit mask of the shifted indices for the enum values in this set. | 32 /// The bit mask of the shifted indices for the enum values in this set. |
| 33 int get value; | 33 int get value; |
| 34 | 34 |
| 35 /// Adds [enumValue] to this set. | 35 /// Adds [enumValue] to this set. |
| 36 void add(E enumValue); | 36 void add(E enumValue); |
| 37 | 37 |
| 38 /// Adds all enum values in [set] to this set. |
| 39 void addAll(EnumSet<E> set); |
| 40 |
| 38 /// Removes [enumValue] from this set. | 41 /// Removes [enumValue] from this set. |
| 39 void remove(E enumValue); | 42 void remove(E enumValue); |
| 40 | 43 |
| 44 /// Removes all enum values in [set] from this set. The set of removed values |
| 45 /// is returned. |
| 46 EnumSet<E> removeAll(EnumSet<E> set); |
| 47 |
| 48 /// Returns a new set containing all values in both this and the [other] set. |
| 49 EnumSet<E> intersection(EnumSet<E> other) { |
| 50 return new EnumSet.fromValue(value & other.value); |
| 51 } |
| 52 |
| 53 /// Returns a new set containing all values in this set that are not in the |
| 54 /// [other] set. |
| 55 EnumSet<E> minus(EnumSet<E> other) { |
| 56 return new EnumSet.fromValue(value & ~other.value); |
| 57 } |
| 58 |
| 41 /// Clears this set. | 59 /// Clears this set. |
| 42 void clear(); | 60 void clear(); |
| 43 | 61 |
| 44 /// Returns `true` if [enumValue] is in this set. | 62 /// Returns `true` if [enumValue] is in this set. |
| 45 bool contains(E enumValue) { | 63 bool contains(E enumValue) { |
| 46 return (value & (1 << (enumValue as dynamic).index)) != 0; | 64 return (value & (1 << (enumValue as dynamic).index)) != 0; |
| 47 } | 65 } |
| 48 | 66 |
| 49 /// Returns an [Iterable] of the values is in this set using [values] to | 67 /// Returns an [Iterable] of the values is in this set using [values] to |
| 50 /// convert the stored indices to enum values. | 68 /// convert the stored indices to enum values. |
| 51 /// | 69 /// |
| 52 /// The method is typically called with the `values` property of the enum | 70 /// The method is typically called with the `values` property of the enum |
| 53 /// class as argument: | 71 /// class as argument: |
| 54 /// | 72 /// |
| 55 /// EnumSet<EnumClass> set = ... | 73 /// EnumSet<EnumClass> set = ... |
| 56 /// Iterable<EnumClass> iterable = set.iterable(EnumClass.values); | 74 /// Iterable<EnumClass> iterable = set.iterable(EnumClass.values); |
| 57 /// | 75 /// |
| 58 Iterable<E> iterable(List<E> values) { | 76 Iterable<E> iterable(List<E> values) { |
| 59 return new _EnumSetIterable(this, values); | 77 return new _EnumSetIterable(this, values); |
| 60 } | 78 } |
| 61 | 79 |
| 62 /// Returns `true` if this and [other] have any elements in common. | 80 /// Returns `true` if this and [other] have any elements in common. |
| 63 bool intersects(EnumSet<E> other) { | 81 bool intersects(EnumSet<E> other) { |
| 64 return (value & other.value) != 0; | 82 return (value & other.value) != 0; |
| 65 } | 83 } |
| 66 | 84 |
| 67 /// Returns `true` if this set is empty. | 85 /// Returns `true` if this set is empty. |
| 68 bool get isEmpty => value == 0; | 86 bool get isEmpty => value == 0; |
| 69 | 87 |
| 88 /// Returns `true` if this set is not empty. |
| 89 bool get isNotEmpty => value != 0; |
| 90 |
| 70 int get hashCode => value.hashCode * 19; | 91 int get hashCode => value.hashCode * 19; |
| 71 | 92 |
| 72 bool operator ==(other) { | 93 bool operator ==(other) { |
| 73 if (identical(this, other)) return true; | 94 if (identical(this, other)) return true; |
| 74 if (other is! EnumSet<E>) return false; | 95 if (other is! EnumSet<E>) return false; |
| 75 return value == other.value; | 96 return value == other.value; |
| 76 } | 97 } |
| 77 | 98 |
| 78 String toString() { | 99 String toString() { |
| 79 if (value == 0) return '0'; | 100 if (value == 0) return '0'; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 96 _EnumSet() : this.fromValue(0); | 117 _EnumSet() : this.fromValue(0); |
| 97 | 118 |
| 98 _EnumSet.fromValue(this._value) : super._(); | 119 _EnumSet.fromValue(this._value) : super._(); |
| 99 | 120 |
| 100 _EnumSet.fromValues(Iterable<E> values) | 121 _EnumSet.fromValues(Iterable<E> values) |
| 101 : this._value = 0, | 122 : this._value = 0, |
| 102 super._() { | 123 super._() { |
| 103 values.forEach(add); | 124 values.forEach(add); |
| 104 } | 125 } |
| 105 | 126 |
| 127 @override |
| 106 int get value => _value; | 128 int get value => _value; |
| 107 | 129 |
| 130 @override |
| 108 void add(E enumValue) { | 131 void add(E enumValue) { |
| 109 _value |= 1 << (enumValue as dynamic).index; | 132 _value |= 1 << (enumValue as dynamic).index; |
| 110 } | 133 } |
| 111 | 134 |
| 135 @override |
| 136 void addAll(EnumSet<E> set) { |
| 137 _value |= set.value; |
| 138 } |
| 139 |
| 140 @override |
| 112 void remove(E enumValue) { | 141 void remove(E enumValue) { |
| 113 _value &= ~(1 << (enumValue as dynamic).index); | 142 _value &= ~(1 << (enumValue as dynamic).index); |
| 114 } | 143 } |
| 115 | 144 |
| 145 @override |
| 146 EnumSet<E> removeAll(EnumSet<E> set) { |
| 147 int removed = _value & set.value; |
| 148 _value &= ~set.value; |
| 149 return new EnumSet<E>.fromValue(removed); |
| 150 } |
| 151 |
| 152 @override |
| 116 void clear() { | 153 void clear() { |
| 117 _value = 0; | 154 _value = 0; |
| 118 } | 155 } |
| 119 } | 156 } |
| 120 | 157 |
| 121 /// Immutable implementation of [EnumSet]. | 158 /// Immutable implementation of [EnumSet]. |
| 122 class _ConstEnumSet<E> extends EnumSet<E> { | 159 class _ConstEnumSet<E> extends EnumSet<E> { |
| 123 final int value; | 160 final int value; |
| 124 | 161 |
| 125 const _ConstEnumSet(this.value) : super._(); | 162 const _ConstEnumSet(this.value) : super._(); |
| 126 | 163 |
| 127 factory _ConstEnumSet.fromValues(Iterable<E> values) { | 164 factory _ConstEnumSet.fromValues(Iterable<E> values) { |
| 128 int value = 0; | 165 int value = 0; |
| 129 void add(E enumValue) { | 166 void add(E enumValue) { |
| 130 if (enumValue != null) { | 167 if (enumValue != null) { |
| 131 value |= 1 << (enumValue as dynamic).index; | 168 value |= 1 << (enumValue as dynamic).index; |
| 132 } | 169 } |
| 133 } | 170 } |
| 134 | 171 |
| 135 values.forEach(add); | 172 values.forEach(add); |
| 136 return new _ConstEnumSet(value); | 173 return new _ConstEnumSet(value); |
| 137 } | 174 } |
| 138 | 175 |
| 139 @override | 176 @override |
| 140 void add(E enumValue) { | 177 void add(E enumValue) { |
| 141 throw new UnsupportedError('EnumSet.add'); | 178 throw new UnsupportedError('EnumSet.add'); |
| 142 } | 179 } |
| 143 | 180 |
| 144 @override | 181 @override |
| 182 void addAll(EnumSet<E> set) { |
| 183 throw new UnsupportedError('EnumSet.addAll'); |
| 184 } |
| 185 |
| 186 @override |
| 145 void clear() { | 187 void clear() { |
| 146 throw new UnsupportedError('EnumSet.clear'); | 188 throw new UnsupportedError('EnumSet.clear'); |
| 147 } | 189 } |
| 148 | 190 |
| 149 @override | 191 @override |
| 150 void remove(E enumValue) { | 192 void remove(E enumValue) { |
| 151 throw new UnsupportedError('EnumSet.remove'); | 193 throw new UnsupportedError('EnumSet.remove'); |
| 152 } | 194 } |
| 195 |
| 196 @override |
| 197 EnumSet<E> removeAll(EnumSet<E> set) { |
| 198 throw new UnsupportedError('EnumSet.removeAll'); |
| 199 } |
| 153 } | 200 } |
| 154 | 201 |
| 155 class _EnumSetIterable<E> extends IterableBase<E> { | 202 class _EnumSetIterable<E> extends IterableBase<E> { |
| 156 final EnumSet<E> _enumSet; | 203 final EnumSet<E> _enumSet; |
| 157 final List<E> _values; | 204 final List<E> _values; |
| 158 | 205 |
| 159 _EnumSetIterable(this._enumSet, this._values); | 206 _EnumSetIterable(this._enumSet, this._values); |
| 160 | 207 |
| 161 @override | 208 @override |
| 162 Iterator<E> get iterator => new _EnumSetIterator(_enumSet.value, _values); | 209 Iterator<E> get iterator => new _EnumSetIterator(_enumSet.value, _values); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 191 _mask >>= 1; | 238 _mask >>= 1; |
| 192 _index--; | 239 _index--; |
| 193 if (_current != null) { | 240 if (_current != null) { |
| 194 break; | 241 break; |
| 195 } | 242 } |
| 196 } | 243 } |
| 197 return _current != null; | 244 return _current != null; |
| 198 } | 245 } |
| 199 } | 246 } |
| 200 } | 247 } |
| OLD | NEW |