CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
13
Task/Deepcopy/0DESCRIPTION
Normal file
13
Task/Deepcopy/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Demonstrate how to copy data structures containing complex hetrogeneous and cyclic semantics. This is often referred to as [[wp:Deep_copy#Deep_copy|deep copying]], and is normally required where structures are mutable and to ensure that independent copies can be manipulated without side-effects.
|
||||
|
||||
If this facility is not built into the language, it is permissible to use functions from a common library, or a coded procedure.
|
||||
|
||||
The task should show:
|
||||
|
||||
* Relevant semantics of structures, such as their [[wp:Homogeneity and heterogeneity|homogeneous or heterogeneous]] properties, or containment of (self- or mutual-reference) cycles.
|
||||
|
||||
* Any limitations of the method.
|
||||
|
||||
* That the structure and its copy are different.
|
||||
|
||||
* Suitable links to external documentation for common libraries.
|
||||
28
Task/Deepcopy/C-sharp/deepcopy.cs
Normal file
28
Task/Deepcopy/C-sharp/deepcopy.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
|
||||
namespace prog
|
||||
{
|
||||
class MainClass
|
||||
{
|
||||
class MyClass : ICloneable
|
||||
{
|
||||
public MyClass() { f = new int[3]{2,3,5}; c = '1'; }
|
||||
|
||||
public object Clone()
|
||||
{
|
||||
MyClass cpy = (MyClass) this.MemberwiseClone();
|
||||
cpy.f = (int[]) this.f.Clone();
|
||||
return cpy;
|
||||
}
|
||||
|
||||
public char c;
|
||||
public int[] f;
|
||||
}
|
||||
|
||||
public static void Main( string[] args )
|
||||
{
|
||||
MyClass c1 = new MyClass();
|
||||
MyClass c2 = (MyClass) c1.Clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Task/Deepcopy/Common-Lisp/deepcopy.lisp
Normal file
7
Task/Deepcopy/Common-Lisp/deepcopy.lisp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
$ clisp -q
|
||||
[1]> (setf *print-circle* t)
|
||||
T
|
||||
[2]> (let ((a (cons 1 nil))) (setf (cdr a) a)) ;; create circular list
|
||||
#1=(1 . #1#)
|
||||
[3]> (read-from-string "#1=(1 . #1#)") ;; read it from a string
|
||||
#1=(1 . #1#) ;; a similar circular list is returned
|
||||
4
Task/Deepcopy/E/deepcopy-1.e
Normal file
4
Task/Deepcopy/E/deepcopy-1.e
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def deSubgraphKit := <elib:serial.deSubgraphKit>
|
||||
def deepcopy(x) {
|
||||
return deSubgraphKit.recognize(x, deSubgraphKit.makeBuilder())
|
||||
}
|
||||
22
Task/Deepcopy/E/deepcopy-2.e
Normal file
22
Task/Deepcopy/E/deepcopy-2.e
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
? def x := ["a" => 1, "b" => [x].diverge()]
|
||||
# value: ["a" => 1, "b" => [<***CYCLE***>].diverge()]
|
||||
|
||||
? def y := deepcopy(x)
|
||||
# value: ["a" => 1, "b" => [<***CYCLE***>].diverge()]
|
||||
|
||||
? y["b"].push(2)
|
||||
|
||||
? y
|
||||
# value: ["a" => 1, "b" => [<***CYCLE***>, 2].diverge()]
|
||||
|
||||
? x
|
||||
# value: ["a" => 1, "b" => [<***CYCLE***>].diverge()]
|
||||
|
||||
? y["b"][0] == y
|
||||
# value: true
|
||||
|
||||
? y["b"][0] == x
|
||||
# value: false
|
||||
|
||||
? x["b"][0] == x
|
||||
# value: true
|
||||
37
Task/Deepcopy/Go/deepcopy-1.go
Normal file
37
Task/Deepcopy/Go/deepcopy-1.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// a complex data structure
|
||||
type cds struct {
|
||||
i int // no special handling needed for deep copy
|
||||
s string // no special handling
|
||||
b []byte // copied easily with append function
|
||||
m map[int]bool // deep copy requires looping
|
||||
}
|
||||
|
||||
// a method
|
||||
func (c cds) deepcopy() *cds {
|
||||
// copy what you can in one line
|
||||
r := &cds{c.i, c.s, append([]byte{}, c.b...), make(map[int]bool)}
|
||||
// populate map with a loop
|
||||
for k, v := range c.m {
|
||||
r.m[k] = v
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// demo
|
||||
func main() {
|
||||
// create and populate a structure
|
||||
c1 := &cds{1, "one", []byte("unit"), map[int]bool{1: true}}
|
||||
fmt.Println(c1) // show it
|
||||
c2 := c1.deepcopy() // copy it
|
||||
fmt.Println(c2) // show copy
|
||||
c1.i = 0 // change original
|
||||
c1.s = "nil"
|
||||
copy(c1.b, "zero")
|
||||
c1.m[1] = false
|
||||
fmt.Println(c1) // show changes
|
||||
fmt.Println(c2) // show copy unaffected
|
||||
}
|
||||
62
Task/Deepcopy/Go/deepcopy-2.go
Normal file
62
Task/Deepcopy/Go/deepcopy-2.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// capability requested by task
|
||||
func deepcopy(dst, src interface{}) error {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
enc := gob.NewEncoder(w)
|
||||
err = enc.Encode(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dec := gob.NewDecoder(r)
|
||||
return dec.Decode(dst)
|
||||
}
|
||||
|
||||
// define linked list type, an example of a recursive type
|
||||
type link struct {
|
||||
Value string
|
||||
Next *link
|
||||
}
|
||||
|
||||
// method satisfies stringer interface for fmt.Println
|
||||
func (l *link) String() string {
|
||||
if l == nil {
|
||||
return "nil"
|
||||
}
|
||||
s := "(" + l.Value
|
||||
for l = l.Next; l != nil; l = l.Next {
|
||||
s += " " + l.Value
|
||||
}
|
||||
return s + ")"
|
||||
}
|
||||
|
||||
func main() {
|
||||
// create a linked list with two elements
|
||||
l1 := &link{"a", &link{Value: "b"}}
|
||||
// print original
|
||||
fmt.Println(l1)
|
||||
// declare a variable to hold deep copy
|
||||
var l2 *link
|
||||
// copy
|
||||
if err := deepcopy(&l2, l1); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
// print copy
|
||||
fmt.Println(l2)
|
||||
// now change contents of original list
|
||||
l1.Value, l1.Next.Value = "c", "d"
|
||||
// show that it is changed
|
||||
fmt.Println(l1)
|
||||
// show that copy is unaffected
|
||||
fmt.Println(l2)
|
||||
}
|
||||
8
Task/Deepcopy/JavaScript/deepcopy-1.js
Normal file
8
Task/Deepcopy/JavaScript/deepcopy-1.js
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
var deepcopy = function(o){
|
||||
return JSON.parse(JSON.stringify(src));
|
||||
};
|
||||
|
||||
var src = {foo:0,bar:[0,1]};
|
||||
print(JSON.stringify(src));
|
||||
var dst = deepcopy(src);
|
||||
print(JSON.stringify(src));
|
||||
8
Task/Deepcopy/JavaScript/deepcopy-2.js
Normal file
8
Task/Deepcopy/JavaScript/deepcopy-2.js
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
var deepcopy = function(o){
|
||||
return eval(uneval(o));
|
||||
};
|
||||
var src = {foo:0,bar:[0,1]};
|
||||
src['baz'] = src;
|
||||
print(uneval(src));
|
||||
var dst = deepcopy(src);
|
||||
print(uneval(src));
|
||||
21
Task/Deepcopy/PHP/deepcopy-1.php
Normal file
21
Task/Deepcopy/PHP/deepcopy-1.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
class Foo
|
||||
{
|
||||
public function __clone()
|
||||
{
|
||||
$this->child = clone $this->child;
|
||||
}
|
||||
}
|
||||
|
||||
$object = new Foo;
|
||||
$object->some_value = 1;
|
||||
$object->child = new stdClass;
|
||||
$object->child->some_value = 1;
|
||||
|
||||
$deepcopy = clone $object;
|
||||
$deepcopy->some_value++;
|
||||
$deepcopy->child->some_value++;
|
||||
|
||||
echo "Object contains {$object->some_value}, child contains {$object->child->some_value}\n",
|
||||
"Clone of object contains {$deepcopy->some_value}, child contains {$deepcopy->child->some_value}\n";
|
||||
?>
|
||||
14
Task/Deepcopy/PHP/deepcopy-2.php
Normal file
14
Task/Deepcopy/PHP/deepcopy-2.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
// stdClass is a default PHP object
|
||||
$object = new stdClass;
|
||||
$object->some_value = 1;
|
||||
$object->child = new stdClass;
|
||||
$object->child->some_value = 1;
|
||||
|
||||
$deepcopy = unserialize(serialize($object));
|
||||
$deepcopy->some_value++;
|
||||
$deepcopy->child->some_value++;
|
||||
|
||||
echo "Object contains {$object->some_value}, child contains {$object->child->some_value}\n",
|
||||
"Clone of object contains {$deepcopy->some_value}, child contains {$deepcopy->child->some_value}\n";
|
||||
11
Task/Deepcopy/Perl/deepcopy.pl
Normal file
11
Task/Deepcopy/Perl/deepcopy.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use Storable;
|
||||
use Data::Dumper;
|
||||
|
||||
my $src = { foo => 0, bar => [0, 1] };
|
||||
$src->{baz} = $src;
|
||||
my $dst = Storable::dclone($src);
|
||||
print Dumper($src);
|
||||
print Dumper($dst);
|
||||
1
Task/Deepcopy/PicoLisp/deepcopy-1.l
Normal file
1
Task/Deepcopy/PicoLisp/deepcopy-1.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
(mapcar copy List)
|
||||
4
Task/Deepcopy/PicoLisp/deepcopy-2.l
Normal file
4
Task/Deepcopy/PicoLisp/deepcopy-2.l
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(de deepCopy (X)
|
||||
(if (atom X)
|
||||
X
|
||||
(cons (deepCopy (car X)) (deepCopy (cdr X))) ) )
|
||||
25
Task/Deepcopy/PicoLisp/deepcopy-3.l
Normal file
25
Task/Deepcopy/PicoLisp/deepcopy-3.l
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
: (setq A '((a . b) (c d e) f g . e))
|
||||
-> ((a . b) (c d e) f g . e)
|
||||
|
||||
: (setq B (deepCopy A))
|
||||
-> ((a . b) (c d e) f g . e)
|
||||
|
||||
: A
|
||||
-> ((a . b) (c d e) f g . e)
|
||||
|
||||
: B
|
||||
-> ((a . b) (c d e) f g . e)
|
||||
|
||||
: (= A B)
|
||||
-> T # A and its copy B are structure-equal
|
||||
: (== A B)
|
||||
-> NIL # but they are not identical (pointer-equal)
|
||||
|
||||
: (cadr A)
|
||||
-> (c d e)
|
||||
|
||||
: (cadr B)
|
||||
-> (c d e)
|
||||
|
||||
: (== (cadr A) (cadr B))
|
||||
-> NIL # The same holds for sub-structures
|
||||
11
Task/Deepcopy/PicoLisp/deepcopy-4.l
Normal file
11
Task/Deepcopy/PicoLisp/deepcopy-4.l
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(de deepCopy (X)
|
||||
(let Mark NIL
|
||||
(recur (X)
|
||||
(cond
|
||||
((atom X) X)
|
||||
((asoq X Mark) (cdr @))
|
||||
(T
|
||||
(prog1 (cons)
|
||||
(push 'Mark (cons X @))
|
||||
(set @ (recurse (car X)))
|
||||
(con @ (recurse (cdr X))) ) ) ) ) ) )
|
||||
12
Task/Deepcopy/PicoLisp/deepcopy-5.l
Normal file
12
Task/Deepcopy/PicoLisp/deepcopy-5.l
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
: (setq A '(a b .) B (deepCopy A))
|
||||
-> (a b .)
|
||||
: A
|
||||
-> (a b .)
|
||||
: B
|
||||
-> (a b .)
|
||||
|
||||
: (= A B)
|
||||
-> T # A and its copy B are structure-equal
|
||||
|
||||
: (== A B)
|
||||
-> NIL # but they are not identical (pointer-equal)
|
||||
2
Task/Deepcopy/Python/deepcopy.py
Normal file
2
Task/Deepcopy/Python/deepcopy.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import copy
|
||||
deepcopy_of_obj = copy.deepcopy(obj)
|
||||
22
Task/Deepcopy/Ruby/deepcopy.rb
Normal file
22
Task/Deepcopy/Ruby/deepcopy.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# _orig_ is a Hash that contains an Array.
|
||||
orig = { :num => 1, :ary => [2, 3] }
|
||||
orig[:cycle] = orig # _orig_ also contains itself.
|
||||
|
||||
# _copy_ becomes a deep copy of _orig_.
|
||||
copy = Marshal.load(Marshal.dump orig)
|
||||
|
||||
# These changes to _orig_ never affect _copy_,
|
||||
# because _orig_ and _copy_ are disjoint structures.
|
||||
orig[:ary] << 4
|
||||
orig[:rng] = (5..6)
|
||||
|
||||
# Because of deep copy, orig[:ary] and copy[:ary]
|
||||
# refer to different Arrays.
|
||||
p orig # => {:num=>1, :ary=>[2, 3, 4], :cycle=>{...}, :rng=>5..6}
|
||||
p copy # => {:num=>1, :ary=>[2, 3], :cycle=>{...}}
|
||||
|
||||
# The original contains itself, and the copy contains itself,
|
||||
# but the original and the copy are not the same object.
|
||||
p [(orig.equal? orig[:cycle]),
|
||||
(copy.equal? copy[:cycle]),
|
||||
(not orig.equal? copy)] # => [true, true, true]
|
||||
1
Task/Deepcopy/Tcl/deepcopy-1.tcl
Normal file
1
Task/Deepcopy/Tcl/deepcopy-1.tcl
Normal file
|
|
@ -0,0 +1 @@
|
|||
set deepCopy [string range ${valueToCopy}x 0 end-1]
|
||||
1
Task/Deepcopy/Tcl/deepcopy-2.tcl
Normal file
1
Task/Deepcopy/Tcl/deepcopy-2.tcl
Normal file
|
|
@ -0,0 +1 @@
|
|||
set copiedObject [oo::copy $originalObject]
|
||||
Loading…
Add table
Add a link
Reference in a new issue