Jump to content
  • Advertisement
Sign in to follow this  
chbrules

[C++]Best Way To Do This...

This topic is 4746 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

What I'm trying to do is create a bacteria culture simulation program. I figured I'd use a vector of structs to add and delete when I needed to 'kill off' or 'create' a new bacteria to the culture. I just found out that I can't just tell the vector to delete any random element of itself at any time. What would any of you recommend in creating such a program? Maybe just a struct array and setting 'dead' bacteria elements to NULL value instead? Then the RAM isn't free though. Not too sure how to go about this, I hope I've explained it clear enough. Thank you!

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure of either of those. I just want to make something that isn't overly complex. There will probably only be maybe a couple thousand bacteria at any given time, which might be like 10-20MB of RAM. It's all console based, statistical, and real-time.

Share this post


Link to post
Share on other sites
Well, you can delete random elements from a vector with std::vector::erase, however if your doing this a lot from anywhere in the vector then you'll be better off using a linked list (std::list). You'll probably also benefit from using a pooled allocator such as boost::fast_pool_allocator .

Are you storing pointers to structs or the structs themselves? If your storing pointers, you could use the method of writing a NULL at the location, and then when allocating new bacteria either keep a list of free indices or just scan through the vector for NULLs (may not be as bad as it sounds, depending on how large your array is). Keep in mind you need to deallocate the struct's memory yourself if you do it this way.

If your not using pointers, then you can't set the elements to NULL. Instead, you could use a flag thats part of the struct to indicate that the bacteria in that element is 'dead' and can be replaced with a new bacteria.

Share this post


Link to post
Share on other sites
I think the most efficient way might be having a 'dead' flag in the struct. I don't want to put too much extra strain on the system since I'll be dealing with thousand of objects. I'm using a vector of structs for this.

Edit: Basically I have 4 classes of bacteria that will 'live' in this virtual culture. They each have different lifespans, health, reproduction rates, and they can move randomly each turn. If any 2 bacteria come in contact on the 'playing field,' they will drain each others' life each cycle (each turn the system goes around) until they're not touching anymore. If one dies, it will be taken out of the game and the rest of the culture will continue until only one type of bacteria is left.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

If the order of the vector doesn't matter (huge if), then you could just copy the last element to the element which you are deleting, and then perform a pop_back()

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster

If the order of the vector doesn't matter (huge if), then you could just copy the last element to the element which you are deleting, and then perform a pop_back()


Not a bad idea, might save a little space in RAM, but I think it might be a little less efficient.

Share this post


Link to post
Share on other sites
Why are you insistent on using a vector of structs? Why not a vector of pointers to structs on the heap? Then it's easy to just reallocate the location of a pointer within the vector a la Mr. Annonymous Poster's suggestion.

so...



// Make a function that accepts an integer argument(the bacteria's location
// within the vector, and delete the pointer there, replacing it with the
// last element of the vector and popping the back.
void DeleteBacteria(int x)
{
vector<*Bacteria>::iterator iter = x;
delete *iter;
*iter = Bacteria.end();
Bacteria.pop_back():
}



I may be way off base with this, as I'm just beginning to get the hang of how vectors of pointers work and all, but I'm pretty sure that might just solve your problem... maybe hopefully...

Vopisk

Share this post


Link to post
Share on other sites
Quote:
Original post by chbrules
I think the most efficient way might be having a 'dead' flag in the struct. I don't want to put too much extra strain on the system since I'll be dealing with thousand of objects. I'm using a vector of structs for this.


Perhaps better yet would be keep a separate std::vector<bool> (IIRC it's specialized to use a single bit per bool value), where each bool indicates whether the corresponding bacteria in your main vector is dead or alive. This would make it *very* fast to scan through and find a dead bacteria to replace with a living bacteria since with only a couple of 1000 bacteria the entire vector would fit in the L1 cache

Share this post


Link to post
Share on other sites
Vopisk, I think the code you want is this:

void DeleteBacteria(std::vector<Bacteria*>& bacteria, int idx)
{
delete bacteria[idx];
bacteria[idx] = bacteria.back();
bacteria.pop_back();
}

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!