deep-copy

Deep copy duplicate() of Java's ByteBuffer

℡╲_俬逩灬. 提交于 2019-11-30 13:44:28
问题 java.nio.ByteBuffer#duplicate() returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. What if I want a deep copy of the byte buffer? 回答1: I think the deep copy need not involve byte[] . Try the following: public static ByteBuffer clone(ByteBuffer original) { ByteBuffer clone = ByteBuffer.allocate(original.capacity()); original.rewind();//copy from the beginning clone.put(original); original.rewind

Deep copy Map in Groovy

倾然丶 夕夏残阳落幕 提交于 2019-11-30 11:12:27
How can I deep copy a map of maps in Groovy? The map keys are Strings or Ints. The values are Strings, Primitive Objects or other maps, in a recursive way. An easy way is this: // standard deep copy implementation def deepcopy(orig) { bos = new ByteArrayOutputStream() oos = new ObjectOutputStream(bos) oos.writeObject(orig); oos.flush() bin = new ByteArrayInputStream(bos.toByteArray()) ois = new ObjectInputStream(bin) return ois.readObject() } I've just hit this issue as well, and I just found: deepCopy = evaluate(original.inspect()) Although I've been coding in Groovy for less than 12 hours, I

Is clone() in java shallow copy?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 08:58:43
Is clone() in java a shallow copy? Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy"). I read this from wikipedia . I don't understand why it is a shallow copy. clone() will create a new instance with all fields. Is this just a deep copy? confused. Need some explanation for me. The default Object.clone() is indeed a shallow copy. However, it's designed to throw a CloneNotSupportedException unless your object implements Cloneable . And when you

Creating a completely new copy of bitmap from a bitmap in C#

守給你的承諾、 提交于 2019-11-30 08:48:50
问题 I need a deep copy of bitmap from another bitmap. Now, most of the solutions say something like this, which is not a deep copy . Meaning that when I lock the original bitmap, then the copy gets locked too, as the clone is a shallow copy of the original bitmap. Now the following seems to work for me, but I am not sure that will work in all cases. public static Bitmap GetCopyOf(Bitmap originalImage) { Rectangle rect = new Rectangle(0, 0, originalImage.Width, originalImage.Height); Bitmap

Deep copy duplicate() of Java's ByteBuffer

吃可爱长大的小学妹 提交于 2019-11-30 08:04:45
java.nio.ByteBuffer#duplicate() returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. What if I want a deep copy of the byte buffer? mingfai I think the deep copy need not involve byte[] . Try the following: public static ByteBuffer clone(ByteBuffer original) { ByteBuffer clone = ByteBuffer.allocate(original.capacity()); original.rewind();//copy from the beginning clone.put(original); original.rewind(); clone.flip(); return clone; } jdmichal As this question still comes up as one of the first hits

Deep copy Map in Groovy

对着背影说爱祢 提交于 2019-11-30 07:04:18
问题 How can I deep copy a map of maps in Groovy? The map keys are Strings or Ints. The values are Strings, Primitive Objects or other maps, in a recursive way. 回答1: An easy way is this: // standard deep copy implementation def deepcopy(orig) { bos = new ByteArrayOutputStream() oos = new ObjectOutputStream(bos) oos.writeObject(orig); oos.flush() bin = new ByteArrayInputStream(bos.toByteArray()) ois = new ObjectInputStream(bin) return ois.readObject() } 回答2: I've just hit this issue as well, and I

Python dictionary deepcopy

£可爱£侵袭症+ 提交于 2019-11-30 06:15:17
I was wondering in how does exactly deepcopy work in the following context: from copy import deepcopy def copyExample: self.myDict = {} firstPosition = "First" firstPositionContent = ["first", "primero"] secondPosition = "Second" secondPositionContent = ["second"] self.myDict[firstPosition] = firstPositionContent self.myDict[secondPosition] = secondPositionContent return deepcopy(self.myDict) def addExample(self): copy = self.copyExample() copy["Second"].add("segundo") Does it return the reference to the lists I have in the dictionary? Or does it work as I expect and copy every list in a new

VB.NET, Is Object Returned by Reference from Function

我只是一个虾纸丫 提交于 2019-11-30 05:09:29
问题 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

Quicker way to deepcopy objects in golang

谁说我不能喝 提交于 2019-11-30 04:26:14
问题 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

std vector C++ — deep or shallow copy

眉间皱痕 提交于 2019-11-30 01:13:49
I wonder whether copying a vector I am copying the vector with its values (whereas this is not working with array, and deep copy need a loop or memcpy). Could you hint to an explanation? Regards Andrew You are making a deep copy any time you copy a vector. But if your vector is a vector of pointers you are getting the copy of pointers, not the values are pointed to For example: std::vector<Foo> f; std::vector<Foo> cp = f; //deep copy. All Foo copied std::vector<Foo*> f; std::vector<Foo*> cp = f; //deep copy (of pointers), or shallow copy (of objects). //All pointers to Foo are copied, but not