Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: pkg/collection/lib/src/canonicalized_map.dart

Issue 350183010: Add a CanonicalizedMap class to pkg/collection. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 '../wrappers.dart';
8
9 /**
10 * A map whose keys are converted to canonical values.
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.
15 */
16 class CanonicalizedMap<K, V> extends DelegatingMap<K, V> {
Lasse Reichstein Nielsen 2014/06/27 06:57:47 Could we allow the canonicalization of a key to no
nweiz 2014/06/30 20:37:46 Done.
17 final Function _canonicalize;
18
19 /**
20 * A map from the canonicalized keys to their original values.
21 */
22 final _keys = new Map<K, V>();
Lasse Reichstein Nielsen 2014/06/27 06:57:47 Should be <K,K>. Have you considered using a
nweiz 2014/06/30 20:37:46 Done, using the Pair class that's used elsewhere i
23
24 /**
25 * Creates an empty canonicalized map.
26 *
27 * The [canonicalize] function should return the canonical value for the given
28 * key. Keys with the same canonical value are considered identical, and the
Lasse Reichstein Nielsen 2014/06/27 06:57:47 identical -> equivalent Ditto below.
nweiz 2014/06/30 20:37:46 Done.
29 * canonical values of the keys are stored in the map.
Lasse Reichstein Nielsen 2014/06/27 06:57:47 "stored in the map" is vague but sounds significan
nweiz 2014/06/30 20:37:46 I just removed this. It was a holdover from before
30 */
31 CanonicalizedMap(K canonicalize(K key))
Lasse Reichstein Nielsen 2014/06/27 06:57:47 I'd also like to have an "bool isValidKey(Object o
Lasse Reichstein Nielsen 2014/06/27 06:57:47 Please see if you can also make the map a paramete
nweiz 2014/06/30 20:37:46 I don't completely understand the purpose of this
nweiz 2014/06/30 20:37:46 I don't think it's worth complicating the API to s
Lasse Reichstein Nielsen 2014/07/01 09:18:14 ACK, if we make the type of canonical object C ins
Lasse Reichstein Nielsen 2014/07/01 09:18:14 I assume that if isValidKey(o) return false, then
nweiz 2014/07/01 21:21:44 Done.
32 : _canonicalize = canonicalize,
33 super({});
34
35 /**
36 * Creates a canonicalized map that contains all key/value pairs of [other].
Lasse Reichstein Nielsen 2014/06/27 06:57:47 Because of the canonicalization, it might contain
nweiz 2014/06/30 20:37:46 Good point. Done.
37 *
38 * The [canonicalize] function should return the canonical value for the given
39 * key. Keys with the same canonical value are considered identical, and the
40 * canonical values of the keys are stored in the map.
41 */
42 CanonicalizedMap.from(Map<K, V> other, K canonicalize(K key))
43 : _canonicalize = canonicalize,
44 super({}) {
45 addAll(other);
46 }
47
48 V operator [](Object key) {
49 if (key != null && key is! K) return null;
50 return super[_canonicalize(key)];
51 }
52
53 void operator []=(K key, V value) {
54 var canonical = _canonicalize(key);
55 _keys[canonical] = key;
56 super[canonical] = value;
57 }
58
59 void addAll(Map<K, V> other) {
60 other.forEach((key, value) => this[key] = value);
61 }
62
63 bool containsKey(Object key) {
64 if (key != null && key is! K) return null;
65 return super.containsKey(_canonicalize(key));
66 }
67
68 void forEach(void f(K key, V value)) {
69 super.forEach((key, value) => f(_keys[key], value));
70 }
71
72 Iterable<K> get keys => _keys.values;
73
74 V putIfAbsent(K key, V ifAbsent()) {
75 var canonical = _canonicalize(key);
76 return super.putIfAbsent(canonical, () {
77 _keys[canonical] = key;
78 return ifAbsent();
79 });
80 }
81
82 V remove(Object key) {
83 if (key != null && key is! K) return null;
84 var canonical = _canonicalize(key);
85 _keys.remove(canonical);
86 return super.remove(canonical);
87 }
88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698