-
Notifications
You must be signed in to change notification settings - Fork 2
/
cloner.dart
64 lines (57 loc) · 1.35 KB
/
cloner.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import 'cloner_base.dart';
/// deepcopy with mutable list
class NormalCloner implements ClonerBase {
/// mutable list deepcopy
@override
List deepcopyList(List list) {
List copy = [];
for (final item in list) {
if (item is Map) {
copy.add(deepcopyMap(item));
} else if (item is List) {
copy.add(deepcopyList(item));
} else if (item is Set) {
copy.add(deepcopySet(item));
} else {
copy.add(item);
}
}
return copy;
}
/// mutable map deepcopy
@override
Map deepcopyMap(Map map) {
Map copy = {};
for (final entry in map.entries) {
final key = entry.key;
final value = entry.value;
if (value is Map) {
copy[key] = deepcopyMap(value);
} else if (value is List) {
copy[key] = deepcopyList(value);
} else if (value is Set) {
copy[key] = deepcopySet(value);
} else {
copy[key] = value;
}
}
return copy;
}
/// mutable map deepcopy
@override
Set deepcopySet(Set set) {
Set copy = {};
for (final item in set) {
if (item is Map) {
copy.add(deepcopyMap(item));
} else if (item is List) {
copy.add(deepcopyList(item));
} else if (item is Set) {
copy.add(deepcopySet(item));
} else {
copy.add(item);
}
}
return copy;
}
}