deep-copy

ECMAScript5 deep copy of object and arrays

可紊 提交于 2019-12-06 07:52:47
问题 I'd hope to find an example code to do a deep copying of objects in ECMAScript5. The copying should be able to clone Nested objects Nested arrays Nested objects in arrays (clone each array item individually) Note: jQuery.extend() does not seem to handle case 3). Also, I'd hope to do this in clean ECMAScript. Quick googling did not bring up any worthy implementations. 回答1: I finally settled to jQuery.extend() as I couldn't find other good implementations http://api.jquery.com/jQuery.extend/

Efficient cloning of cached objects

元气小坏坏 提交于 2019-12-06 03:12:07
We have an application that performs comparisons on data objects to determine if one version of the object is different than another. Our application also does some extensive caching of these objects, and we've run into a bit of a performance issue when it comes to doing these comparisons. Here's the workflow: Data item 1 is the current item in memory. This item was initially retrieved from cache and deep cloned (all sub objects such as Dictionaries etc). Data item 1 is then edited, and its properties are modified. We are then comparing this object against the original version that was stored

Object deep clone implementation

夙愿已清 提交于 2019-12-06 01:39:11
I have to implement generic extention deepclone method which can be used with any reference type instance to get its deep copy. I implement it as the following static class ClassCopy { static public T DeepClone<T> (this T instance) { if (instance == null) return null; var type = instance.GetType(); T copy; var flags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; var fields = type.GetFields(flags); // If type is serializable - create instance copy using BinaryFormatter if (type.IsSerializable) { using (var stream = new MemoryStream()) {

What is the runtime complexity of Python's deepcopy()?

此生再无相见时 提交于 2019-12-05 22:44:58
I'm trying to improve the speed of an algorithm and, after looking at which operations are being called, I'm having difficulty pinning down exactly what's slowing things up. I'm wondering if Python's deepcopy() could possibly be the culprit or if I should look a little further into my own code. Looking at the code (you can too), it goes through every object in the tree of referenced objects (e.g. dict's keys and values, object member variables, ...) and does two things for them: see if it's already been copied, by looking it in id-indexed memo dict copy of the object if not The second one is O

c# Thread Safe Deep Copy

安稳与你 提交于 2019-12-05 19:10:45
问题 I have been reading alot of the other questions as well as alot of google searches and I've been unable to find a clear solution. Based on some best practices I've read, the static methods of a class should be created thread safe, and the instance members should leave thread safety to the consumers. I would like to implement a deep copy method for the class. The class itself has other reference type members. Is there any way to make the deep copy method thread safe without having to impose

Performant way to detect immutables?

谁说胖子不能爱 提交于 2019-12-05 17:28:52
I have a utility that is designed to pass objects around our system. Because it is a multithreaded environment, the utility makes a deep copy of each object it passes to prevent any thread safety issues. I'm working on transitioning our system to use immutable objects to eliminate the need for this copy. I'm wonder what is the best (fastest) way to detect that the object is immutable? My first thought was to just to pick up on the attribute that we put on all our immutable objects (MessageAttribute). As you can see from the performance profile below, it takes quite a hit (roughly 10x the time

Deepcopy on nested referenced lists created by list multiplication does not work

笑着哭i 提交于 2019-12-05 17:21:29
As much as I love Python, the reference and deepcopy stuff sometimes freaks me out. Why does deepcopy not work here: >>> import copy >>> a = 2*[2*[0]] >>> a [[0, 0], [0, 0]] >>> b = copy.deepcopy(a) >>> b[0][0] = 1 >>> b [[1, 0], [1, 0]] #should be: [[1, 0], [0, 1]] >>> I am using a numpy array as a workarround which I need later on anyway. But I really had hoped that if I used deepcopy I would not have to chase any unintended references any more. Are there any more traps where it does not work? It doesn't work because you are creating an array with two references to the same array. An

Creating a deepcopy of class instance with nested weakref to it

空扰寡人 提交于 2019-12-05 16:20:42
I have two classes: a parent class and a container class. The parent class instance has matching container class instance as a weak reference. There is a problem while deep copying the parent instance, the weakref is still linking to the original instance. Here is a minimal example: import weakref from copy import deepcopy class Container: def __init__(self, parent): self.parent = weakref.ref(parent) class Parent: def __init__(self): self.container = Container(self) if __name__ == '__main__': parent1 = Parent() assert(parent1 is parent1.container.parent()) parent2 = deepcopy(parent1) assert

Creating an easy to maintain copy constructor

十年热恋 提交于 2019-12-05 15:03:04
问题 Consider the following class: class A { char *p; int a, b, c, d; public: A(const &A); }; Note that I have to define a copy constructor in order to do a deep copy of "p". This has two issues: Most of the fields should simply be copied. Copying them one by one is ugly and error prone. More importantly, whenever a new attribute is added to the class, the copy constructor needs to be updated, which creates a maintenance nightmare. I would personally like to do something like: A(const A &a) : A(a)

How can I create a deep copy of my list collection

僤鯓⒐⒋嵵緔 提交于 2019-12-05 13:23:06
Suppose I have the following class: public class Author { public int ID {get; private set;} public string firstName {get; private set;} public string lastName {get; private set; } public Author(int id, string firstname, string lastname) { this.ID = ID; this.firstName = firstname; this.lastName = lastName; } public static Author Clone(Author clone) { Author copyAuth = new Author(clone.ID, clone.firstName, clone.lastName); return copyAuth; } } and public class Book { public string bookTitle {get; private set;} private List<Author> authors; private List<Author> copyofAuthors; public string ISBN