Hello,
I am working on a program that must perform a certain task on numeric data stored in a file. The task is given by user input from a list of possible tasks.
The file has some particular format and I have already a loop that is able to read sequentially data from the file. The question is how to operate with the data inside the loop in an efficient way.
The straight forward way (which I did) is creating as many functions as tasks are defined, each function containing the same file reading loop but with a different part inside.
To give a simple example, let's say that I want to sum all the content of the file
And this for all tasks.
However, this is a nightmare to maintain and to expand. It will be also very annoying to perform the same tasks in other file type. That's why I would like to use function objects to do it. However, I am not sure how to do it in an appropriate way. The tasks that I need to perform most of the time need to keep some persistent information. Going back to my example before, Is the following code the most efficient way to do it using function objects? Will the performance be the same as the previous case?
I am using c++ compiled with gcc 4.0.1.
Thanks in advance,
HernĂ¡n
I am working on a program that must perform a certain task on numeric data stored in a file. The task is given by user input from a list of possible tasks.
The file has some particular format and I have already a loop that is able to read sequentially data from the file. The question is how to operate with the data inside the loop in an efficient way.
The straight forward way (which I did) is creating as many functions as tasks are defined, each function containing the same file reading loop but with a different part inside.
To give a simple example, let's say that I want to sum all the content of the file
Code:
double Task1(string &filename) {
// Opening part of the reading loop
// Code to operate on one element at a time and perform task 1
// Closing part of the reading loop
}
double TaskSum(string &filename) {
double sum = 0;
// Opening part of the reading loop Identical!!!
// the value of the last read element is stored in el
sum += el;
// Closing part of the reading loop Identical !!!
return sum;
}
However, this is a nightmare to maintain and to expand. It will be also very annoying to perform the same tasks in other file type. That's why I would like to use function objects to do it. However, I am not sure how to do it in an appropriate way. The tasks that I need to perform most of the time need to keep some persistent information. Going back to my example before, Is the following code the most efficient way to do it using function objects? Will the performance be the same as the previous case?
Code:
class tasks
{
public:
virtual void do(double el)=0;
virtual double result()=0;
};
class sumtask : public tasks
private:
double current;
public:
sumtask(): current(0) {} ;
void do(double el) { current += el; };
double result() { return current; };
};
void Task(string &filename, tasks &t) {
// Opening part of the reading loop Identical!!!
// the value of the last read element is stored in el
t.do(el);
// Closing part of the reading loop Identical !!!
return t.result();
}
int main(int argc, char* argv[]) {
string f = "file.bin";
sumtask t;
double result = Task(f, t);
}
Thanks in advance,
HernĂ¡n
Comment