shallow-copy

Does LINQ new up memory when creating returns

て烟熏妆下的殇ゞ 提交于 2019-12-07 03:10:37
问题 Does LINQ actually perform a deep copy of the results to a different list/array/etc, or does it simply give me a list/array/etc. composed of references to the original? 回答1: It's going to depend on if (and how) you use Select to project the results. If you do not create new objects in a projection then the result will reference the same objects as the original collection. If, however, you create new objects in the project then, obviously, they will not be the same. The collection returned

Java - Implement deep and shallow copy of an array

可紊 提交于 2019-12-06 23:01:13
问题 I am trying to understand the concept of shallow vs deep copy in Java. There is a lot of articles and Q&A about this subject, but whenever I try to implement these concepts in a real Java code, everything become unclear to me. One of the answers on which I base my understanding is in this link, where deep and shallow copying are explained via schemas. I will show you below my implementation for each case: Shallow copy: I took for my example the method System.arraycopy() as I read in many

Creating clone of an object not working with virtual base class

断了今生、忘了曾经 提交于 2019-12-06 16:09:28
问题 #include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<"Something initialized. j="<<j<<endl;} }; class Base { private: Base(const Base&) {} public: Base() {} virtual Base *clone() { return new Base(*this); } virtual void ID() { cout<<"BASE"<<endl; } }; class Derived : public Base { private: int id; Something *s; Derived(const Derived&) {} public: Derived():id(10) {cout<<"Called constructor and allocated id"<<endl;s=new Something();} ~Derived()

Avoiding ConcurrentModificationException on List by making a shallow copy

て烟熏妆下的殇ゞ 提交于 2019-12-05 16:19:03
I have a class like the following: class Test { private LinkedList<Person> persons = new LinkedList<Person>; public synchronized void remove(Person person) { persons.remove(person); } public List<Person> getAllPersons() { // Clients may iterate over the copy returned and modify the structure. return new ArrayList<Person>(persons); } } persons may be modified concurrently: one is via remove() by one thread and two via the shallow copied instance returned by getAllPersons() . I have tested the above scenario in a multithreaded environment to see if I can avoid ConcurrentModificationException by

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

Does LINQ new up memory when creating returns

狂风中的少年 提交于 2019-12-05 09:02:36
Does LINQ actually perform a deep copy of the results to a different list/array/etc, or does it simply give me a list/array/etc. composed of references to the original? It's going to depend on if (and how) you use Select to project the results. If you do not create new objects in a projection then the result will reference the same objects as the original collection. If, however, you create new objects in the project then, obviously, they will not be the same. The collection returned here will contain references to the same objects in _myCollection : from m in _myCollection where m

Git cannot create shallow-since locally

徘徊边缘 提交于 2019-12-05 06:45:13
I am trying to create a --shallow-since working clone from a local bare clone but it keeps pulling everything. --depth=N works fine. I'm thinking the issue is I'm using the wrong format? I've tried searching but no where does it explicitly say what format < date > is supposed to be for --shallow-since=< date >. Format is YYYY-MM-DD See above comment for what confused me and how that confusion was resolved. While the date format is indeed, for instance, YYYY-MM-DD, " git fetch --shallow-since=<cutoff> " had a problem: If you specify a cut-off point that is newer than the existing history, it

Java - Implement deep and shallow copy of an array

一曲冷凌霜 提交于 2019-12-05 03:18:41
I am trying to understand the concept of shallow vs deep copy in Java. There is a lot of articles and Q&A about this subject, but whenever I try to implement these concepts in a real Java code, everything become unclear to me. One of the answers on which I base my understanding is in this link , where deep and shallow copying are explained via schemas. I will show you below my implementation for each case: Shallow copy: I took for my example the method System.arraycopy() as I read in many articles that it performs a shallow copy (along with the clone method) public class Test { public static

Why is copying a list using a slice[:] faster than using the obvious way?

可紊 提交于 2019-12-04 23:36:29
Why is shallow-copying a list using a slice so much faster than using list builtin? In [1]: x = range(10) In [2]: timeit x_ = x[:] 10000000 loops, best of 3: 83.2 ns per loop In [3]: timeit x_ = list(x) 10000000 loops, best of 3: 147 ns per loop Usually when I see weird things like this, they're fixed in python3 - but this discrepancy is still there: In [1]: x = list(range(10)) In [2]: timeit x_ = x[:] 10000000 loops, best of 3: 100 ns per loop In [3]: timeit x_ = list(x) 10000000 loops, best of 3: 178 ns per loop The difference is in additional function call (just SLICE+0 vs CALL_FUNCTION 1

Creating clone of an object not working with virtual base class

流过昼夜 提交于 2019-12-04 22:25:41
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<"Something initialized. j="<<j<<endl;} }; class Base { private: Base(const Base&) {} public: Base() {} virtual Base *clone() { return new Base(*this); } virtual void ID() { cout<<"BASE"<<endl; } }; class Derived : public Base { private: int id; Something *s; Derived(const Derived&) {} public: Derived():id(10) {cout<<"Called constructor and allocated id"<<endl;s=new Something();} ~Derived() {delete s;} virtual Base *clone() { return new Derived(*this); } virtual void ID() { cout<<"DERIVED id="<<id<