daniel_t@earthlink.net (Daniel T.) wrote (abridged):
I would be interested in code that solved the same problem that
the original code solved. Or are you agreeing that the original
code was the best, for that problem? Can you only do better if you
change the problem by adding extra invariants?
Am I adding extra invariants? I don't know what the invariants are,
and I have no reason to believe that the code presented solved any
problem at all.
That's part of the point I'm trying to make here. We have been
handed an incomplete solution to a nonexistent problem and you are
asking me if I could write code that "solved the same problem..."
Here you go:
int main() { }
My example above has the benefit of actually being able to compile
and run. You can't do that with the original example.
The original code was:
Value_t* MyClass::findValue(const Value_t& value)
{
for(size_t xInd = 0; xInd < data.size(); ++xInd)
for(size_t yInd = 0; yInd < data[xInd].size(); ++yInd)
for(size_t zInd = 0; zInd < data[xInd][yInd].size(); ++zInd)
{
if(data[xInd][yInd][zInd] == value)
return &data[xInd][yInd][zInd];
}
return 0;
}
If you are truly saying you don't understand what problem this code is
trying to solve, to the point where you claim "int main() { }" is
solving the same problem, then I have trouble believing you are
debating in good faith. How can your code comprehension skills be so
poor?
If it helps, I'll add some code.
#include <vector>
using namespace std;
typedef int Value_t;
typedef vector<Value_t> z_vec;
typedef vector<z_vec> y_vec;
typedef vector<y_vec> x_vec;
struct MyClass {
x_vec data;
void test();
Value_t *findValue( const Value_t& value);
};
int main() {
MyClass().test();
}
void MyClass::test() {
findValue( 0 );
}
Putting this in front of the original code is enough to make it
compile. Any competent C++ programmer could have come up with it. I'll
let you write some code to initialise MyClass::data from stdin
yourself. If you are truly incapable of doing that, then I have to
question whether your opinions are worth any weight at all. (I don't
need to see the code. I just want you to stop being obtuse, when you
clearly know better.)
There are other ways to define data etc so that the original code
compiles. It shouldn't be necessary to assume more about the problem
than what the original code gave us, which was basically operator[]
and size() as used. That was enough for the original code. Alternative
solutions ought to work with any reasonable definitions, including
this one.
You shouldn't assume that the data are contiguous, or that someone
else will write a convenient magic iterator for you.
that sometimes multiple returns are the best solution... I've already