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 map is a data structure useful for tracking down | |
9 * excessive memory usage due to large maps. It acts as an ordinary | |
10 * hash map, but it uses 10 times more memory (by default). | |
11 */ | |
12 class ExpensiveMap<K, V> implements Map<K, V> { | |
13 | |
14 final List _maps; | |
15 | |
16 ExpensiveMap([int copies = 10]) : _maps = new List(copies) { | |
17 assert(copies > 0); | |
18 for (int i = 0; i < _maps.length; i++) { | |
19 _maps[i] = new Map<K, V>(); | |
20 } | |
21 } | |
22 | |
23 int get length => _maps[0].length; | |
24 bool get isEmpty => _maps[0].isEmpty; | |
25 bool get isNotEmpty => _maps[0].isNotEmpty; | |
26 | |
27 Iterable<K> get keys => _maps[0].keys; | |
28 Iterable<V> get values => _maps[0].values; | |
29 | |
30 bool containsKey(K key) => _maps[0].containsKey(key); | |
31 bool containsValue(V value) => _maps[0].containsValue(value); | |
32 | |
33 V operator[](K key) => _maps[0][key]; | |
34 | |
35 void forEach(void action(K key, V value)) { | |
36 _maps[0].forEach(action); | |
37 } | |
38 | |
39 void operator[]=(K key, V value) { | |
40 for (int i = 0; i < _maps.length; i++) { | |
41 _maps[i][key] = value; | |
42 } | |
43 } | |
44 | |
45 V putIfAbsent(K key, V ifAbsent()) { | |
46 if (containsKey(key)) return this[key]; | |
47 V value = ifAbsent(); | |
48 this[key] = value; | |
49 return value; | |
50 } | |
51 | |
52 void addAll(Map<K, V> other) { | |
53 for (int i = 0; i < _maps.length; i++) { | |
54 _maps[i].addAll(other); | |
55 } | |
56 } | |
57 | |
58 V remove(Object key) { | |
59 V result = _maps[0].remove(key); | |
60 for (int i = 1; i < _maps.length; i++) { | |
61 _maps[i].remove(key); | |
62 } | |
63 return result; | |
64 } | |
65 | |
66 void clear() { | |
67 for (int i = 0; i < _maps.length; i++) { | |
68 _maps[i].clear(); | |
69 } | |
70 } | |
71 | |
72 String toString() => "expensive(${_maps[0]}x${_maps.length})"; | |
73 } | |
OLD | NEW |