f0(..., (f1)( ..., (f2)(...), ...), ... )
I'm totally lost on some function pointer syntax. I'm working on a hw assignment and I've built some automated execution time analysis stuff to look at templatized sort functions that can accept vectors of general elements where these elements have a correpsponding comparison function defined to determine element ordering. This comparison function is passed as a callback function to the templatized Sort function because non-primitive types of data can't rely on the built in relational operators to establish ordering (ex Ball_1 < Ball_2). Basically I'm trying to sort general elements given a defined ordering for those elements, which could be strings, stacks, queues, maps, etc, so long as a definite ordering can be defined for the Sort function to work with them.
I'm not sure how to define the function header for the timing analysis function I'm writing, or even how to call this function once properly defined.
The difficulty I'm stuck with is that I need to pass a Sort function parameter to the timing function (ie which sorting method do you wish to time analyze?), but this Sort function also has a function argument of its own which is the specialized comparison function used to determine order (ie for these elements you wish to sort how does one determine relational order? <, >, == ). This function argument of a function with its own function argument has got me baffled.
For the sake of being concrete the particular sorting problem I'm dealing with is that I need to sort a vector of integer sets, where set order is defined by the sum of their elements. I have the following function definition shown below, which I'm calling in main() with the following line, where VectorOfSetsOfIntegers is a vector containing a list of integer sets, along with the needed CompareSets comparison function needed to define the set ordering just mentioned. And Sort is a templatized sorting method that operates on vectors of any type, given their comparison function. Any help would be greatly appreciated, you'd really be teaching me some deep syntax.
Calling the Function (is this how it's done?):
GeneralTimeComputation( VectorOfSetsOfIntegers, Sort);
Function Definition:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
template <typename Type>
double GeneralTimeComputation(Vector<Type> &elems2sort, void (sort_func)(Vector<Type> &, int (cmpFn)(Type, Type)) ){
double start, finish, elapsed = 0;
int iterations = 1;
Vector<Type> elems2sortCOPY = elems2sort;
start = double(clock()) / CLOCKS_PER_SEC;
while(true){
(sort_func)(elems2sortCOPY, cmpFn);
finish = double(clock()) / CLOCKS_PER_SEC;
elapsed = finish - start;
if(elapsed == 0){
iterations++;
elems2sortCOPY = elems2sort; // to repeat the sorting operation with unsorted elements
}
else
break;
}
return (elapsed / (double) iterations);
}
| |