-
Notifications
You must be signed in to change notification settings - Fork 2
/
t9.html
220 lines (183 loc) · 4.38 KB
/
t9.html
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html>
<body>
<pre id="printarea"></pre>
<script>
// Copyright (c) 2019, Leonid Titov, Mentions Highly Appreciated.
var printarea = document.getElementById("printarea");
// build test structure, quite complex for demo purposes:
var st1 = {
e1: ["Banana", "Orange", "Apple", "Mango"],
e2: {Banana : 0, Orange : 1},
e3: "bac",
e4: null,
e5: undefined,
deep_copy: deep_copy,
deep_copy2: deep_copy,
};
st1.recursive = st1;
arr1 = ["Saab", "Volvo", "BMW"];
st1.e1.push(arr1);
st1.e1[st1.e1.length - 1].push(arr1);
st1.e1[st1.e1.length - 1][st1.e1[st1.e1.length - 1].length - 1].push(arr1); // try to figure this out :)
// now do some play with it:
pr(`Print st1 out:`);
deep_print(st1);
pr(`\n\nDeep copy st1 to st2, and see what happens.\n`);
var st2 = st1.deep_copy();
pr(`Print st1 out:`); deep_print(st1);
pr(`Print st2 out:`); deep_print(st2);
pr(`\n\nNow modify st2 a little.\n`);
delete st2.e2;
st2.e2 = 'fruits were here';
delete st2.deep_copy2;
st2.e1[2] = "Apple the fruit (MODIFIED)";
pr(`\nAnd print both again, see what changed, and what not.\n`);
pr(`Print st1 out:`); deep_print(st1);
pr(`Print st2 out:`); deep_print(st2);
pr(`Done.`);
//
function deep_copy() {
'use strict'; // required for undef test of 'this' below
// Copyright (c) 2019, Leonid Titov, Mentions Highly Appreciated.
var id_cnt = 1;
var all_old_objects = {};
var all_new_objects = {};
var root_obj = this;
if (root_obj === undefined) {
console.log(`deep_copy() error: wrong call context`);
return;
}
var new_obj = copy_obj(root_obj);
for (var id in all_old_objects) {
delete all_old_objects[id].__temp_id;
}
return new_obj;
//
function copy_obj(o) {
var new_obj = {};
if (o.__temp_id === undefined) {
o.__temp_id = id_cnt;
all_old_objects[id_cnt] = o;
all_new_objects[id_cnt] = new_obj;
id_cnt ++;
for (var prop in o) {
if (o[prop] instanceof Array) {
new_obj[prop] = copy_array(o[prop]);
}
else if (o[prop] instanceof Object) {
new_obj[prop] = copy_obj(o[prop]);
}
else if (prop === '__temp_id') {
continue;
}
else {
new_obj[prop] = o[prop];
}
}
}
else {
new_obj = all_new_objects[o.__temp_id];
}
return new_obj;
}
function copy_array(a) {
var new_array = [];
if (a.__temp_id === undefined) {
a.__temp_id = id_cnt;
all_old_objects[id_cnt] = a;
all_new_objects[id_cnt] = new_array;
id_cnt ++;
a.forEach((v,i) => {
if (v instanceof Array) {
new_array[i] = copy_array(v);
}
else if (v instanceof Object) {
new_array[i] = copy_object(v);
}
else {
new_array[i] = v;
}
});
}
else {
new_array = all_new_objects[a.__temp_id];
}
return new_array;
}
}
//
function deep_print(object) {
// Copyright (c) 2019, Leonid Titov, Mentions Highly Appreciated.
var level = 1;
var id_cnt = 1;
var all_old_objects = {};
print_obj(object);
for (var id in all_old_objects) {
delete all_old_objects[id].__temp_prn_id;
}
return;
//
function print_obj(o) {
if (o.__temp_prn_id === undefined) {
o.__temp_prn_id = id_cnt;
all_old_objects[id_cnt] = o;
id_cnt ++;
for (var prop in o) {
pr_lev(prop);
level ++;
if (o[prop] instanceof Array) {
print_array(o[prop]);
}
else if (o[prop] instanceof Object) {
print_obj(o[prop]);
}
else {
pr_lev(o[prop]);
}
level --;
}
}
else {
pr_lev(`recursive link to id=${o.__temp_prn_id}`);
}
}
function print_array(a) {
if (a.__temp_prn_id === undefined) {
a.__temp_prn_id = id_cnt;
all_old_objects[id_cnt] = a;
id_cnt ++;
pr_lev(`array, id=${id_cnt - 1}:`);
level ++;
a.forEach( v => {
if (v instanceof Array) {
print_array(v);
}
else if (v instanceof Object) {
print_object(v);
}
else {
pr_lev(v);
}
});
level --;
}
else {
pr_lev(`recursive link to id=${a.__temp_prn_id}`);
}
}
function pr_lev (text) {
var lev = level;
for (; lev > 0; lev--) {
printarea.innerHTML += "\t";
}
printarea.innerHTML += text + "\n";
}
}
//
function pr (text) {
printarea.innerHTML += text + "\n";
}
</script>
</body>
</html>