[C++] Vector and Map synchronous?

Started by
6 comments, last by Zahlman 15 years, 5 months ago
Hey, I have this system where I need to have an array describing the order in which the elements are accessed (begin() till end()), and an array to identify the elements regardless of the order in which they are drawn. So, when ever I do some swapping, I want certain user-defined enums (unsigned int's) to still point to the same elements. I figured this would work with a vector and a map together, where the map points to elements in the vector, and user-defined uint's would be the keys for the map. One problem occured, was that I want to be able to swap the order in which they are drawn, since I am swapping vector elements, I only know the value's (not the keys) of the map array which need to be swapped, not their corresponding keys. So, is there a way to swap map elements by values rather then keys, or is there a way to get the corresponding key per value? Or, would you have a better system to make this work? Thank you!
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
You need two things: a "handle" for elements which can be used to get at the element, which never changes over the element's lifetime; and a way to specify the drawing order.

So, don't store the elements in the thing that's responsible for drawing order; store the handles.

A really bare-bones way to do this is to store the elements in a std::list (which never moves its elements around in memory, it just twiddles pointers around to keep things in a doubly-linked list), and use pointer-to-element as the handle type; then use a container (likely std::vector) of pointer-to-element to specify the drawing order. Keep in mind that these are "weak" pointers; you should never call 'delete' through them, because the memory management for the pointed-at elements is already the responsibility of the std::list object (and naturally you should never "clone" the elements when you store one of these pointers in another object and copy that object, etc.)
Quote:Original post by Zahlman
Keep in mind that these are "weak" pointers; you should never call 'delete' through them, because the memory management for the pointed-at elements is already the responsibility of the std::list object
Just to clear up a possible source of confusion: it is the responsibility of the owner of the std::list to delete the objects pointed-at by elements in the list - std::list deletes the elements on its own, but not any objects pointed-at by those elements.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Quote:Original post by Zahlman
Keep in mind that these are "weak" pointers; you should never call 'delete' through them, because the memory management for the pointed-at elements is already the responsibility of the std::list object
Just to clear up a possible source of confusion: it is the responsibility of the owner of the std::list to delete the objects pointed-at by elements in the list - std::list deletes the elements on its own, but not any objects pointed-at by those elements.


And of course this won't be a problem if the elements in the list are a breed of smart pointer, [edit] or the object-type itself. [/edit]

Cheers
-Scott
Thank you Zahlman, nice idea, I will try it out.

No worries on the memory management, I've made and implemented a reference counting system, which works flawlessly (AFAIK :O) :D
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Since there seems to have been some confusion: I was advocating storing the elements themselves in the list - not pointers - and returning the addresses of elements as handles.
Quote:Original post by Zahlman
Since there seems to have been some confusion: I was advocating storing the elements themselves in the list - not pointers - and returning the addresses of elements as handles.


Why not the pointers? It is a heterogeneous array, so I think there isn't really another way then to use pointers.

But the idea to split order and access makes very much sense. Thank you for that :)
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:It is a heterogeneous array


Oh. :) Uh, I'd just work something out with boost::shared_ptr then. :)

This topic is closed to new replies.

Advertisement