Created
March 5, 2019 10:34
-
-
Save rostopira/005dea51c8b76ac8fe351930a9a0611b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:collection'; | |
class LazyMap<K, V> extends MapMixin<K, V> { | |
final _map = Map<K, V>(); | |
final V Function(K) _generator; | |
LazyMap(this._generator); | |
@override | |
V operator [](Object key) { | |
var res = _map[key]; | |
if (res == null) { | |
res = _generator(key); | |
_map[key] = res; | |
} | |
return res; | |
} | |
@override | |
void operator []=(K key, V value) { | |
_map[key] = value; | |
} | |
@override | |
void clear() { | |
_map.clear(); | |
} | |
@override | |
Iterable<K> get keys => _map.keys; | |
@override | |
V remove(Object key) { | |
return _map.remove(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment