Deep Clone A Set Of DOM Elements - jQuery Deepest Copy
A jQuery deep copy plugin that enables deep cloning of a set of DOM elements while preserving the current state.
Demo
Download
seen from Germany
seen from United States
seen from United States
seen from United States
seen from Mexico
seen from United States

seen from United States

seen from United States
seen from United States
seen from United States
seen from United States
seen from United States
seen from United States
seen from United States

seen from Brazil

seen from T1

seen from Maldives
seen from T1
seen from Maldives
seen from Mexico
Deep Clone A Set Of DOM Elements - jQuery Deepest Copy
A jQuery deep copy plugin that enables deep cloning of a set of DOM elements while preserving the current state.
Demo
Download
Understand Shallow And Deep Copy In JavaScript
Understand Shallow And Deep Copy In JavaScript
Understanding shallow and deep copy in JavaScript is very important. In this tutorial, we will learn about what is a shallow and deep copy in JavaScript. First of all, what is a copy? In day to day programming, we store values in variables. Making a copy means that you initiate a new variable with the same value(s).
When you change the copy, you expect the original thing to stay the same,…
View On WordPress
Python list copy - How to perform shallow copy and deep copy
Python list copy involves copying or cloning lists. Lists can store literally any type of data. Whether it’s a primitive data type like int or float or an object or another list, all can fit into a list. Situations arise when the content of one list needs to be copied into another. As easy you might have found python programming to be, if you don’t understand how things work, you’ll be in…
View On WordPress
The conversion and copy CvMat, Mat and between IplImage
The conversion and copy CvMat, Mat and between IplImage
The conversion and copy CvMat, Mat and between IplImage
In OpenCV Mat, CvMat and IplImage types can represent and display the image. IplImage derived from the CvMat, and CvMat that is derived from the CvArr CvArr -> CvMat -> IplImage, Mat type is a C ++ version of the matrix type (CvArr used as a function of the parameters, either passed or are CvMat IplImage, inside it is by CvMat deal with).
Ma…
View On WordPress
Deep Copy in JavaScript
When performing complex operations, sometimes one object needs to be created from another, but not modify the original. In JavaScript, every object is soft copied when assigning to another variable. Meaning they point to the same reference, and modifying one will modify the other. For example:
var a = { property1: 'valueA', property2: { property3: 'valueB', property4: { property5: 'valueC' } }, property5: ['valueD', 'valueE'] }; var b = a; b.property2.property3 = 'modified'; console.log(a.property2.property3); // modified
The only way around this is to construct a new object and return it by copying all the non object values. Since the object to copy will be structured like a tree of unknown depth it is best to use recursion. With the base case being a primitive:
var clone = function(obj) { // No point in looping through primitives e.g. strings if (typeof obj !== 'object' || obj === null){ return obj; } var newObj = Array.isArray(obj) ? [] : {}; for (var name in obj) { newObj[name] = clone(obj[name]); } return newObj; }; var b = clone(a); b.property2.property3 = 'modified'; console.log(a.property2.property3); // valueB
Obviously, a costly operation. So it should be used when performance is not a factor. Strangely, there is no function for this in current JavaScript. So a libary function like lodash's clone will need to be used.
Github Location: https://github.com/Jacob-Friesen/obscurejs/blob/master/2015/clone.js
Deep vs. Shallow Copies (Day 16)
Yesterday I came across a problem in my game of life code wherein I was putting my "ticked" cells into a new board array so that I didn't mess up my old board by changing the cells states as I went through. Since the state of the cells in generation 2 depends on its neighbors states, you don't want to change anything as you go through, but what I didn't realize was that just by calling tick on my cell I was changing it in the original array, even though I was putting it into a new array.
So I tried to create a copy by saying new_cell = cell and then put new_cell.tick into my new array. This is how I found out about how Ruby deals with copies. For certain classes, including Arrays and any class you make up yourself, Ruby creates a "shallow copy" when you call my_new_class_obj = my_old_class_object. This means that if you change anything about your copy (my_new_class_obj), it will also change the original (my_old_class_obj). So how do you make a "deep copy"?
There's this thing called marshaling but I didn't get into it. I just created my own method in my Cell class called copy_cell which you call on your old cell object and returns a new cell object with all of the same attributes:
Recap:
shallow copy: if you change the copy you change the original. (copies of your class elements in Ruby are automatically shallow)
deep copy: if you change the copy you DON'T change the original.
Deep copy v.s. shallow copy
'''c
define STR_OUT(X) printf("structure: " #X ": %c, %d, %p, %s, %s, %d\n", X.character, X.strl_len, X.pointer_to_char, X.pointer_to_char, X.char_array, X.instance_of_B.c);
typedef struct B { int c; /* some declarations of B, not important */ } B;
typedef struct A { char character; int strl_len; char *pointer_to_char; char char_array[10]; B instance_of_B; } A;
int main(void) { char v_s[] = "this srting"; A a; A b;
a.character = 'a'; a.pointer_to_char = v_s; a.strl_len = strlen(a.pointer_to_char); a.char_array[0] = 'b'; a.char_array[1] = 'o'; a.char_array[2] = 'o'; a.char_array[3] = 'k'; a.char_array[4] = 0; a.instance_of_B.c = 100;
printf("Step 1\n"); STR_OUT(a); STR_OUT(b);
b = a;
printf("Step 2\n"); STR_OUT(a); STR_OUT(b);
a.pointer_to_char[0] = 'y'; a.char_array[0] = 'y';
printf("Step 3\n"); STR_OUT(a); STR_OUT(b);
b.pointer_to_char = (char *)malloc(b.strl_len); strcpy(b.pointer_to_char, a.pointer_to_char);
printf("Step 4\n"); STR_OUT(a); STR_OUT(b);
b.pointer_to_char[0] = 'x';
printf("Step 5\n"); STR_OUT(a); STR_OUT(b);
return 0; }
$gcc main.c -Wall -o main $./main '''
Step 1 structure: a: a, 11, 0x22ccd0, this srting, book, 100 structure: b: , 0, 0x0, (null), , 0
Step 2 structure: a: a, 11, 0x22ccd0, this srting, book, 100 structure: b: a, 11, 0x22ccd0, this srting, book, 100
Step 3 structure: a: a, 11, 0x22ccd0, yhis srting, yook, 100 structure: b: a, 11, 0x22ccd0, yhis srting, book, 100
Step 4 structure: a: a, 11, 0x22ccd0, yhis srting, yook, 100 structure: b: a, 11, 0x7c0168, yhis srting, book, 100
Step 5 structure: a: a, 11, 0x22ccd0, yhis srting, yook, 100 structure: b: a, 11, 0x7c0168, xhis srting, book, 100 $
Shallow Copies & Deep Copies
I spent a few hours this afternoon pairing with [Jeremy](http://jneander.github.com/) on the project he's been working on (with several of the other resident apprentices). Because I don't know Clojure, I wasn't *pairing* as much as I was observing Jeremy, asking questions, and listening to him explain his thought process and a few nuances of Clojure. As we were talking, the concept of shallow copies and deep copies came up. While I had heard the terms before, I didn't have a clear understanding of their meaning. For example, I wouldn't have felt comfortable explaining them to someone else. When you make a shallow copy it copies each of the member field values. *Example:* person = {:name => "Kyrie", :occupation => "baller"} person_copy = person So now I'm thinking I have a copy of `person` and I can play around with it and my original should remain untouched. person_copy[:occupation] = "trapeze artist" # person_copy is now: {:name=>"Kyrie", :occupation=>"trapeze artist"} # person is also now: {:name=>"Kyrie", :occupation=>"trapeze artist"} What happened? Certain types (symbols, true/false, integers, and others) are immediate values. When you reference one of these types with a variable, in Ruby the variable stores the value directly. When a Fixnum object is passed in as an argument, the actual object is passed (not a reference to that object). With complex objects (like my hash), it stores a reference to the value object. It just got copied as a reference to the original object. Let's see how it's different with integers. *Example:* number = 3 number_copy = number # number is 3 # number_copy is 3 number_copy = 14 # number_copy is now 14 # number is still 3 In Ruby, we have two methods, `dup` and `clone`, that are used to create shallow copies. With shallow copies, modifying one (the original or the copy) will alter the other. Both are sharing the same data. Executing shallow copies is faster though. `dup` and `clone` are slightly different. baseball = Object.new def baseball.throw puts "THROW" end print baseball.throw # => "THROW" another_baseball = baseball.dup print another_baseball.throw # => NoMethodError `clone` does two things `dup` cannot do. It copies the singleton class of the copied object. a_third_baseball = baseball.clone print a_third_baseball.throw # => "THROW" It also maintains the frozen state of the copied object. The clone of a frozen object is also frozen. baseball.freeze print baseball.frozen? # => true a_third_baseball.freeze print a_third_baseball.frozen? # => true another_baseball.freeze print another_baseball.frozen? # => false If we go back to my first example (with Kyrie), we ran into a problem when we noticed that edits made to the copy also changed the original version. There is a way around that, albeit a somewhat hacky way. Instead of: `person_copy = person` ...we will try: person_copy = Marshal.load(Marshal.dump(person)) person_copy[:occupation] = "trapeze artist" # person_copy is now: {:name=>"Kyrie", :occupation=>"trapeze artist"} # person, however, is still: {:name=>"Kyrie", :occupation=>"baller"} Marshalling is a way of making deep copies in Ruby. Deep copies allow the original and the copy to be disconnected. `person_copy` can be modified at no risk to `person`. The variable referencing the copy (`person_Copy`) receives its own local copy of the object. Another quick example of how `dup` and `clone` merely create shallow copies. class Weather attr_accessor :temperature def initialize self.temperature = [65, 61, 57] end end weather = Weather.new print weather.temperature # => [65, 61, 57] more_weather = weather.clone # at this point, more_weather.temperature returns the same thing as weather.temperature print weather.temperature.clear # => [] print more_weather.temperature # => [] Same issue. We modified the original and the copy received the same modification. That's the essence of a shallow copy versus a deep one. *Some Helpful Stuff* [C++ Notes: Shallow vs Deep Copies](http://www.fredosaurus.com/notes-cpp/oop-condestructors/shallowdeepcopy.html) [stackoverflow: What's the differences between ruby dup and clone method?](http://stackoverflow.com/questions/10183370/whats-the-differences-between-ruby-dup-and-clone-method) [Object copy](http://en.wikipedia.org/wiki/Object_copy) [stackoverflow: How to create a deep copy of an object in Ruby?](http://stackoverflow.com/questions/8206523/how-to-create-a-deep-copy-of-an-object-in-ruby) [stackoverflow: What is the difference between a deep copy and a shallow copy?](http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy)