deep-copy

How to create an operator for deep copy/cloning of objects in Ruby?

给你一囗甜甜゛ 提交于 2019-12-01 01:15:26
I would like to achieve the following by introducing a new operator (e.g. := ) a := b = {} b[1] = 2 p a # => {} p b # => {1=>2} As far as I understand, I need to modify the Object class, but I don't know what to do in order to get what I want. require 'superators' class Object superator ":=" operand # update, must be: superator ":=" do |operand| # self = Marshal.load(Marshal.dump(operand)) # ??? end end Could you help me with this? Update Ok, superators will probably not help me here, but I still want such operator. How can I (or you) create an extension for Ruby, which I could load as a

VB.NET, Is Object Returned by Reference from Function

随声附和 提交于 2019-11-30 20:46:16
This should be a fairly common question, but I haven't found a straightforward answer anywhere. If I instantiate an object within a function in VB.NET and return it, does it return it by reference or by value. IE - should I be worried about performance if I write something like this: Public Function ret_obj_func() As big_object Dim ret_obj As New big_obj(<lots of stuff>) Return ret_obj End Function If I call this function from somewhere else, will it instantiate the object in the ret_obj and then create a deep copy to pass back a copy to the caller, Or will it just pass back a reference? It

How to create an operator for deep copy/cloning of objects in Ruby?

廉价感情. 提交于 2019-11-30 20:37:40
问题 I would like to achieve the following by introducing a new operator (e.g. := ) a := b = {} b[1] = 2 p a # => {} p b # => {1=>2} As far as I understand, I need to modify the Object class, but I don't know what to do in order to get what I want. require 'superators' class Object superator ":=" operand # update, must be: superator ":=" do |operand| # self = Marshal.load(Marshal.dump(operand)) # ??? end end Could you help me with this? Update Ok, superators will probably not help me here, but I

Quicker way to deepcopy objects in golang

僤鯓⒐⒋嵵緔 提交于 2019-11-30 20:17:49
I am using go 1.9 . And I want to deepcopy value of object into another object. I try to do it with encoding/gob and encoding/json. But it takes more time for gob encoding than json encoding. I see some other questions like this and they suggest that gob encoding should be quicker. But I see exact opposite behaviour. Can someone tell me if I am doing something wrong? Or any better and quicker way to deepcopy than these two? My object's struct is complex and nested. The test code: package main import ( "bytes" "encoding/gob" "encoding/json" "log" "time" "strconv" ) // Test ... type Test struct

“CopyConstructible” requirement for C++ stl container element

本秂侑毒 提交于 2019-11-30 19:05:12
Regarding to the requirement for C++ stl container element, the standard says: the element type should be CopyConstructible, and there is a table for CopyConstructible requirements. Also by various books (Josuttis, etc.), the generated copy should be "equivalent to" the source. I think I need some clarity here. What is exactly "equivalent to"? Also I am a bit confused with the relation between the "CopyConstructible" and the "deep/shallow copy". In general, a copy constructor is either shallow copy or deep copy. So which one applies to the "CopyConstructible", and which does not? Thanks for

Kotlin data class copy method not deep copying all members

旧时模样 提交于 2019-11-30 17:57:15
Could someone explain how exactly the copy method for Kotlin data classes work? It seems like for some members, a (deep) copy is not actually created and the references are still to the original. fun test() { val bar = Bar(0) val foo = Foo(5, bar, mutableListOf(1, 2, 3)) println("foo : $foo") val barCopy = bar.copy() val fooCopy = foo.copy() foo.a = 10 bar.x = 2 foo.list.add(4) println("foo : $foo") println("fooCopy: $fooCopy") println("barCopy: $barCopy") } data class Foo(var a: Int, val bar: Bar, val list: MutableList<Int> = mutableListOf()) data class Bar(var x: Int = 0) Output: foo : Foo(a

How to deep copy a class without marking it as Serializable

梦想与她 提交于 2019-11-30 17:18:56
Given the following class: class A { public List<B> ListB; // etc... } where B is another class that may inherit/contain some other classes. Given this scenario: A is a large class and contains many reference types I cannot mark B as [Serializable] as I don't have access to source code of B The following methods to perform deep copying do not work: I cannot use ICloneable or MemberwiseClone as class A contains many reference types I cannot write a copy constructor for A , as the class is large and continuously being added to, and contains classes (like B ) that cannot be deep copied I cannot

How to deep copy a class without marking it as Serializable

可紊 提交于 2019-11-30 16:34:49
问题 Given the following class: class A { public List<B> ListB; // etc... } where B is another class that may inherit/contain some other classes. Given this scenario: A is a large class and contains many reference types I cannot mark B as [Serializable] as I don't have access to source code of B The following methods to perform deep copying do not work: I cannot use ICloneable or MemberwiseClone as class A contains many reference types I cannot write a copy constructor for A , as the class is

What is the difference between being shallowly and deeply equal? How is this applied to caching?

微笑、不失礼 提交于 2019-11-30 15:32:37
问题 Found the following in my notes, but I am unable to make sense of it: Primitive type wrapper classes implement caching for a limited number of values. This guarantees that a limited number of deeply equal wrapper objects are also shallowly equal: If o1.equals( o2 ) then o1 == o2 . For example, new Integer( 0 ) == new Integer( 0 ) . In general this does not always work. For example, new Integer( 666 ) == new Integer( 666 ) may not hold. The reason for caching is that it saves memory. In

What is the difference between being shallowly and deeply equal? How is this applied to caching?

我与影子孤独终老i 提交于 2019-11-30 14:22:37
Found the following in my notes, but I am unable to make sense of it: Primitive type wrapper classes implement caching for a limited number of values. This guarantees that a limited number of deeply equal wrapper objects are also shallowly equal: If o1.equals( o2 ) then o1 == o2 . For example, new Integer( 0 ) == new Integer( 0 ) . In general this does not always work. For example, new Integer( 666 ) == new Integer( 666 ) may not hold. The reason for caching is that it saves memory. In general caching works for “small” primitive values. I don't understand what is meant by this, or what the