Jump to content

  • Log In with Google      Sign In   
  • Create Account


What is the best way to implement this "weird" kind of graph in C++ for a game?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
28 replies to this topic

#21 swiftcoder   Senior Moderators   -  Reputation: 9723

Like
0Likes
Like

Posted 10 February 2012 - 06:19 PM

Would the best approach, then, be just to use some kind of graph that does not invalidate iterators on deletion, or has some other type of stable node reference?

You just told me that nodes don't ever get deleted, only strings get deleted. In which case, node iterators can never be invalidated. Which is it?

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


Sponsor:

#22 mike3   Members   -  Reputation: 149

Like
0Likes
Like

Posted 10 February 2012 - 08:26 PM


Would the best approach, then, be just to use some kind of graph that does not invalidate iterators on deletion, or has some other type of stable node reference?

You just told me that nodes don't ever get deleted, only strings get deleted. In which case, node iterators can never be invalidated. Which is it?


Deleting a string deletes the associated nodes. That happens in the string object's destructor. If you were thinking of this post:

http://www.gamedev.net/topic/619851-what-is-the-best-way-to-implement-this-weird-kind-of-graph-in-c-for-a-game/page__view__findpost__p__4910656

I was trying to say you could not delete nodes individually, not that they couldn't be deleted at all, as deleting a string entails deleting all the nodes that belong to it. Sorry if that wasn't clear.

#23 swiftcoder   Senior Moderators   -  Reputation: 9723

Like
0Likes
Like

Posted 10 February 2012 - 08:43 PM

Deleting a string deletes the associated nodes. That happens in the string object's destructor.

But surely, the same node can occur in more than one string? If so, what happens to the other strings when a node they depend on is deleted by another string?

If not, then your strings are disjoint subsets of the graph, and there is no particular advantage to storing the nodes in a shared container.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#24 mike3   Members   -  Reputation: 149

Like
0Likes
Like

Posted 10 February 2012 - 10:41 PM


Deleting a string deletes the associated nodes. That happens in the string object's destructor.

But surely, the same node can occur in more than one string? If so, what happens to the other strings when a node they depend on is deleted by another string?

If not, then your strings are disjoint subsets of the graph, and there is no particular advantage to storing the nodes in a shared container.


Yes, they are disjoint. But they can be connected together by connecting nodes from different strings with an edge, and thus I use one graph, to represent the whole "web" of nodes. And also, because building on a standard graph type was suggested on one of the other forums I asked this question on, as it's a "standard" kind of data structure.

#25 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 11 February 2012 - 09:22 AM

::iterator types are for iteration. While some may be held on to, it causes aforementioned problems.

In most newer languages, iterators are explicitly checked for modification and will cause failures when trying to use them as references.

Iterators are not identity types and cannot be used for such purpose, even though in some corner cases, depending on implementation, they can temporarily take such role.

Instead of insisting on such approach, use a different approach. Establish strong identity (NodeID, StringID, EdgeID). Keep track of those separately as well as their connections. Then build structures on top of that.


There really are no difficulties here. It's just a basic list/tree structure, which iterators make impossible to implement if any of them can be mutated, something that iterators, as a concept, cannot handle. Only general solution to this problem is garbage collection, which doesn't solve cross-linking issues.

#26 mike3   Members   -  Reputation: 149

Like
0Likes
Like

Posted 11 February 2012 - 03:07 PM

::iterator types are for iteration. While some may be held on to, it causes aforementioned problems.

In most newer languages, iterators are explicitly checked for modification and will cause failures when trying to use them as references.

Iterators are not identity types and cannot be used for such purpose, even though in some corner cases, depending on implementation, they can temporarily take such role.

Instead of insisting on such approach, use a different approach. Establish strong identity (NodeID, StringID, EdgeID). Keep track of those separately as well as their connections. Then build structures on top of that.


There really are no difficulties here. It's just a basic list/tree structure, which iterators make impossible to implement if any of them can be mutated, something that iterators, as a concept, cannot handle. Only general solution to this problem is garbage collection, which doesn't solve cross-linking issues.


However, I've noticed in some implementations like "Boost" that there are "descriptors" in addition to iterators, but at least in the case of Boost, with some forms of the graph these too can be invalidated, and so we get the same problems. Would I be right in guessing that these problems are saying that messing around with invalidating references is the wrong way to go, and there's simply no way to use invalidating references in this way without making code a mess? As it seems unavoidable, and so I'm really strongly thinking of using your ID-tag approach. Especially considering I was already going in that direction with giving strings ID codes by which one could reference and access them. The only downside I can see is the added time complexity of the accesses, but considering that this is not performance-critical code that is not actually a problem.

#27 mike3   Members   -  Reputation: 149

Like
0Likes
Like

Posted 20 February 2012 - 04:26 PM

NEW: Well, now I've settled on the approach of using a graph that can refer to nodes by ID codes instead of iterators. This removes problems 1, 2, and 3 in my original post, but I'm still not sure what to do about problem 4. Is it okay for the "string" class to be "unsafe to copy" due to how it creates/deletes iterators in the graph (see the OP)?

#28 LorenzoGatti   Crossbones+   -  Reputation: 2580

Like
0Likes
Like

Posted 21 February 2012 - 03:33 AM

::iterator types are for iteration. While some may be held on to, it causes aforementioned problems.

In most newer languages, iterators are explicitly checked for modification and will cause failures when trying to use them as references.

Iterators are not identity types and cannot be used for such purpose, even though in some corner cases, depending on implementation, they can temporarily take such role.


This is how iterators work in the real world: their lifespan is limited to some activity that iterates through members of a data structure, and in this small scope one can take care not to invalidate them with inappropriate operations.
If you insist on storing iterators instead of plain pointers or names, you are abusing the standard library and looking for trouble.
Produci, consuma, crepa

#29 mike3   Members   -  Reputation: 149

Like
0Likes
Like

Posted 27 February 2012 - 05:53 PM


::iterator types are for iteration. While some may be held on to, it causes aforementioned problems.

In most newer languages, iterators are explicitly checked for modification and will cause failures when trying to use them as references.

Iterators are not identity types and cannot be used for such purpose, even though in some corner cases, depending on implementation, they can temporarily take such role.


This is how iterators work in the real world: their lifespan is limited to some activity that iterates through members of a data structure, and in this small scope one can take care not to invalidate them with inappropriate operations.
If you insist on storing iterators instead of plain pointers or names, you are abusing the standard library and looking for trouble.


Actually, I had programmed my own graph implementation and featured iterators. So I guess I was abusing the concept of iterator, then. But there's no problem now -- I've since replaced that graph with a new version that uses ID codes to give stable references, like Antheus suggested. Granted, in theory the use of std::map gives a performance hit, but this code is not used in anything performance-critical so that is no problem.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS