Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,24 @@
procedure deepcopy(A, cache) #: return a deepcopy of A
local k
/cache := table() # used to handle multireferenced objects
if \cache[A] then return cache[A]
case type(A) of {
"table"|"list": {
cache[A] := copy(A)
every cache[A][k := key(A)] := deepcopy(A[k], cache)
}
"set": {
cache[A] := set()
every insert(cache[A], deepcopy(!A, cache))
}
default: { # records and objects (encoded as records)
cache[A] := copy(A)
if match("record ",image(A)) then {
every cache[A][k := key(A)] := deepcopy(A[k], cache)
}
}
}
return .cache[A]
end

View file

@ -0,0 +1,54 @@
link printf,ximage
procedure main()
knot := makeknot() # create a structure with loops
knota := knot # copy by assignment (reference)
knotc := copy(knot) # built-in copy (shallow)
knotdc := deepcopy(knot) # deep copy
showdeep("knota (assignment) vs. knot",knota,knot)
showdeep("knotc (copy) vs. knot",knotc,knot)
showdeep("knotdc (deepcopy) vs. knot",knotdc,knot)
xdump("knot (original)",knot)
xdump("knota (assignment)",knota)
xdump("knotc (copy)",knotc)
xdump("knotdc (deepcopy)",knotdc)
end
record rec1(a,b,c) # record for example
class Class1(a1,a2) # class - looks like a record under the covers
method one()
self.a1 := 1
return
end
initially
self.a1 := 0
end
procedure makeknot() #: return a homogeneous structure with loops
L := [9,8,7]
T := table()
T["a"] := 1
R := rec1(T)
S := set(R)
C := Class1()
C.one()
T["knot"] := [L,R,S,C]
put(L,R,S,T,C)
return L
end
procedure showdeep(tag,XC,X) #: demo to show (non-)equivalence of list elements
printf("Analysis of copy depth for %s:\n",tag)
showequiv(XC,X)
every showequiv(XC[i := 1 to *X],X[i])
end
procedure showequiv(x,y) #: show (non-)equivalence of two values
return printf(" %i %s %i\n",x,if x === y then "===" else "~===",y)
end