Chromium Code Reviews| 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 'utils.dart'; | |
| 8 | |
| 9 /** | |
| 10 * A map whose keys are converted to canonical values of type `C`. | |
| 11 * | |
| 12 * This is useful for using case-insensitive String keys, for example. It's more | |
| 13 * efficient than a [LinkedHashMap] with a custom equality operator because it | |
| 14 * only canonicalizes each once, rather than doing so for each comparison. | |
|
Lasse Reichstein Nielsen
2014/07/01 09:18:15
each once -> each key once
nweiz
2014/07/01 21:21:44
Done.
| |
| 15 */ | |
|
Lasse Reichstein Nielsen
2014/07/01 09:18:15
Document whether the Map allows null as a key (afa
nweiz
2014/07/01 21:21:44
Done.
| |
| 16 class CanonicalizedMap<C, K, V> implements Map<K, V> { | |
| 17 final Function _canonicalize; | |
| 18 | |
| 19 final _base = new Map<C, Pair<K, V>>(); | |
| 20 | |
| 21 /** | |
| 22 * Creates an empty canonicalized map. | |
| 23 * | |
| 24 * The [canonicalize] function should return the canonical value for the given | |
| 25 * key. Keys with the same canonical value are considered equivalent. | |
| 26 */ | |
| 27 CanonicalizedMap(C canonicalize(K key)) | |
| 28 : _canonicalize = canonicalize; | |
| 29 | |
| 30 /** | |
| 31 * Creates a canonicalized map that is initialized with the key/value pairs of | |
| 32 * [other]. | |
| 33 * | |
| 34 * The [canonicalize] function should return the canonical value for the given | |
| 35 * key. Keys with the same canonical value are considered equivalent. | |
| 36 */ | |
| 37 CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key)) | |
| 38 : _canonicalize = canonicalize { | |
| 39 addAll(other); | |
| 40 } | |
| 41 | |
| 42 V operator [](Object key) { | |
| 43 if (key != null && key is! K) return null; | |
| 44 var pair = _base[_canonicalize(key)]; | |
| 45 return pair == null ? null : pair.last; | |
| 46 } | |
| 47 | |
| 48 void operator []=(K key, V value) { | |
| 49 _base[_canonicalize(key)] = new Pair(key, value); | |
| 50 } | |
| 51 | |
| 52 void addAll(Map<K, V> other) { | |
| 53 other.forEach((key, value) => this[key] = value); | |
| 54 } | |
| 55 | |
| 56 void clear() => _base.clear(); | |
|
Lasse Reichstein Nielsen
2014/07/01 09:18:15
I'd prefer "{...}" to "=>" for a void function.
nweiz
2014/07/01 21:21:44
Done.
| |
| 57 | |
| 58 bool containsKey(Object key) { | |
| 59 if (key != null && key is! K) return null; | |
| 60 return _base.containsKey(_canonicalize(key)); | |
| 61 } | |
| 62 | |
| 63 bool containsValue(Object value) => | |
| 64 _base.values.any((pair) => pair.last == value); | |
| 65 | |
| 66 void forEach(void f(K key, V value)) { | |
| 67 _base.forEach((key, pair) => f(pair.first, pair.last)); | |
| 68 } | |
| 69 | |
| 70 bool get isEmpty => _base.isEmpty; | |
| 71 | |
| 72 bool get isNotEmpty => _base.isNotEmpty; | |
| 73 | |
| 74 Iterable<K> get keys => _base.values.map((pair) => pair.first); | |
| 75 | |
| 76 int get length => _base.length; | |
| 77 | |
| 78 V putIfAbsent(K key, V ifAbsent()) { | |
| 79 return _base.putIfAbsent(_canonicalize(key), | |
| 80 () => new Pair(key, ifAbsent())).last; | |
| 81 } | |
| 82 | |
| 83 V remove(Object key) { | |
| 84 if (key != null && key is! K) return null; | |
| 85 var pair = _base.remove(_canonicalize(key)); | |
| 86 return pair == null ? null : pair.last; | |
| 87 } | |
| 88 | |
| 89 Iterable<K> get values => _base.values.map((pair) => pair.last); | |
| 90 | |
| 91 String toString() => "{" + | |
| 92 _base.values.map((pair) => "${pair.first}: ${pair.last}").join(", ") + | |
| 93 "}"; | |
|
Lasse Reichstein Nielsen
2014/07/01 09:18:15
Just use:
String toString() => Maps.mapToString
nweiz
2014/07/01 21:21:44
Done.
| |
| 94 } | |
| OLD | NEW |