A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
24
Task/Deepcopy/Icon/deepcopy-1.icon
Normal file
24
Task/Deepcopy/Icon/deepcopy-1.icon
Normal 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
|
||||
54
Task/Deepcopy/Icon/deepcopy-2.icon
Normal file
54
Task/Deepcopy/Icon/deepcopy-2.icon
Normal 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
|
||||
6
Task/Deepcopy/J/deepcopy.j
Normal file
6
Task/Deepcopy/J/deepcopy.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
a=:b=: 2 2 2 2 2 NB. two copies of the same array
|
||||
b=: 3 (2)} b NB. modify one of the arrays
|
||||
b
|
||||
2 2 3 2 2
|
||||
a
|
||||
2 2 2 2 2
|
||||
Loading…
Add table
Add a link
Reference in a new issue