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