| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 barback.utils.multiset; | 5 library barback.utils.multiset; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 /// A set of objects where each object can appear multiple times. | 9 /// A set of objects where each object can appear multiple times. |
| 10 /// | 10 /// |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 /// in the set. | 22 /// in the set. |
| 23 final _map = new Map<E, int>(); | 23 final _map = new Map<E, int>(); |
| 24 | 24 |
| 25 Iterator<E> get iterator { | 25 Iterator<E> get iterator { |
| 26 return _map.keys | 26 return _map.keys |
| 27 .expand((element) => | 27 .expand((element) => |
| 28 new Iterable<E>.generate(_map[element], (_) => element)) | 28 new Iterable<E>.generate(_map[element], (_) => element)) |
| 29 .iterator; | 29 .iterator; |
| 30 } | 30 } |
| 31 | 31 |
| 32 Multiset() | 32 Multiset() : super(); |
| 33 : super(); | |
| 34 | 33 |
| 35 /// Creates a multi-set and initializes it using the contents of [other]. | 34 /// Creates a multi-set and initializes it using the contents of [other]. |
| 36 Multiset.from(Iterable<E> other) | 35 Multiset.from(Iterable<E> other) : super() { |
| 37 : super() { | |
| 38 other.forEach(add); | 36 other.forEach(add); |
| 39 } | 37 } |
| 40 | 38 |
| 41 /// Adds [value] to the set. | 39 /// Adds [value] to the set. |
| 42 void add(E value) { | 40 void add(E value) { |
| 43 _map.putIfAbsent(value, () => 0); | 41 _map.putIfAbsent(value, () => 0); |
| 44 _map[value] += 1; | 42 _map[value] += 1; |
| 45 } | 43 } |
| 46 | 44 |
| 47 /// Removes one copy of [value] from the set. | 45 /// Removes one copy of [value] from the set. |
| 48 /// | 46 /// |
| 49 /// Returns whether a copy of [value] was removed, regardless of whether more | 47 /// Returns whether a copy of [value] was removed, regardless of whether more |
| 50 /// copies remain. | 48 /// copies remain. |
| 51 bool remove(E value) { | 49 bool remove(E value) { |
| 52 if (!_map.containsKey(value)) return false; | 50 if (!_map.containsKey(value)) return false; |
| 53 | 51 |
| 54 _map[value] -= 1; | 52 _map[value] -= 1; |
| 55 if (_map[value] == 0) _map.remove(value); | 53 if (_map[value] == 0) _map.remove(value); |
| 56 return true; | 54 return true; |
| 57 } | 55 } |
| 58 | 56 |
| 59 /// Returns whether [value] is in the set. | 57 /// Returns whether [value] is in the set. |
| 60 bool contains(Object value) => _map.containsKey(value); | 58 bool contains(Object value) => _map.containsKey(value); |
| 61 | 59 |
| 62 /// Returns the number of copies of [value] in the set. | 60 /// Returns the number of copies of [value] in the set. |
| 63 int count(E value) => _map.containsKey(value) ? _map[value] : 0; | 61 int count(E value) => _map.containsKey(value) ? _map[value] : 0; |
| 64 } | 62 } |
| OLD | NEW |