Deepcopy: Difference between revisions
Content added Content deleted
(Added C++ implementation) |
m (→{{header|Phix}}: added syntax colouring the hard way, phix/basics) |
||
Line 1,751: | Line 1,751: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
|||
Handled natively. Phix uses reference counting with copy-on-write semantics; the initial copy is fast even for huge complex and deeply nested structures (copying a single machine-word-sized reference and incrementing a single machine-word-sized reference count), and when a shared object (anything with a refcount>1) is modified, an internal clone of the minimum necessary levels occurs, with all the rest of the structure remaining shared (but obviously still properly protected in the same way). |
Handled natively. Phix uses reference counting with copy-on-write semantics; the initial copy is fast even for huge complex and deeply nested structures (copying a single machine-word-sized reference and incrementing a single machine-word-sized reference count), and when a shared object (anything with a refcount>1) is modified, an internal clone of the minimum necessary levels occurs, with all the rest of the structure remaining shared (but obviously still properly protected in the same way). |
||
<lang Phix> |
<!--<lang Phix>--> |
||
<span style="color: #004080;">object</span> <span style="color: #000000;">a<span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span> |
|||
a = {1,{2,3},"four",{5.6,7,{8.9}}} |
|||
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #0000FF;">{<span style="color: #000000;">2<span style="color: #0000FF;">,<span style="color: #000000;">3<span style="color: #0000FF;">}<span style="color: #0000FF;">,<span style="color: #008000;">"four"<span style="color: #0000FF;">,<span style="color: #0000FF;">{<span style="color: #000000;">5.6<span style="color: #0000FF;">,<span style="color: #000000;">7<span style="color: #0000FF;">,<span style="color: #0000FF;">{<span style="color: #000000;">8.9<span style="color: #0000FF;">}<span style="color: #0000FF;">}<span style="color: #0000FF;">}</span> |
|||
b = a |
|||
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span> |
|||
b[3] = 4 |
|||
<span style="color: #000000;">b<span style="color: #0000FF;">[<span style="color: #000000;">3<span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span> |
|||
?a |
|||
<span style="color: #0000FF;">?<span style="color: #000000;">a</span> |
|||
⚫ | |||
<span style="color: #0000FF;">?<span style="color: #000000;">b |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,766: | Line 1,769: | ||
Should you for some (probably misguided) reason really want it, a recursive clone function might look like this: |
Should you for some (probably misguided) reason really want it, a recursive clone function might look like this: |
||
<lang Phix> |
<!--<lang Phix>--> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">deep_copy<span style="color: #0000FF;">(<span style="color: #004080;">object</span> <span style="color: #000000;">o<span style="color: #0000FF;">)</span> |
|||
object res |
|||
<span style="color: #004080;">object</span> <span style="color: #000000;">res</span> |
|||
if atom(o) then |
|||
<span style="color: #008080;">if</span> <span style="color: #004080;">atom<span style="color: #0000FF;">(<span style="color: #000000;">o<span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> |
|||
res = o |
|||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">o</span> |
|||
else |
|||
<span style="color: #008080;">else</span> |
|||
res = repeat(' ',length(o)) |
|||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat<span style="color: #0000FF;">(<span style="color: #008000;">' '<span style="color: #0000FF;">,<span style="color: #7060A8;">length<span style="color: #0000FF;">(<span style="color: #000000;">o<span style="color: #0000FF;">)<span style="color: #0000FF;">)</span> |
|||
for i=1 to length(o) do |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i<span style="color: #0000FF;">=<span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length<span style="color: #0000FF;">(<span style="color: #000000;">o<span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
res[i] = deep_copy(o[i]) |
|||
<span style="color: #000000;">res<span style="color: #0000FF;">[<span style="color: #000000;">i<span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">deep_copy<span style="color: #0000FF;">(<span style="color: #000000;">o<span style="color: #0000FF;">[<span style="color: #000000;">i<span style="color: #0000FF;">]<span style="color: #0000FF;">)</span> |
|||
end for |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
return res |
|||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span> |
|||
end function |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
object c = deep_copy(b) |
|||
<span style="color: #004080;">object</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">deep_copy<span style="color: #0000FF;">(<span style="color: #000000;">b<span style="color: #0000FF;">)</span> |
|||
⚫ | |||
<span style="color: #0000FF;">?<span style="color: #000000;">c |
|||
⚫ | |||
Or you could just serialize and deserialize: |
Or you could just serialize and deserialize: |
||
<lang Phix> |
<!--<lang Phix>--> |
||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins<span style="color: #0000FF;">\<span style="color: #000000;">serialize<span style="color: #0000FF;">.<span style="color: #000000;">e</span> |
|||
object d = deserialize(serialize(a)) |
|||
<span style="color: #004080;">object</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">deserialize<span style="color: #0000FF;">(<span style="color: #000000;">serialize<span style="color: #0000FF;">(<span style="color: #000000;">a<span style="color: #0000FF;">)<span style="color: #0000FF;">)</span> |
|||
⚫ | |||
<span style="color: #0000FF;">?<span style="color: #000000;">d |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre> |
<pre> |