We've some function foo(g, v, output)
that accepts a vertex v
searches some graph g
for nearby vertices and returns some vector of information about the path (steps) it took during that search.
Now the steps are a big struct (getting bigger) and we often just want some specific datum (member of the struct) from each step.
The issue is that we now have multiple functions (duplicate code) doing something like this:
foo_get_xs(g, v, result) {
// initialize vectors needed to catch output from foo (1)
foo(g, v, output); // (2)
// parse output to get only xs // (3)
}
foo_get_xs(g, v, result) {
// initialize vectors needed to catch output from foo
foo(g, v, output);
// parse output to get only ys
}
foo_get_xs(g, v, result) {
// initialize vectors needed to catch output from foo
foo(g, v, output);
// parse output to get only zs
}
How could you make it so there is only one function that accepts an enum possibly and returns a vector filled by the desired field?
The issue is that each field is of a different type. Other than that we duplicate (1), (2) and most of (3).
* In reality it's actually worse because we actually need to test with every v
so there is a function that calls foo_xs
, a function that calls foo_ys
and so on...
An option could be to pass a callback function which is invoked on every step along the path.
Then you would provide several functions that fill a vector with the desired field. Unfortunately, the vector needs to be abstract.
Alternatively, use a template for the different field types that you need. This will avoid duplication of the source code.
You can also consider using a vector of references to the desired nodes, and transfer the individual fields as a post-processing operation.