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 /** | 5 /** |
6 * The expensive map is a data structure useful for tracking down | 6 * The expensive map is a data structure useful for tracking down |
7 * excessive memory usage due to large maps. It acts as an ordinary | 7 * excessive memory usage due to large maps. It acts as an ordinary |
8 * hash map, but it uses 10 times more memory (by default). | 8 * hash map, but it uses 10 times more memory (by default). |
9 */ | 9 */ |
10 class ExpensiveMap<K, V> implements Map<K, V> { | 10 class ExpensiveMap<K, V> implements Map<K, V> { |
11 final List _maps; | 11 final List _maps; |
12 | 12 |
13 ExpensiveMap([int copies = 10]) : _maps = new List(copies) { | 13 ExpensiveMap([int copies = 10]) : _maps = new List(copies) { |
14 assert(copies > 0); | 14 assert(copies > 0); |
15 for (int i = 0; i < _maps.length; i++) { | 15 for (int i = 0; i < _maps.length; i++) { |
16 _maps[i] = new Map<K, V>(); | 16 _maps[i] = new Map<K, V>(); |
17 } | 17 } |
18 } | 18 } |
19 | 19 |
20 int get length => _maps[0].length; | 20 int get length => _maps[0].length; |
21 bool get isEmpty => _maps[0].isEmpty; | 21 bool get isEmpty => _maps[0].isEmpty; |
22 bool get isNotEmpty => _maps[0].isNotEmpty; | 22 bool get isNotEmpty => _maps[0].isNotEmpty; |
23 | 23 |
24 Iterable<K> get keys => _maps[0].keys; | 24 Iterable<K> get keys => _maps[0].keys; |
25 Iterable<V> get values => _maps[0].values; | 25 Iterable<V> get values => _maps[0].values; |
26 | 26 |
27 bool containsKey(K key) => _maps[0].containsKey(key); | 27 bool containsKey(Object key) => _maps[0].containsKey(key); |
28 bool containsValue(V value) => _maps[0].containsValue(value); | 28 bool containsValue(Object value) => _maps[0].containsValue(value); |
29 | 29 |
30 V operator [](K key) => _maps[0][key]; | 30 V operator [](Object key) => _maps[0][key]; |
31 | 31 |
32 void forEach(void action(K key, V value)) { | 32 void forEach(void action(K key, V value)) { |
33 _maps[0].forEach(action); | 33 _maps[0].forEach(action); |
34 } | 34 } |
35 | 35 |
36 void operator []=(K key, V value) { | 36 void operator []=(K key, V value) { |
37 for (int i = 0; i < _maps.length; i++) { | 37 for (int i = 0; i < _maps.length; i++) { |
38 _maps[i][key] = value; | 38 _maps[i][key] = value; |
39 } | 39 } |
40 } | 40 } |
(...skipping 20 matching lines...) Expand all Loading... |
61 } | 61 } |
62 | 62 |
63 void clear() { | 63 void clear() { |
64 for (int i = 0; i < _maps.length; i++) { | 64 for (int i = 0; i < _maps.length; i++) { |
65 _maps[i].clear(); | 65 _maps[i].clear(); |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 String toString() => "expensive(${_maps[0]}x${_maps.length})"; | 69 String toString() => "expensive(${_maps[0]}x${_maps.length})"; |
70 } | 70 } |
OLD | NEW |