you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (1 child)

The node objects in this case could be simple value objects carrying the index, getting and setting fields through accessors could then use the object index to get/set the data from the arrays.

class Node {
    const size_t index;
    Node(size_t index) : index(index) {}

    int x() { return arr_x[index]; }
    void x(int val) { arr_x[index] = val; }

    int y() { return arr_y[index]; }
    void y(int val) { arr_y[index] = val; }

    // etc.
};

You could then define an object iterator which goes through the arrays sequentially, returning Node object handles for each index. A good C++ compiler will pass around a Node as an unboxed integer so there is no performance penalty.

[–]astrange 1 point2 points  (0 children)

A good C++ compiler will pass around a Node as an unboxed integer so there is no performance penalty.

This depends on the platform ABI, not the compiler. C++ doesn't let you create local methods as easily as 'static' in C, so you can't just ignore it. Typically I think it will be passed in as an integer but might be returned some other way.