Skip to content

Commit 6a12161

Browse files
authored
Rewrite E2 table clone function/partial table extension modernization (#3195)
* Rewrite table copy functions Add shallow table copying Add table perf function Minor optimizations and cleanups * E2descriptions * Remove references to arrayCopy It was a stub, NOT AI-generated. I swear. * Combine these loops * Remove unnecessary return statement * Add test
1 parent 99271ee commit 6a12161

File tree

3 files changed

+334
-130
lines changed

3 files changed

+334
-130
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
## SHOULD_PASS:EXECUTE
2+
@strict
3+
4+
let Dummy = function() {}
5+
6+
# Clone equality
7+
let OT = table(1 = 10, 2 = 20, 3 = "a", 4 = "b", 5 = Dummy,
8+
"a" = "A", "b" = "B", "c" = array(1, 2, 3), "d" = table("a" = 1, "b" = 2, "c" = 3), "e" = Dummy)
9+
10+
let T = OT
11+
let T2 = T:clone()
12+
13+
assert(T:count() == T2:count())
14+
15+
foreach(I:number, V:number = T) {
16+
assert(T2[I, number] == V)
17+
}
18+
19+
foreach(I:number, V:string = T) {
20+
assert(T2[I, string] == V)
21+
}
22+
23+
foreach(K:string, V:string = T) {
24+
assert(T2[K, string] == V)
25+
}
26+
27+
# Compare array by value
28+
29+
let A1 = T["c", array]
30+
let A2 = T2["c", array]
31+
32+
assert(A1 != A2)
33+
34+
assert(A1:count() == A2:count())
35+
36+
foreach(I:number, V:number = A1) {
37+
assert(A2[I, number] == V)
38+
}
39+
40+
# Compare subtable by value
41+
42+
let S1 = T["d", table]
43+
let S2 = T2["d", table]
44+
45+
assert(S1 != S2)
46+
47+
assert(S1:count() == S2:count())
48+
49+
foreach(K:string, V:number = S1) {
50+
assert(S2[K, number] == V)
51+
}
52+
53+
# Compare functions
54+
55+
assert(!!T2[5, function])
56+
assert(!!T2["e", function])
57+
58+
assert(T[5, function] == T2[5, function])
59+
assert(T["e", function] == T2["e", function])
60+
61+
# Object-like tables
62+
63+
T = table(table(noegpobject(), Dummy))
64+
T2 = T:clone()
65+
66+
S1 = T[1, table]
67+
S2 = T2[1, table]
68+
69+
assert(S1 != S2)
70+
assert(S1[1, egpobject] == S2[1, egpobject])
71+
assert(S1[1, egpobject]:toString() == S2[1, egpobject]:toString())
72+
assert(S1[2, function] == S2[2, function])
73+
74+
# Shallow copying
75+
76+
function number compT(T1:table, T2:table) {
77+
foreach(I:number, V:table = T2) {
78+
if(T1[I, table] != V) { return 0 }
79+
}
80+
81+
return 1
82+
}
83+
84+
T = table(table(), table())
85+
T2 = T:copy()
86+
87+
assert(compT(T, T2))
88+
89+
# Merge
90+
91+
T2 = table(3 = table(), 4 = table())
92+
93+
let T3 = T:merge(T2)
94+
95+
assert(compT(T3, T))
96+
assert(compT(T3, T2))
97+
98+
# Add
99+
100+
T3 = T:add(T)
101+
102+
for(I = 1, 2) {
103+
assert(T3[I, table] == T[I, table])
104+
}
105+
for(I = 3, 4) {
106+
assert(T3[I, table] == T[I - 2, table])
107+
}
108+
109+
# Typeids
110+
111+
T = OT:typeids()
112+
T2 = table(1 = "n", 2 = "n", 3 = "s", 4 = "s", 5 = "f",
113+
"a" = "s", "b" = "s", "c" = "r", "d" = "t", "e" = "f")
114+
115+
assert(T:count() == T2:count())
116+
117+
foreach(I:number, V:string = T) {
118+
assert(T2[I, string] == V)
119+
}
120+
121+
foreach(K:string, V:string = T) {
122+
assert(T2[K, string] == V)
123+
}

0 commit comments

Comments
 (0)