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

Started by
27 comments, last by mike3 12 years, 1 month ago

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. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement

[quote name='mike3' timestamp='1328918404' post='4911825']
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?
[/quote]

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.

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. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


[quote name='mike3' timestamp='1328927187' post='4911857']
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.
[/quote]

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.
::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.

::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.
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)?

::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.

Omae Wa Mou Shindeiru


[quote name='Antheus' timestamp='1328973736' post='4911974']
::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.
[/quote]

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.

This topic is closed to new replies.

Advertisement