mcostalba wrote:mcostalba wrote: to be continued...
This is my attempt:
Code: Select all
struct BSPNode
{
BSPNode *front, *back;
};
struct MapNode
{
int front, back;
};
class Map
{
std::vector< MapNode > nodes;
public:
int addNode(const BSPNode *node, int &idx);
};
int Map::addNode(const BSPNode *node, int &idx )
{
if ( !node )
return -1;
idx++;
nodes.push_back(MapNode{ addNode( node->front, idx ), addNode( node->back, idx ) });
return idx;
}
void test() {
Map map;
BSPNode* root = 0;
int index = - 1;
map.addNode( root, index );
}
Note that now root is stored at the end of the vector, don't know if for you is ok.
P.S: Not tested !
I think it should work, but this code is really hard to understand plus you now need to call it with an extra integer parameter that MUST be initialised to -1 and for which there is no apparent need from the point of view of a programmer invoking addNode().
And of course this code leads to a different result, so is no real substitute.
The original code is much simpler to understand. It just tripped over a missing sequence point.
Hmm, if we want to talk about bad design....

I remember a recent SF patch to fix a problem due to assignment not being a sequence point:
Code: Select all
o["Write Debug Log"] = Option(false, on_logger);
This first parameter is stored with index Options.size(), which is either 0 or 1 depending on what's evaluated first (left-hand side or right-hand side of the assignment).
The problem shows itself in the code that prints out all options:
Code: Select all
for (size_t idx = 0; idx < om.size(); ++idx)
If the left-hand side is evaluated first, indices could run from 1 to om.size() instead of the expected 0 to om.size()-1.
This was fixed as follows:
Code: Select all
for (size_t idx = 0; idx < om.size() + 1; ++idx) // idx could start from 1
But this is just a stopgap measure. It does not address the real issue.
The compiler is free to choose between evaluating the left-hand side first or the right-hand side first. It can make a different choice for each option!
So:
Code: Select all
o["Write Debug Log"] = Option(false, on_logger);
o["Write Search Log"] = Option(false);
Could assign index 1 to both "Write Debug Log" and "Write Search Log".
Maybe the compiler of today will not do this, but the compiler of tomorrow might.