开发者

Is there a way to optimize this code?

I am working on this small piece of code, though it works as i expected, but i need it to be optimized. Please suggest me some ideas.

Here is important piece of my code:

std::replace_if(inputImage,inputImage+ m_xDim*m_yDim*m_zDim, bind2nd(std::less<float>(), 0), 0); // replace the values <0 with 0 of array input

std::replace_if(inputImage,inputImage+ m_xDim*m_yDim*m_zDim, bind2nd(std::greater&开发者_开发百科lt;float>(), 4095), 4095);   // replace the values >4095 with 4095 of array input

As you can see i am make the value of inputImage to be with in the range [0,4095]. But, it is obvious that this is not efficient code, becoz i can have a loop running and do the both jobs in once shot. But, that becomes C style rather than C++.

Is there a way to use replace_if and get the both things done in one shot.


float clip(float value) {
    return std::min(std::max(0, value), 4096);
}

int size = m_xDim * m_yDim * m_zDim;
std::transform(inputImage, inputImage + size, inputImage, clip);


You can always use transform, with an appropriate transformer, i.e.:

struct Clipper
{
    int operator()( int in ) const
    {
        return in < 0 ? 0 : 4096 < in ? 4095 : in;
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜