Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm making several assumptions based on your writeup:</p> <ul> <li>Keys are cheap to copy and compare</li> <li>There should be only one copy of the object in the system</li> <li>The same key may refer to many objects, but only one object corresponds to a given key (one-to-many)</li> <li>You want to be able to efficiently look up which objects correspond to a given key, and which key corresponds to a given object</li> </ul> <p>I'd suggest:</p> <ul> <li>Use a linked list or some other container to maintain a global list of all objects in the system. The objects are allocated on the linked list.</li> <li>Create one <code>std::multimap&lt;Key, Object *&gt;</code> that maps keys to object pointers, pointing to the single canonical location in the linked list.</li> <li>Do one of: <ul> <li>Create one <code>std::map&lt;Object *, Key&gt;</code> that allows looking up the key attached to a particular object. Make sure your code updates this map when the key is changed. (This could also be a <code>std::multimap</code> if you need a many-to-many relationship.)</li> <li>Add a member variable to the <code>Object</code> that contains the current <code>Key</code> (allowing O(1) lookups). Make sure your code updates this variable when the key is changed.</li> </ul></li> </ul> <p>Since your writeup mentioned "coordinates" as the keys, you might also be interested in reading the suggestions at <a href="https://stackoverflow.com/questions/72128/fastest-way-to-find-if-a-3d-coordinate-is-already-used#72178">Fastest way to find if a 3D coordinate is already used</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload