I am trying to combine the following two functions into one portable function:
void NeedleUSsim::FindIdxRho()
{
searchTmp = &ninfo->rho;
double *p = std::find_if(tplRho_deg, tplRho_deg+sampleDim[2], &NeedleUSsim::GreaterThanOrEqualTo);
while(p != tplRho_deg+sampleDim[2])
{
idxRho = p - tplRho_deg;
p = std::find_if(p+1, tplRho_deg+sampleDim[2], &NeedleUSsim::GreaterThanOrEqualTo);
}
}
void NeedleUSsim::FindIdxDepth()
{
searchTmp = &ninfo->l;
double *p = std::find_if(tplL, tplL+sampleDim[1], &NeedleUSsim::LessThanOrEqualTo);
while(p != tplL+sampleDim[1])
{
idxL = p - tplL;
p = std::find_if(p+1, tplL+sampleDim[1], &NeedleUSsim::LessThanOrEqualTo);
}
}
Ideally, I want the parameters of the function to have tpl member to be passed as a pointer, with the size and rho/l passed as value. searchTmp is a file scope double precision pointer. Is there any easy way of passing &NeedleUSsim::GreaterThanOrEqualTo function as a parameter of the function that I'm trying to write easily?
Thanks in advance for the advice.
The simplest way to make your code a bit more generic is the following :
template<typename ComparisonType>
double* NeedleUSsim::FindIdx(double* containerBegin, double* containerEnd, ComparisonType comparison) {
double* p = std::find_if(containerBegin, containerEnd, comparison);
double* idx = 0;
while(p != containerEnd)
{
idx = p - containerBegin;
p = std::find_if(p+1, containerEnd, comparison);
}
return idx;
}
void NeedleUSsim::FindIdxRho()
{
searchTmp = &ninfo->rho;
double* idx = FindIdx(tplRho_deg, tplRho_deg+sampleDim[2], &NeedleUSsim::GreaterThanOrEqualTo);
if( idx != 0 )
{
idxL = idx;
}
}
void NeedleUSsim::FindIdxDepth()
{
searchTmp = &ninfo->l;
double* idx = FindIdx(tplL, tplL+sampleDim[1], &NeedleUSsim::LessThanOrEqualTo);
if( idx != 0 )
{
idxRho = idx;
}
}
Is there any easy way of passing &NeedleUSsim::GreaterThanOrEqualTo function as a parameter of the function that I'm trying to write easily?
There's a couple ways to do this.
The first method is covered above by eJames.
The second method involves wrapping your comparison functions in some function-object hierarchy. A function object is an instance of a class with the () operator overloaded. This makes the instance callable:
class IComparator
{
public:
virtual bool operator()(lhs, rhs) = 0;
}
class CComparatorLessThan : public IComparator
{
public:
virtual bool operator()(lhs, rhs) {...}
}
class CComparatorGreaterThan : public IComparator
{
public:
virtual bool operator()(lhs, rhs) {...}
}
Your common function would take an ICompatator reference and the behavior would be dynamically bound at runtime.
The third method involves templatizing instead of creating an object hierarchy
template <class Comparator>
void foo(...)
{
...
Comparator comparer;
std::find_if(..., comparer);
}
then calling foo would involve:
foo<CComparatorGreaterThan>(...);
This eliminates a lot of the runtime overhead of the second solution. Here you don't have to define the base class. You only have to have some kind of class that has operator() overloaded and will return bool.
It is possible to pass a member function pointer to a function as follows:
typedef bool (NeedleUSsim::*compFunctionPtr)(NeedleUSsim &x, NeedleUSsim &y);
void NeedleUSsim::FindIdxRho(compFunctionPtr comparison)
{
//..
p = std::find_if(tplRho_deg, tplRho_deg+sampleDim[2], comparison);
//..
}
Which can then be called like so:
//..
someNeedleUSsim.FindIdxRho(&NeedleUSsim::LessThanOrEqualTo);
//..
Have a look at this C++ FAQ Lite article for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With