September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,62 +1,60 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
import "fmt"
|
||||
|
||||
// capability requested by task
|
||||
func deepcopy(dst, src interface{}) error {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
return err
|
||||
// a type that allows cyclic structures
|
||||
type node []*node
|
||||
|
||||
// recursively print the contents of a node
|
||||
func (n *node) list() {
|
||||
if n == nil {
|
||||
fmt.Println(n)
|
||||
return
|
||||
}
|
||||
enc := gob.NewEncoder(w)
|
||||
err = enc.Encode(src)
|
||||
if err != nil {
|
||||
return err
|
||||
listed := map[*node]bool{nil: true}
|
||||
var r func(*node)
|
||||
r = func(n *node) {
|
||||
listed[n] = true
|
||||
fmt.Printf("%p -> %v\n", n, *n)
|
||||
for _, m := range *n {
|
||||
if !listed[m] {
|
||||
r(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
dec := gob.NewDecoder(r)
|
||||
return dec.Decode(dst)
|
||||
r(n)
|
||||
}
|
||||
|
||||
// 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"
|
||||
// construct a deep copy of a node
|
||||
func (n *node) ccopy() *node {
|
||||
if n == nil {
|
||||
return n
|
||||
}
|
||||
s := "(" + l.Value
|
||||
for l = l.Next; l != nil; l = l.Next {
|
||||
s += " " + l.Value
|
||||
cc := map[*node]*node{nil: nil}
|
||||
var r func(*node) *node
|
||||
r = func(n *node) *node {
|
||||
c := make(node, len(*n))
|
||||
cc[n] = &c
|
||||
for i, m := range *n {
|
||||
d, ok := cc[m]
|
||||
if !ok {
|
||||
d = r(m)
|
||||
}
|
||||
c[i] = d
|
||||
}
|
||||
return &c
|
||||
}
|
||||
return s + ")"
|
||||
return r(n)
|
||||
}
|
||||
|
||||
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)
|
||||
a := node{nil}
|
||||
c := &node{&node{&a}}
|
||||
a[0] = c
|
||||
c.list()
|
||||
cc := c.ccopy()
|
||||
fmt.Println("copy:")
|
||||
cc.list()
|
||||
fmt.Println("original:")
|
||||
c.list()
|
||||
}
|
||||
|
|
|
|||
62
Task/Deepcopy/Go/deepcopy-3.go
Normal file
62
Task/Deepcopy/Go/deepcopy-3.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)
|
||||
}
|
||||
3
Task/Deepcopy/Julia/deepcopy.julia
Normal file
3
Task/Deepcopy/Julia/deepcopy.julia
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# v0.6.0
|
||||
|
||||
cp = deepcopy(obj)
|
||||
6
Task/Deepcopy/Phix/deepcopy-1.phix
Normal file
6
Task/Deepcopy/Phix/deepcopy-1.phix
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
object a, b
|
||||
a = {1,{2,3},"four",{5.6,7,{8.9}}}
|
||||
b = a
|
||||
b[3] = 4
|
||||
?a
|
||||
?b
|
||||
15
Task/Deepcopy/Phix/deepcopy-2.phix
Normal file
15
Task/Deepcopy/Phix/deepcopy-2.phix
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
function deep_copy(object o)
|
||||
object res
|
||||
if atom(o) then
|
||||
res = o
|
||||
else
|
||||
res = repeat(' ',length(o))
|
||||
for i=1 to length(o) do
|
||||
res[i] = deep_copy(o[i])
|
||||
end for
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
object c = deep_copy(b)
|
||||
?c
|
||||
3
Task/Deepcopy/Phix/deepcopy-3.phix
Normal file
3
Task/Deepcopy/Phix/deepcopy-3.phix
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
include builtins\serialize.e
|
||||
object d = deserialize(serialize(a))
|
||||
?d
|
||||
26
Task/Deepcopy/Rust/deepcopy.rust
Normal file
26
Task/Deepcopy/Rust/deepcopy.rust
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// The compiler can automatically implement Clone on structs (assuming all members have implemented Clone).
|
||||
#[derive(Clone)]
|
||||
struct Tree<T> {
|
||||
left: Leaf<T>,
|
||||
data: T,
|
||||
right: Leaf<T>,
|
||||
}
|
||||
|
||||
type Leaf<T> = Option<Box<Tree<T>>>;
|
||||
|
||||
impl<T> Tree<T> {
|
||||
fn root(d: T) -> Self {
|
||||
Tree { left: None, data: d, right: None }
|
||||
}
|
||||
fn leaf(d: T) -> Leaf<T> {
|
||||
Some(Box::new(Tree::root(d)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut tree = Tree::root(vec![4,5,6]);
|
||||
tree.right = Tree::leaf(vec![1,2,3]);
|
||||
tree.left = Tree::leaf(vec![7,8,9]);
|
||||
let newtree = tree.clone();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue