OLD | NEW |
| (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 part of dart2js.helpers; | |
6 | |
7 /** | |
8 * The expensive set is a data structure useful for tracking down | |
9 * excessive memory usage due to large sets. It acts as an ordinary | |
10 * hash set, but it uses 10 times more memory (by default). | |
11 */ | |
12 class ExpensiveSet<E> extends IterableBase<E> implements Set<E> { | |
13 | |
14 final List _sets; | |
15 | |
16 ExpensiveSet([int copies = 10]) : _sets = new List(copies) { | |
17 assert(copies > 0); | |
18 for (int i = 0; i < _sets.length; i++) { | |
19 _sets[i] = new Set<E>(); | |
20 } | |
21 } | |
22 | |
23 int get length => _sets[0].length; | |
24 bool get isEmpty => _sets[0].isEmpty; | |
25 bool get isNotEmpty => _sets[0].isNotEmpty; | |
26 | |
27 Iterator<E> get iterator => _sets[0].iterator; | |
28 | |
29 bool contains(Object object) => _sets[0].contains(object); | |
30 E lookup(Object object) => _sets[0].lookup(object); | |
31 | |
32 void forEach(void action(E element)) { | |
33 _sets[0].forEach(action); | |
34 } | |
35 | |
36 bool add(E element) { | |
37 bool result = _sets[0].add(element); | |
38 for (int i = 1; i < _sets.length; i++) { | |
39 _sets[i].add(element); | |
40 } | |
41 return result; | |
42 } | |
43 | |
44 void addAll(Iterable<E> objects) { | |
45 for (E each in objects) { | |
46 add(each); | |
47 } | |
48 } | |
49 | |
50 bool remove(Object object) { | |
51 bool result = _sets[0].remove(object); | |
52 for (int i = 1; i < _sets.length; i++) { | |
53 _sets[i].remove(object); | |
54 } | |
55 return result; | |
56 } | |
57 | |
58 void clear() { | |
59 for (int i = 0; i < _sets.length; i++) { | |
60 _sets[i].clear(); | |
61 } | |
62 } | |
63 | |
64 void removeAll(Iterable<Object> objectsToRemove) { | |
65 for (var each in objectsToRemove) { | |
66 remove(each); | |
67 } | |
68 } | |
69 | |
70 void removeWhere(bool test(E element)) { | |
71 removeAll(this.toList().where((e) => test(e))); | |
72 } | |
73 | |
74 void retainWhere(bool test(E element)) { | |
75 removeAll(toList().where((e) => !test(e))); | |
76 } | |
77 | |
78 bool containsAll(Iterable<Object> other) { | |
79 for (Object object in other) { | |
80 if (!this.contains(object)) return false; | |
81 } | |
82 return true; | |
83 } | |
84 | |
85 Set _newSet() => new ExpensiveSet(_sets.length); | |
86 | |
87 Set<E> intersection(Set<Object> other) { | |
88 Set<E> result = _newSet(); | |
89 if (other.length < this.length) { | |
90 for (var element in other) { | |
91 if (this.contains(element)) result.add(element); | |
92 } | |
93 } else { | |
94 for (E element in this) { | |
95 if (other.contains(element)) result.add(element); | |
96 } | |
97 } | |
98 return result; | |
99 } | |
100 | |
101 Set<E> union(Set<E> other) { | |
102 return _newSet()..addAll(this)..addAll(other); | |
103 } | |
104 | |
105 Set<E> difference(Set<E> other) { | |
106 Set<E> result = _newSet(); | |
107 for (E element in this) { | |
108 if (!other.contains(element)) result.add(element); | |
109 } | |
110 return result; | |
111 } | |
112 | |
113 void retainAll(Iterable objectsToRetain) { | |
114 Set retainSet; | |
115 if (objectsToRetain is Set) { | |
116 retainSet = objectsToRetain; | |
117 } else { | |
118 retainSet = objectsToRetain.toSet(); | |
119 } | |
120 retainWhere(retainSet.contains); | |
121 } | |
122 | |
123 Set<E> toSet() { | |
124 var result = new ExpensiveSet<E>(_sets.length); | |
125 for (int i = 0; i < _sets.length; i++) { | |
126 result._sets[i] = _sets[i].toSet(); | |
127 } | |
128 return result; | |
129 } | |
130 | |
131 String toString() => "expensive(${_sets[0]}x${_sets.length})"; | |
132 } | |
OLD | NEW |