Combining similar functions into one common function, including the functions of function pointers as parameters

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 function parameters to have a tpl member that should be passed as a pointer, with size and rho / l passed as a value. searchTmp is a double-precision pointer to a file region. Is there an easy way to pass & NeedleUSsim :: GreaterThanOrEqualTo as a parameter to the function I'm trying to write easily?

Thanks in advance for your advice.

+5
source share
5 answers

The easiest way to make your code more versatile:

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;
  }
}
+6
source

& NeedleUSsim:: GreaterThanOrEqualTo , ?

.

  • ( )

eJames.

2

-. (). :

 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) {...}
 }

ICompatator, .

3

templatizing

template <class Comparator>
void foo(...)
{
   ...
   Comparator comparer;
   std::find_if(..., comparer);
}

foo :

foo<CComparatorGreaterThan>(...);

. . - , () bool.

+3

- :

typedef bool (NeedleUSsim::*compFunctionPtr)(NeedleUSsim &x, NeedleUSsim &y);

void NeedleUSsim::FindIdxRho(compFunctionPtr comparison)
{
    //..
    p = std::find_if(tplRho_deg, tplRho_deg+sampleDim[2], comparison);
    //..
}

:

//..
someNeedleUSsim.FindIdxRho(&NeedleUSsim::LessThanOrEqualTo);
//..

this ++ FAQ Lite .

+1

- ( )

template <typename F>
void NeedleUSsim::FindIdx(double *ninfoMember, double *tplParam, size_t dimension, F CompareFunc, int &target)
{
    searchTmp = ninfoMember;
    double *p = std::find_if(tplParam, tplParam+sampleDim[dimension], CompareFunc);
    while(p != tplParam+sampleDim[dimension])
    {
        target= p - tplParam;
        p = std::find_if(p+1, tplParam+sampleDim[dimension], CompareFunc);
    }
}

:

FindIdx(&ninfo->rho, tplRho_deg, 2, &NeedleUSsim::GreaterThanOrEqualTo, idxRho);
FindIdx(&ninfo->l, tplL, 1, &NeedleUSsim::LessThanOrEqualTo, idxL);
+1
double *p = std::find_if(b, e, &NeedleUSsim::GreaterThanOrEqualTo);
while(p != e)
{
    idxRho = p - b;
    p = std::find_if(p + 1, e, &NeedleUSsim::GreaterThanOrEqualTo);
}

, , , .

std::reverse_iterator<double*> rb(e), re(b);
rb = std::find_if(rb, re, &NeedleUSsim::GreaterThanOrEqualTo);
if(rb != re) {
    idxRho = re - rb;
}

, . idxRho , - , ,

std::reverse_iterator<double*> rb(e), re(b);
idxRho = re - std::find_if(rb, re, &NeedleUSsim::GreaterThanOrEqualTo);
+1

All Articles