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

Side by Side Diff: pkg/barback/lib/src/multi_set.dart

Issue 26598002: Track the original assets more efficiently in Phase. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/barback/lib/src/phase.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library barback.multi_set;
6
7 import 'dart:collection';
8
9 /// A set of objects where each object can appear multiple times.
10 ///
11 /// Like a set, this has amortized O(1) insertion, removal, and
12 /// existence-checking of elements. Counting the number of copies of an element
13 /// in the set is also amortized O(1).
14 ///
15 /// Distinct elements retain insertion order. Additional copies of an element
16 /// beyond the first are grouped with the original element.
17 class MultiSet<E> extends IterableBase<E> {
Bob Nystrom 2013/10/09 17:04:48 Nit, but "multi" is a prefix, not a word, so this
nweiz 2013/10/15 21:32:06 Renamed to "Multiset".
18 /// A map from each element in the set to the number of copies of that element
19 /// in the set.
20 final _map = new Map<E, int>();
Bob Nystrom 2013/10/09 17:04:48 This assumes that all == E's will never need to be
nweiz 2013/10/15 21:32:06 I think it's clear that equality is based on ==, b
21
22 Iterator<E> get iterator {
23 return _map.keys.expand((element) {
24 return new Iterable.generate(_map[element], (_) => element);
25 }).iterator;
26 }
27
28 MultiSet()
29 : super();
30
31 /// Creates a multi-set and initializes it using the contents of [other].
32 MultiSet.from(Iterable<E> other)
33 : super() {
34 other.forEach(add);
35 }
36
37 /// Adds [value] to the set.
38 void add(E value) {
39 _map.putIfAbsent(value, () => 0);
40 _map[value] += 1;
41 }
42
43 /// Removes one copy of [value] from the set.
44 ///
45 /// Returns whether a copy of [value] was removed, regardless of whether more
46 /// copies remain.
47 bool remove(E value) {
48 if (!_map.containsKey(value)) return false;
49
50 _map[value] -= 1;
51 if (_map[value] == 0) _map.remove(value);
52 return true;
53 }
54
55 /// Returns whether [value] is in the set.
56 bool contains(E value) => _map.containsKey(value);
57
58 /// Returns the number of copies of [value] in the set.
59 int count(E value) => _map.containsKey(value) ? _map[value] : 0;
60 }
OLDNEW
« no previous file with comments | « no previous file | pkg/barback/lib/src/phase.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698