deep-copy

deep copying a graph structure

心不动则不痛 提交于 2019-11-28 20:53:52
I have a graph class with Node's, where each Node can connect to others: public class Node { List<Node> connections; } I would like to make a deep copy of the entire graph. As a first attempt, I tried making a copy constructor like: public Node(Node other) { connections = new ArrayList<Node>(); for (Node n : other.connections) { connections.add(new Node(n)); } } So deep copying a graph would just be: public Graph deepCopy () { Graph g = new Graph(); g.nodes = new ArrayList<Node>(); for (Node n : nodes) { g.nodes.add(new Node(n)); } } But that doesn't work as that destroys the connection

deep extend (like jQuery's) for nodeJS

狂风中的少年 提交于 2019-11-28 18:34:25
I am struggling with deep copies of objects in nodeJS. my own extend is crap. underscore's extend is flat. there are rather simple extend variants here on stackexchange, but none are even close to jQuery.extend(true, {}, obj, obj, obj) .. (most are actually terrible and screw up the benefits of asnyc code.) hence, my question: is there a good deep copy for NodeJS? Has anybody ported jQuery's ? You want jQuery's, so just use it: function extend() { var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, toString = Object

javascript deep copy using JSON

ぃ、小莉子 提交于 2019-11-28 17:17:47
I have problem with javascript object(array) deep copy. I read many good way to deal with it. And I also know that jQuery has $.extend API to this problem. But my question is: Can I just using JSON stringify and parse method to solve this problem? Here's my code: function deepCopy(oldValue) { var newValue strValue = JSON.stringify(oldValue) return newValue = JSON.parse(strValue) } var a = { b: 'b', c: [1,2,4], d: null } copy = deepCopy(a) console.log(a === copy) // false console.log(a.c === copy.c) // false PS: I've known that if no all objects are serializable, but the only situation I know

Move Constructor vs Copy Elision. Which one gets called?

我的未来我决定 提交于 2019-11-28 10:25:20
I have two pieces of code here to show you. They are two classes and each one provides a Move Constructor and a function which returns a temporary. In the first case, the function returning a temporary calls the Move Constructor In the second case, the function returning a temporary just tells the compiler to perform a copy elision I'm confused: in both cases I define a Move Constructor and a random member function returning a temporary. But the behavior changes, and my question is why . Note that in the following examples, the operator<< was overloaded in order to print a list (in the first

Pulling data from a CMSampleBuffer in order to create a deep copy

ⅰ亾dé卋堺 提交于 2019-11-28 09:08:45
I am trying to create a copy of a CMSampleBuffer as returned by captureOutput in a AVCaptureVideoDataOutputSampleBufferDelegate. Since the CMSampleBuffers come from a preallocated pool of (15) buffers, if I attach a reference to them they cannot be recollected. This causes all remaining frames to be dropped. To maintain optimal performance, some sample buffers directly reference pools of memory that may need to be reused by the device system and other capture inputs. This is frequently the case for uncompressed device native capture where memory blocks are copied as little as possible. If

deep copy for array of objects in swift

China☆狼群 提交于 2019-11-28 07:50:32
I have this class named Meal class Meal { var name : String = "" var cnt : Int = 0 var price : String = "" var img : String = "" var id : String = "" init(name:String , cnt : Int, price : String, img : String, id : String) { self.name = name self.cnt = cnt self.price = price self.img = img self.id = id } } and I have an array of Meal : var ordered = [Meal]() I want to duplicate that array and then do some changes to the Meal instances in one of them without changing the Meal instances in the second one, how would I make a deep copy of it? This search result didn't help me How do I make a exact

How to do “Deep Copy” in Swift?

做~自己de王妃 提交于 2019-11-28 07:29:12
In Objective-C, one can deep-copy by following: Foo *foo = [[Foo alloc] init]; Foo *foo2 = foo.copy; How to do this deep-copy in Swift? Cameron Lowell Palmer Deep Copy Your example is not a deep copy as discussed on StackOverflow. Getting a true deep copy of an object would often require NSKeyedArchiver Swift and copying The NSCopying protocol is the Objective-C way of providing object copies because everything was a pointer and you needed a way of managing the generation of copies of arbitrary objects. For an arbitrary object copy in Swift you might provide a convenience initializer where you

Python: deepcopy does not work on user-defined classes?

放肆的年华 提交于 2019-11-28 07:25:24
问题 In the following example I would expect deepcopy to create a copy of field and not just copy the reference. What happens here and is there an easy way around it? from copy import deepcopy class Test: field = [(1,2)] t1 = Test() t2 = deepcopy(t1) t2.field[0]=(5,10) print t1.field # [(1,2)] expected but [(5,10)] obtained print t2.field # [(5,10)] expected Output: [(5, 10)] [(5, 10)] 回答1: Deep copying (by default) only applies to instance level attributes - not class level - It doesn't make much

Copying an array of objects into another array in javascript (Deep Copy)

天大地大妈咪最大 提交于 2019-11-28 06:53:27
Copying an array of objects into another array in javascript using slice(0) and concat() doesnt work. I have tried the following to test if i get the expected behaviour of deep copy using this. But the original array is also getting modified after i make changes in the copied array. var tags = []; for(var i=0; i<3; i++) { tags.push({ sortOrder: i, type: 'miss' }) } for(var tag in tags) { if(tags[tag].sortOrder == 1) { tags[tag].type = 'done' } } console.dir(tags) var copy = tags.slice(0) console.dir(copy) copy[0].type = 'test' console.dir(tags) var another = tags.concat() another[0].type =

Copy an arbitrary n-dimensional array in JavaScript?

一世执手 提交于 2019-11-28 06:27:03
问题 I am basically looking for a general function copy(array) that will return identical n-dimensional array without any references to the former one. 回答1: This works for arrays, it won't work for nested objects (that aren't Arrays): function copy(arr){ var new_arr = arr.slice(0); for(var i = new_arr.length; i--;) if(new_arr[i] instanceof Array) new_arr[i] = copy(new_arr[i]); return new_arr; } Use it like this: var arr = [ [[1,2,3],[75]], 100, [[[1]]], [], [1,[2,[3],4],5], 6, 7, 8 ]; var new_arr