OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 dart.pkg.collection.canonicalized_map; | |
6 | |
7 import 'dart:collection'; | |
8 | |
9 import 'utils.dart'; | |
10 | |
11 /** | |
12 * A map whose keys are converted to canonical values of type `C`. | |
13 * | |
14 * This is useful for using case-insensitive String keys, for example. It's more | |
15 * efficient than a [LinkedHashMap] with a custom equality operator because it | |
16 * only canonicalizes each key once, rather than doing so for each comparison. | |
17 * | |
18 * By default, `null` is allowed as a key. It can be forbidden via the | |
19 * `isValidKey` parameter. | |
20 */ | |
21 class CanonicalizedMap<C, K, V> implements Map<K, V> { | |
22 final Function _canonicalize; | |
23 | |
24 final Function _isValidKeyFn; | |
25 | |
26 final _base = new Map<C, Pair<K, V>>(); | |
27 | |
28 /** | |
29 * Creates an empty canonicalized map. | |
30 * | |
31 * The [canonicalize] function should return the canonical value for the given | |
32 * key. Keys with the same canonical value are considered equivalent. | |
33 * | |
34 * The [isValidKey] function is called before calling [canonicalize] for | |
35 * methods that take arbitrary objects. It can be used to filter out keys that | |
36 * can't be canonicalized. | |
37 */ | |
38 CanonicalizedMap(C canonicalize(K key), {bool isValidKey(Object key)}) | |
39 : _canonicalize = canonicalize, | |
40 _isValidKeyFn = isValidKey; | |
41 | |
42 /** | |
43 * Creates a canonicalized map that is initialized with the key/value pairs of | |
44 * [other]. | |
45 * | |
46 * The [canonicalize] function should return the canonical value for the given | |
47 * key. Keys with the same canonical value are considered equivalent. | |
48 * | |
49 * The [isValidKey] function is called before calling [canonicalize] for | |
50 * methods that take arbitrary objects. It can be used to filter out keys that | |
51 * can't be canonicalized. | |
52 */ | |
53 CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key), | |
54 {bool isValidKey(Object key)}) | |
55 : _canonicalize = canonicalize, | |
56 _isValidKeyFn = isValidKey { | |
57 addAll(other); | |
58 } | |
59 | |
60 V operator [](Object key) { | |
61 if (!_isValidKey(key)) return null; | |
62 var pair = _base[_canonicalize(key)]; | |
63 return pair == null ? null : pair.last; | |
64 } | |
65 | |
66 void operator []=(K key, V value) { | |
67 _base[_canonicalize(key)] = new Pair(key, value); | |
68 } | |
69 | |
70 void addAll(Map<K, V> other) { | |
71 other.forEach((key, value) => this[key] = value); | |
72 } | |
73 | |
74 void clear() { | |
75 _base.clear(); | |
76 } | |
77 | |
78 bool containsKey(Object key) { | |
79 if (!_isValidKey(key)) return false; | |
80 return _base.containsKey(_canonicalize(key)); | |
81 } | |
82 | |
83 bool containsValue(Object value) => | |
84 _base.values.any((pair) => pair.last == value); | |
85 | |
86 void forEach(void f(K key, V value)) { | |
87 _base.forEach((key, pair) => f(pair.first, pair.last)); | |
88 } | |
89 | |
90 bool get isEmpty => _base.isEmpty; | |
91 | |
92 bool get isNotEmpty => _base.isNotEmpty; | |
93 | |
94 Iterable<K> get keys => _base.values.map((pair) => pair.first); | |
95 | |
96 int get length => _base.length; | |
97 | |
98 V putIfAbsent(K key, V ifAbsent()) { | |
99 return _base.putIfAbsent(_canonicalize(key), | |
100 () => new Pair(key, ifAbsent())).last; | |
101 } | |
102 | |
103 V remove(Object key) { | |
104 if (!_isValidKey(key)) return null; | |
105 var pair = _base.remove(_canonicalize(key)); | |
106 return pair == null ? null : pair.last; | |
107 } | |
108 | |
109 Iterable<V> get values => _base.values.map((pair) => pair.last); | |
110 | |
111 String toString() => Maps.mapToString(this); | |
112 | |
113 bool _isValidKey(Object key) => (key == null || key is K) && | |
114 (_isValidKeyFn == null || _isValidKeyFn(key)); | |
115 } | |
OLD | NEW |