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

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

Issue 1831103004: Fix strong mode warnings. (Closed) Base URL: git@github.com:dart-lang/collection@master
Patch Set: pubspec/changelog Created 4 years, 9 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
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 import 'dart:collection'; 5 import 'dart:collection';
6 6
7 import 'utils.dart'; 7 import 'utils.dart';
8 8
9 typedef C _Canonicalize<C, K>(K key);
10
11 typedef bool _IsValidKey(Object key);
12
9 /// A map whose keys are converted to canonical values of type `C`. 13 /// A map whose keys are converted to canonical values of type `C`.
10 /// 14 ///
11 /// This is useful for using case-insensitive String keys, for example. It's 15 /// This is useful for using case-insensitive String keys, for example. It's
12 /// more efficient than a [LinkedHashMap] with a custom equality operator 16 /// more efficient than a [LinkedHashMap] with a custom equality operator
13 /// because it only canonicalizes each key once, rather than doing so for each 17 /// because it only canonicalizes each key once, rather than doing so for each
14 /// comparison. 18 /// comparison.
15 /// 19 ///
16 /// By default, `null` is allowed as a key. It can be forbidden via the 20 /// By default, `null` is allowed as a key. It can be forbidden via the
17 /// `isValidKey` parameter. 21 /// `isValidKey` parameter.
18 class CanonicalizedMap<C, K, V> implements Map<K, V> { 22 class CanonicalizedMap<C, K, V> implements Map<K, V> {
19 final Function _canonicalize; 23 final _Canonicalize<C, K> _canonicalize;
20 24
21 final Function _isValidKeyFn; 25 final _IsValidKey _isValidKeyFn;
22 26
23 final _base = new Map<C, Pair<K, V>>(); 27 final _base = new Map<C, Pair<K, V>>();
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
28 /// given key. Keys with the same canonical value are considered equivalent. 32 /// given 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
(...skipping 13 matching lines...) Expand all
45 /// that can't be canonicalized. 49 /// that can't be canonicalized.
46 CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key), 50 CanonicalizedMap.from(Map<K, V> other, C canonicalize(K key),
47 {bool isValidKey(Object key)}) 51 {bool isValidKey(Object key)})
48 : _canonicalize = canonicalize, 52 : _canonicalize = canonicalize,
49 _isValidKeyFn = isValidKey { 53 _isValidKeyFn = isValidKey {
50 addAll(other); 54 addAll(other);
51 } 55 }
52 56
53 V operator [](Object key) { 57 V operator [](Object key) {
54 if (!_isValidKey(key)) return null; 58 if (!_isValidKey(key)) return null;
55 var pair = _base[_canonicalize(key)]; 59 var pair = _base[_canonicalize(key as K)];
56 return pair == null ? null : pair.last; 60 return pair == null ? null : pair.last;
57 } 61 }
58 62
59 void operator []=(K key, V value) { 63 void operator []=(K key, V value) {
60 if (!_isValidKey(key)) return; 64 if (!_isValidKey(key)) return;
61 _base[_canonicalize(key)] = new Pair(key, value); 65 _base[_canonicalize(key)] = new Pair(key, value);
62 } 66 }
63 67
64 void addAll(Map<K, V> other) { 68 void addAll(Map<K, V> other) {
65 other.forEach((key, value) => this[key] = value); 69 other.forEach((key, value) => this[key] = value);
66 } 70 }
67 71
68 void clear() { 72 void clear() {
69 _base.clear(); 73 _base.clear();
70 } 74 }
71 75
72 bool containsKey(Object key) { 76 bool containsKey(Object key) {
73 if (!_isValidKey(key)) return false; 77 if (!_isValidKey(key)) return false;
74 return _base.containsKey(_canonicalize(key)); 78 return _base.containsKey(_canonicalize(key as K));
75 } 79 }
76 80
77 bool containsValue(Object value) => 81 bool containsValue(Object value) =>
78 _base.values.any((pair) => pair.last == value); 82 _base.values.any((pair) => pair.last == value);
79 83
80 void forEach(void f(K key, V value)) { 84 void forEach(void f(K key, V value)) {
81 _base.forEach((key, pair) => f(pair.first, pair.last)); 85 _base.forEach((key, pair) => f(pair.first, pair.last));
82 } 86 }
83 87
84 bool get isEmpty => _base.isEmpty; 88 bool get isEmpty => _base.isEmpty;
85 89
86 bool get isNotEmpty => _base.isNotEmpty; 90 bool get isNotEmpty => _base.isNotEmpty;
87 91
88 Iterable<K> get keys => _base.values.map((pair) => pair.first); 92 Iterable<K> get keys => _base.values.map((pair) => pair.first);
89 93
90 int get length => _base.length; 94 int get length => _base.length;
91 95
92 V putIfAbsent(K key, V ifAbsent()) { 96 V putIfAbsent(K key, V ifAbsent()) {
93 return _base.putIfAbsent(_canonicalize(key), 97 return _base.putIfAbsent(_canonicalize(key),
94 () => new Pair(key, ifAbsent())).last; 98 () => new Pair(key, ifAbsent())).last;
95 } 99 }
96 100
97 V remove(Object key) { 101 V remove(Object key) {
98 if (!_isValidKey(key)) return null; 102 if (!_isValidKey(key)) return null;
99 var pair = _base.remove(_canonicalize(key)); 103 var pair = _base.remove(_canonicalize(key as K));
100 return pair == null ? null : pair.last; 104 return pair == null ? null : pair.last;
101 } 105 }
102 106
103 Iterable<V> get values => _base.values.map((pair) => pair.last); 107 Iterable<V> get values => _base.values.map((pair) => pair.last);
104 108
105 String toString() => Maps.mapToString(this); 109 String toString() => Maps.mapToString(this);
106 110
107 bool _isValidKey(Object key) => (key == null || key is K) && 111 bool _isValidKey(Object key) => (key == null || key is K) &&
108 (_isValidKeyFn == null || _isValidKeyFn(key)); 112 (_isValidKeyFn == null || _isValidKeyFn(key));
109 } 113 }
OLDNEW
« lib/src/algorithms.dart ('K') | « lib/src/algorithms.dart ('k') | lib/src/equality.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698