Timeline for Using vectors of shared pointers to objects in my C++ code to prevent object duplication
Current License: CC BY-SA 3.0
21 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 1, 2018 at 4:52 | answer | added | user204677 | timeline score: 1 | |
| Nov 15, 2017 at 7:17 | history | tweeted | twitter.com/StackSoftEng/status/930696201388912640 | ||
| Nov 11, 2017 at 16:08 | vote | accept | AnInquiringMind | ||
| Nov 11, 2017 at 4:23 | answer | added | user1118321 | timeline score: 0 | |
| Nov 10, 2017 at 23:33 | comment | added | AnInquiringMind | @Omnifarious Thanks for all the replies! You've given me quite a bit of options and advice. Would you mind augmenting your answer with a bit of an overview/summary for future (viewers') reference? If you don't want to / have time to, I totally understand. In any case, thank you! | |
| Nov 10, 2017 at 23:26 | comment | added | Omnifarious |
You can use the find_if and other '_if` algorithms. This will slow accessing a contact from a particle down considerably. en.cppreference.com/w/cpp/algorithm
|
|
| Nov 10, 2017 at 22:45 | comment | added | AnInquiringMind |
@Omnifarious Then I think the best way forward might be to assign a unique contact ID/index (or a unique pair corresponding to the particle IDs) to each contact so that it doesn't matter where it is located in the vector... Though that might result in always having to rely on if statements to find the correct contact?
|
|
| Nov 10, 2017 at 22:39 | comment | added | Omnifarious | And, of course, if you use a list, then you completely destroy any locality of reference advantage you have. | |
| Nov 10, 2017 at 22:37 | comment | added | Omnifarious | You've got a few choices. If you have a vector and you have pointers into the middle of it, any operation that changes the size of the vector (push_back, insert, remove, etc...) will invalidate all of those pointers. If you have indexes, then only operations that remove things from the middle of the vector and squish up all the remaining elements will invalidate them. If neither choice is acceptable, you need to have a dequeue or list instead. And then pointers will not be invalidated and indexes are a bad idea. | |
| Nov 10, 2017 at 22:30 | comment | added | AnInquiringMind | @Omnifarious I might be mistaken, but isn't the fact that the number of contacts, Nc, isn't fixed, and that contacts can change mean that we should use pointers rather than indices? Unless you mean a unique contact index that I assign to each contact,, and not the index in the vector... | |
| Nov 10, 2017 at 22:05 | comment | added | Omnifarious |
Yes. In fact, you should very definitely not use shared pointers in this case. This is because the vector<Contact> holds Contact objects, not pointers to them. So it's the sole manager of their lifetime. In fact, you should use indexes into this array, not pointers at all, because this array might move around as you add and remove contacts.
|
|
| Nov 10, 2017 at 21:44 | comment | added | AnInquiringMind |
@Omnifarious The Network class will ideally be flexible and allow for both cases (depending on which is the main control parameter – particle positions or contact forces). That's why I have defined both as vectors. But in terms of having a list of contacts per particle, knowing the extra information I have provided, is my implementation outlined in my question okay (with shared pointers being replaced with regular pointers, as you suggested)? Or is more information still needed? Thanks for the continued help!
|
|
| Nov 10, 2017 at 21:36 | comment | added | Omnifarious |
Will the simulation engine basically loop through the contacts and use the information to update the associated particles? Or will you be looping through the particles? The thing you loop through should be allocated in all contiguous memory. Basically as members of a vector or something like that.
|
|
| Nov 10, 2017 at 21:24 | comment | added | AnInquiringMind | @Omnifarious I added some additional information to the question that is hopefully helpful. But I will want to use this for various simulations (e.g., Monte Carlo) where I am changing certain positions and/or contact forces of the contacts or positions of the particles at every iteration. | |
| Nov 10, 2017 at 20:41 | history | edited | AnInquiringMind | CC BY-SA 3.0 |
Added some extra info
|
| Nov 10, 2017 at 20:31 | review | Close votes | |||
| Nov 15, 2017 at 3:02 | |||||
| Nov 10, 2017 at 19:22 | answer | added | Chris Drew | timeline score: 7 | |
| Nov 10, 2017 at 18:53 | comment | added | Omnifarious | What is your typical access pattern for the most compute intensive steps of the algorithms you're applying to this data structure? | |
| Nov 10, 2017 at 18:44 | answer | added | Omnifarious | timeline score: 3 | |
| Nov 10, 2017 at 18:38 | history | edited | AnInquiringMind | CC BY-SA 3.0 |
Deleted words
|
| Nov 10, 2017 at 18:24 | history | asked | AnInquiringMind | CC BY-SA 3.0 |