Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[C++] Vector and Map synchronous?

This topic is 3635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Quote:
It is a heterogeneous array


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!