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

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

Issue 1638163002: Modernize the package's style. (Closed) Base URL: git@github.com:dart-lang/collection@master
Patch Set: Code review changes Created 4 years, 11 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
« no previous file with comments | « lib/src/algorithms.dart ('k') | lib/src/comparators.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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 library dart.pkg.collection.canonicalized_map;
6
7 import 'dart:collection'; 5 import 'dart:collection';
8 6
9 import 'utils.dart'; 7 import 'utils.dart';
10 8
11 /** 9 /// A map whose keys are converted to canonical values of type `C`.
12 * A map whose keys are converted to canonical values of type `C`. 10 ///
13 * 11 /// This is useful for using case-insensitive String keys, for example. It's
14 * This is useful for using case-insensitive String keys, for example. It's more 12 /// more efficient than a [LinkedHashMap] with a custom equality operator
15 * efficient than a [LinkedHashMap] with a custom equality operator because it 13 /// because it only canonicalizes each key once, rather than doing so for each
16 * only canonicalizes each key once, rather than doing so for each comparison. 14 /// comparison.
17 * 15 ///
18 * By default, `null` is allowed as a key. It can be forbidden via the 16 /// By default, `null` is allowed as a key. It can be forbidden via the
19 * `isValidKey` parameter. 17 /// `isValidKey` parameter.
20 */
21 class CanonicalizedMap<C, K, V> implements Map<K, V> { 18 class CanonicalizedMap<C, K, V> implements Map<K, V> {
22 final Function _canonicalize; 19 final Function _canonicalize;
23 20
24 final Function _isValidKeyFn; 21 final Function _isValidKeyFn;
25 22
26 final _base = new Map<C, Pair<K, V>>(); 23 final _base = new Map<C, Pair<K, V>>();
27 24
28 /** 25 /// Creates an empty canonicalized map.
29 * Creates an empty canonicalized map. 26 ///
30 * 27 /// The [canonicalize] function should return the canonical value for the
31 * The [canonicalize] function should return the canonical value for the given 28 /// given key. Keys with the same canonical value are considered equivalent.
32 * key. Keys with the same canonical value are considered equivalent. 29 ///
33 * 30 /// The [isValidKey] function is called before calling [canonicalize] for
34 * The [isValidKey] function is called before calling [canonicalize] for 31 /// methods that take arbitrary objects. It can be used to filter out keys
35 * methods that take arbitrary objects. It can be used to filter out keys that 32 /// that can't be canonicalized.
36 * can't be canonicalized.
37 */
38 CanonicalizedMap(C canonicalize(K key), {bool isValidKey(Object key)}) 33 CanonicalizedMap(C canonicalize(K key), {bool isValidKey(Object key)})
39 : _canonicalize = canonicalize, 34 : _canonicalize = canonicalize,
40 _isValidKeyFn = isValidKey; 35 _isValidKeyFn = isValidKey;
41 36
42 /** 37 /// Creates a canonicalized map that is initialized with the key/value pairs
43 * Creates a canonicalized map that is initialized with the key/value pairs of 38 /// of [other].
44 * [other]. 39 ///
45 * 40 /// The [canonicalize] function should return the canonical value for the
46 * The [canonicalize] function should return the canonical value for the given 41 /// given key. Keys with the same canonical value are considered equivalent.
47 * key. Keys with the same canonical value are considered equivalent. 42 ///
48 * 43 /// The [isValidKey] function is called before calling [canonicalize] for
49 * The [isValidKey] function is called before calling [canonicalize] for 44 /// methods that take arbitrary objects. It can be used to filter out keys
50 * methods that take arbitrary objects. It can be used to filter out keys that 45 /// that can't be canonicalized.
51 * can't be canonicalized.
52 */
53 CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key), 46 CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key),
54 {bool isValidKey(Object key)}) 47 {bool isValidKey(Object key)})
55 : _canonicalize = canonicalize, 48 : _canonicalize = canonicalize,
56 _isValidKeyFn = isValidKey { 49 _isValidKeyFn = isValidKey {
57 addAll(other); 50 addAll(other);
58 } 51 }
59 52
60 V operator [](Object key) { 53 V operator [](Object key) {
61 if (!_isValidKey(key)) return null; 54 if (!_isValidKey(key)) return null;
62 var pair = _base[_canonicalize(key)]; 55 var pair = _base[_canonicalize(key)];
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 return pair == null ? null : pair.last; 100 return pair == null ? null : pair.last;
108 } 101 }
109 102
110 Iterable<V> get values => _base.values.map((pair) => pair.last); 103 Iterable<V> get values => _base.values.map((pair) => pair.last);
111 104
112 String toString() => Maps.mapToString(this); 105 String toString() => Maps.mapToString(this);
113 106
114 bool _isValidKey(Object key) => (key == null || key is K) && 107 bool _isValidKey(Object key) => (key == null || key is K) &&
115 (_isValidKeyFn == null || _isValidKeyFn(key)); 108 (_isValidKeyFn == null || _isValidKeyFn(key));
116 } 109 }
OLDNEW
« no previous file with comments | « lib/src/algorithms.dart ('k') | lib/src/comparators.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698