deep-copy

What's the most efficient way to deep copy an object in Ruby?

ぃ、小莉子 提交于 2019-12-02 19:37:27
I know that serializing an object is (to my knowledge) the only way to effectively deep-copy an object (as long as it isn't stateful like IO and whatnot), but is one way particularly more efficient than another? For example, since I'm using Rails, I could always use ActiveSupport::JSON , to_xml - and from what I can tell marshalling the object is one of the most accepted ways to do this. I'd expect that marshalling is probably the most efficient of these since it's a Ruby internal, but am I missing anything? Edit : note that its implementation is something I already have covered - I don't want

Deep Copy of OpenCV cv::Mat

白昼怎懂夜的黑 提交于 2019-12-02 18:04:08
The behaviour of copying cv::Mat is confusing me. I understand from the documentation that Mat::copyTo() is deep copy while the assignment operator is not. My questions: what should I do to return a cv::Mat from a function, such as: cv::Mat func() ? According to the documentation, if I return a cv::Mat it'll have no use, because after the function returns the local copy of the cv::Mat in that function will be destroyed and therefore the one accepting the returned value outside the function should be pointing to some random address. The weird thing is that (most of times) it works correctly.

Bitmap deep copy changing PixelFormat

独自空忆成欢 提交于 2019-12-02 18:01:35
问题 Bitmap img = new Bitmap("C:\\temp\\images\\file.jpg"); img.PixelFormat is Format24bppRgb when I am doing deep copy Bitmap img2 = new Bitmap(img); img.PixelFormat is changed to Format32bppArgb why does it change pixel format? and how to make deep copy for the object if it doesn't make deep copy? 回答1: You can clone the bitmap like this, which will create a deep copy: Bitmap img = new Bitmap("C:\\temp\\images\\file.jpg"); // Clone the bitmap. Rectangle cloneRect = new Rectangle(0, 0, img.Width,

copy.deepcopy vs pickle

[亡魂溺海] 提交于 2019-12-02 17:59:44
I have a tree structure of widgets e.g. collection contains models and model contains widgets. I want to copy whole collection, copy.deepcopy is faster in comparison to 'pickle and de-pickle'ing the object but cPickle as being written in C is much faster, so Why shouldn't I(we) always be using cPickle instead of deepcopy? Is there any other copy alternative? because pickle is slower then deepcopy but cPickle is faster, so may be a C implementation of deepcopy will be the winner Sample test code: import copy import pickle import cPickle class A(object): pass d = {} for i in range(1000): d[i] =

deepcopy and python - tips to avoid using it?

一世执手 提交于 2019-12-02 16:50:29
I have a very simple python routine that involves cycling through a list of roughly 20,000 latitude,longitude coordinates and calculating the distance of each point to a reference point. def compute_nearest_points( lat, lon, nPoints=5 ): """Find the nearest N points, given the input coordinates.""" points = session.query(PointIndex).all() oldNearest = [] newNearest = [] for n in xrange(nPoints): oldNearest.append(PointDistance(None,None,None,99999.0,99999.0)) newNearest.append(obj2) #This is almost certainly an inappropriate use of deepcopy # but how SHOULD I be doing this?!?! for point in

In JS, why does the slice() documentation say it is a shallow copy when it looks like a deep copy?

社会主义新天地 提交于 2019-12-02 11:39:17
问题 According to the docs for Array.prototype.slice() in JavaScript, the slice() method returns a shallow copy of a portion of an array into a new array. It is my understanding that a shallow copy will only copy top level elements in an array and will not copy nested elements. However, when I run a test in my browser console, it sure looks like the slice() method is actually copying the nested elements (deep copying). Where am I misunderstanding the concept of a deep copy? Please help me clarify

How make deep copy (clone) in Entity framework 4?

断了今生、忘了曾经 提交于 2019-12-02 10:00:43
How make deep copy (clone) in Entity framework 4? I need get copy of the EntityObject with copies of all related objects. This is how I do generic deep copy: public static T DeepClone<T>(this T obj) { using (var ms = new MemoryStream()) { var bf = new BinaryFormatter(); bf.Serialize(ms, obj); ms.Position = 0; return (T)bf.Deserialize(ms); } } I am sure this has been asked before. Either way you need to be careful of this. There is a danger that your cloning process uses reflection, thus invoking the deferred data loading supported by EF when the properties are interrogated for reflection. If

Bitmap deep copy changing PixelFormat

烈酒焚心 提交于 2019-12-02 09:44:32
Bitmap img = new Bitmap("C:\\temp\\images\\file.jpg"); img.PixelFormat is Format24bppRgb when I am doing deep copy Bitmap img2 = new Bitmap(img); img.PixelFormat is changed to Format32bppArgb why does it change pixel format? and how to make deep copy for the object if it doesn't make deep copy? You can clone the bitmap like this, which will create a deep copy: Bitmap img = new Bitmap("C:\\temp\\images\\file.jpg"); // Clone the bitmap. Rectangle cloneRect = new Rectangle(0, 0, img.Width, img.Height); System.Drawing.Imaging.PixelFormat format = img.PixelFormat; Bitmap img2 = img.Clone(cloneRect,

Any alternative to a (very slow) deepcopy in a DFS?

有些话、适合烂在心里 提交于 2019-12-02 04:25:45
问题 I have a nice graph (a list) containing 81 vertices (each vertex is an instance of a Vertex class). Each vertex has 20 neighbors. Each vertex has a number of possible values (ranging from 1 to 9) which, given some initial constraints to the problem will be on average 4 or 5. I implemented a simple DFS on this graph, that takes the node with less possible values, foreach value builds another "deepcopied" graph having only one of the possible value, and finally passes the "deepcopied" graph

Implement Kahn's topological sorting algorithm using Python

纵饮孤独 提交于 2019-12-02 03:51:48
Kahn proposed an algorithm in 62 to topologically sort any DAG (directed acyclic graph), pseudo code copied from Wikipedia: L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S add n to tail of L for each node m with an edge e from n to m do remove edge e from the graph # This is a DESTRUCTIVE step! if m has no other incoming edges then insert m into S if graph has edges then return error (graph has at least one cycle) else return L (a topologically sorted order) I need to implement it using IPython3,