1 | | | import 'package:meta/meta.dart'; |
2 | | |
|
3 | | | import 'converter.dart'; |
4 | | |
|
5 | | | @internal |
6 | | | class LazyInPlaceMap<K, V> implements Map<K, V> { |
7 | | 2 | LazyInPlaceMap(this._data, this._vcast); |
8 | | |
|
9 | | | final Map<dynamic, dynamic> _data; |
10 | | | final Cast<V> _vcast; |
11 | | |
|
12 | | 1 | @override |
13 | | 1 | Iterable<MapEntry<K, V>> get entries => |
14 | | 6 | keys.map((k) => MapEntry(k, _get(k) as V)); |
15 | | |
|
16 | | 1 | @override |
17 | | 3 | bool get isEmpty => _data.isEmpty; |
18 | | |
|
19 | | 1 | @override |
20 | | 3 | bool get isNotEmpty => _data.isNotEmpty; |
21 | | |
|
22 | | 1 | @override |
23 | | 4 | Iterable<K> get keys => _data.keys.cast<K>(); |
24 | | |
|
25 | | 1 | @override |
26 | | 3 | int get length => _data.length; |
27 | | |
|
28 | | 1 | @override |
29 | | 1 | Iterable<V> get values sync* { |
30 | | 4 | final keys = _data.keys.toList(); |
31 | | 4 | for (var i = 0; i < keys.length; i++) { |
32 | | 3 | yield _get(keys[i]) as V; |
33 | | | } |
34 | | 1 | } |
35 | | |
|
36 | | 1 | @override |
37 | | 2 | V? operator [](Object? key) => _get(key); |
38 | | |
|
39 | | 1 | @override |
40 | | 3 | void operator []=(K key, V value) => _data[key] = value; |
41 | | |
|
42 | | 1 | @override |
43 | | 3 | void addAll(Map<K, V> other) => _data.addAll(other); |
44 | | |
|
45 | | 1 | @override |
46 | | 1 | void addEntries(Iterable<MapEntry<K, V>> newEntries) => |
47 | | 3 | _data.addEntries(newEntries); |
48 | | |
|
49 | | 1 | @override |
50 | | 3 | Map<RK, RV> cast<RK, RV>() => _forceCast().cast<RK, RV>(); |
51 | | |
|
52 | | 1 | @override |
53 | | 3 | void clear() => _data.clear(); |
54 | | |
|
55 | | 1 | @override |
56 | | 3 | bool containsKey(Object? key) => _data.containsKey(key); |
57 | | |
|
58 | | 1 | @override |
59 | | 7 | bool containsValue(Object? value) => _data.keys.any((k) => value == _get(k)); |
60 | | |
|
61 | | 1 | @override |
62 | | 1 | void forEach(void Function(K key, V value) action) { |
63 | | 4 | for (var k in _data.keys) { |
64 | | 3 | action(k as K, _get(k) as V); |
65 | | | } |
66 | | 1 | } |
67 | | |
|
68 | | 1 | @override |
69 | | 1 | Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> Function(K key, V value) convert) { |
70 | | 5 | final r = <K2, V2>{}, keys = _data.keys.toList(); |
71 | | 5 | for (var i = keys.length - 1; i >= 0; i--) { |
72 | | 4 | final k = keys[i], e = convert(k as K, _get(k) as V); |
73 | | 4 | r[e.key] = e.value; |
74 | | | } |
75 | | 1 | return r; |
76 | | 1 | } |
77 | | |
|
78 | | 1 | @override |
79 | | 1 | V putIfAbsent(K key, V Function() ifAbsent) => |
80 | | 3 | _data.putIfAbsent(key, ifAbsent); |
81 | | |
|
82 | | 1 | @override |
83 | | 3 | V? remove(Object? key) => _data.remove(key); |
84 | | |
|
85 | | 1 | @override |
86 | | 1 | void removeWhere(bool Function(K key, V value) test) { |
87 | | 4 | final keys = _data.keys.toList(); |
88 | | 5 | for (var i = keys.length - 1; i >= 0; i--) { |
89 | | 3 | final k = keys[i], v = _get(k); |
90 | | 2 | if (test(k as K, v as V)) { |
91 | | 3 | _data.remove(k); |
92 | | | } |
93 | | | } |
94 | | 1 | } |
95 | | |
|
96 | | 1 | @override |
97 | | 1 | String toString() { |
98 | | 2 | _forceCast(); |
99 | | 3 | return _data.toString(); |
100 | | 1 | } |
101 | | |
|
102 | | 1 | @override |
103 | | 1 | V update(K key, V Function(V value) update, {V Function()? ifAbsent}) => |
104 | | 5 | _data.update(key, (v) => (v != null && v is! V) ? _vcast(v) : v, |
105 | | 1 | ifAbsent: ifAbsent); |
106 | | |
|
107 | | 1 | @override |
108 | | 1 | void updateAll(V Function(K key, V value) update) { |
109 | | 4 | final keys = _data.keys.toList(); |
110 | | 5 | for (var i = keys.length - 1; i >= 0; i--) { |
111 | | 3 | final k = keys[i], v = _get(k); |
112 | | 4 | _data[k] = update(k as K, v as V); |
113 | | | } |
114 | | 1 | } |
115 | | |
|
116 | | 2 | Map<dynamic, dynamic> _forceCast() { |
117 | | 4 | var keys = _data.keys.toList(); |
118 | | 5 | for (var i = keys.length - 1; i >= 0; i--) { |
119 | | 3 | _get(keys[i]); |
120 | | | } |
121 | | 2 | return _data; |
122 | | 1 | } |
123 | | |
|
124 | | 2 | V? _get(Object? key) { |
125 | | 3 | dynamic v = _data[key]; |
126 | | 2 | if (v != null && v is! V) { |
127 | | 2 | v = _vcast(v); |
128 | | 2 | _data[key] = v; |
129 | | | } |
130 | | 1 | return v; |
131 | | 1 | } |
132 | | | } |