Skip to content

Commit

Permalink
Merge pull request #67 from lsst/tickets/DM-6091
Browse files Browse the repository at this point in the history
Replace boost::lambda with C++11 lambda
  • Loading branch information
Pim Schellart authored and Pim Schellart committed May 25, 2016
2 parents a222f6d + c2b68ad commit 0f8c499
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 51 deletions.
52 changes: 22 additions & 30 deletions src/image/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* LSST Data Management System
* Copyright 2008, 2009, 2010 LSST Corporation.
* Copyright 2008-2016 AURA/LSST.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
Expand Down Expand Up @@ -30,7 +30,6 @@
#include "boost/mpl/vector.hpp"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
#include "boost/lambda/lambda.hpp"
#pragma clang diagnostic pop
#include "boost/format.hpp"
#include "boost/filesystem/path.hpp"
Expand Down Expand Up @@ -639,33 +638,19 @@ void image::swap(Image<PixelT>& a, Image<PixelT>& b) {
}

/************************************************************************************************************/
//
// N.b. We could use the STL, but I find boost::lambda clearer, and more easily extended
// to e.g. setting random numbers
// transform_pixels(_gilView, _gilView, lambda::ret<PixelT>(lambda::_1 + rhs));
// is equivalent to
// transform_pixels(_gilView, _gilView, std::bind2nd(std::plus<PixelT>(), rhs));
//
namespace bl = boost::lambda;

// dstn: being a bear of little brain when it comes to templated lambdas, I found it easier to
// write out this sqrt function which does the casts explicitly.
template<typename PixelT>
static PixelT mysqrt(PixelT x) {
return static_cast<PixelT>(std::sqrt(x));
}

// In-place, per-pixel, sqrt().
template<typename PixelT>
void image::Image<PixelT>::sqrt() {
transform_pixels(_getRawView(), _getRawView(),
boost::bind(mysqrt<PixelT>, bl::_1));
[](PixelT const& l) -> PixelT { return static_cast<PixelT>(std::sqrt(l)); });
}

/// Add scalar rhs to lhs
template<typename PixelT>
void image::Image<PixelT>::operator+=(PixelT const rhs) {
transform_pixels(_getRawView(), _getRawView(), bl::ret<PixelT>(bl::_1 + rhs));
transform_pixels(_getRawView(), _getRawView(),
[&rhs](PixelT const& l) -> PixelT { return l + rhs; });
}

/// Add Image rhs to lhs
Expand All @@ -676,7 +661,8 @@ void image::Image<PixelT>::operator+=(Image<PixelT> const& rhs) {
(boost::format("Images are of different size, %dx%d v %dx%d") %
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()).str());
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(), bl::ret<PixelT>(bl::_1 + bl::_2));
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
[](PixelT const& l, PixelT const& r) -> PixelT { return l + r; });
}

/**
Expand Down Expand Up @@ -705,13 +691,14 @@ void image::Image<PixelT>::scaledPlus(double const c, Image<PixelT> const& rhs)
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()).str());
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
bl::ret<PixelT>(bl::_1 + bl::ret<PixelT>(c*bl::_2)));
[&c](PixelT const& l, PixelT const& r) -> PixelT { return l + static_cast<PixelT>(c*r); });
}

/// Subtract scalar rhs from lhs
template<typename PixelT>
void image::Image<PixelT>::operator-=(PixelT const rhs) {
transform_pixels(_getRawView(), _getRawView(), bl::ret<PixelT>(bl::_1 - rhs));
transform_pixels(_getRawView(), _getRawView(),
[&rhs](PixelT const& l) -> PixelT { return l - rhs; });
}

/// Subtract Image rhs from lhs
Expand All @@ -722,7 +709,8 @@ void image::Image<PixelT>::operator-=(Image<PixelT> const& rhs) {
(boost::format("Images are of different size, %dx%d v %dx%d") %
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()).str());
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(), bl::ret<PixelT>(bl::_1 - bl::_2));
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
[](PixelT const& l, PixelT const& r) -> PixelT { return l - r; });
}

/// Subtract Image c*rhs from lhs
Expand All @@ -734,7 +722,7 @@ void image::Image<PixelT>::scaledMinus(double const c, Image<PixelT> const& rhs)
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()).str());
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
bl::ret<PixelT>(bl::_1 - bl::ret<PixelT>(c*bl::_2)));
[&c](PixelT const& l, PixelT const& r) -> PixelT { return l - static_cast<PixelT>(c*r); });
}

/**
Expand All @@ -757,7 +745,8 @@ void image::Image<PixelT>::operator-=(
/// Multiply lhs by scalar rhs
template<typename PixelT>
void image::Image<PixelT>::operator*=(PixelT const rhs) {
transform_pixels(_getRawView(), _getRawView(), bl::ret<PixelT>(bl::_1 * rhs));
transform_pixels(_getRawView(), _getRawView(),
[&rhs](PixelT const& l) -> PixelT { return l*rhs; });
}

/// Multiply lhs by Image rhs (i.e. %pixel-by-%pixel multiplication)
Expand All @@ -768,7 +757,8 @@ void image::Image<PixelT>::operator*=(Image<PixelT> const& rhs) {
(boost::format("Images are of different size, %dx%d v %dx%d") %
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()).str());
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(), bl::ret<PixelT>(bl::_1 * bl::_2));
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
[](PixelT const& l, PixelT const& r) -> PixelT { return l*r; });
}

/// Multiply lhs by Image c*rhs (i.e. %pixel-by-%pixel multiplication)
Expand All @@ -780,15 +770,16 @@ void image::Image<PixelT>::scaledMultiplies(double const c, Image<PixelT> const&
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()).str());
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
bl::ret<PixelT>(bl::_1 * bl::ret<PixelT>(c*bl::_2)));
[&c](PixelT const& l, PixelT const& r) -> PixelT { return l*static_cast<PixelT>(c*r); });
}

/// Divide lhs by scalar rhs
///
/// \note Floating point types implement this by multiplying by the 1/rhs
template<typename PixelT>
void image::Image<PixelT>::operator/=(PixelT const rhs) {
transform_pixels(_getRawView(), _getRawView(), bl::ret<PixelT>(bl::_1 / rhs));
transform_pixels(_getRawView(), _getRawView(),
[&rhs](PixelT const& l) -> PixelT { return l/rhs; });
}
//
// Specialize float and double for efficiency
Expand All @@ -815,7 +806,8 @@ void image::Image<PixelT>::operator/=(Image<PixelT> const& rhs) {
(boost::format("Images are of different size, %dx%d v %dx%d") %
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()).str());
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(), bl::ret<PixelT>(bl::_1 / bl::_2));
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
[](PixelT const& l, PixelT const& r) -> PixelT { return l/r; });
}

/// Divide lhs by Image c*rhs (i.e. %pixel-by-%pixel division)
Expand All @@ -827,7 +819,7 @@ void image::Image<PixelT>::scaledDivides(double const c, Image<PixelT> const& rh
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()).str());
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
bl::ret<PixelT>(bl::_1 / bl::ret<PixelT>(c*bl::_2)));
[&c](PixelT const& l, PixelT const& r) -> PixelT { return l/static_cast<PixelT>(c*r); });
}

/************************************************************************************************************/
Expand Down
31 changes: 13 additions & 18 deletions src/image/Mask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* LSST Data Management System
* Copyright 2008, 2009, 2010 LSST Corporation.
* Copyright 2008-2016 AURA/LSST.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
Expand Down Expand Up @@ -47,7 +47,6 @@

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
#include "boost/lambda/lambda.hpp"
#pragma clang diagnostic pop
#include "boost/format.hpp"
#include "boost/filesystem/path.hpp"
Expand Down Expand Up @@ -1058,22 +1057,13 @@ void Mask<MaskPixelT>::checkMaskDictionaries(Mask<MaskPixelT> const &other) {
}
}

/************************************************************************************************************/
//
// N.b. We could use the STL, but I find boost::lambda clearer, and more easily extended
// to e.g. setting random numbers
// transform_pixels(_getRawView(), _getRawView(), lambda::ret<PixelT>(lambda::_1 + val));
// is equivalent to
// transform_pixels(_getRawView(), _getRawView(), std::bind2nd(std::plus<PixelT>(), val));
//
namespace bl = boost::lambda;

/**
* \brief OR a bitmask into a Mask
*/
template<typename MaskPixelT>
void Mask<MaskPixelT>::operator|=(MaskPixelT const val) {
transform_pixels(_getRawView(), _getRawView(), bl::ret<MaskPixelT>(bl::_1 | val));
transform_pixels(_getRawView(), _getRawView(),
[&val](MaskPixelT const& l) -> MaskPixelT { return l | val; });
}

/**
Expand All @@ -1088,15 +1078,17 @@ void Mask<MaskPixelT>::operator|=(Mask const &rhs) {
str(boost::format("Images are of different size, %dx%d v %dx%d") %
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()));
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(), bl::ret<MaskPixelT>(bl::_1 | bl::_2));
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
[](MaskPixelT const& l, MaskPixelT const& r) -> MaskPixelT { return l | r; });
}

/**
* \brief AND a bitmask into a Mask
*/
template<typename MaskPixelT>
void Mask<MaskPixelT>::operator&=(MaskPixelT const val) {
transform_pixels(_getRawView(), _getRawView(), bl::ret<MaskPixelT>(bl::_1 & val));
transform_pixels(_getRawView(), _getRawView(),
[&val](MaskPixelT const& l) { return l & val; });
}

/**
Expand All @@ -1111,15 +1103,17 @@ void Mask<MaskPixelT>::operator&=(Mask const &rhs) {
str(boost::format("Images are of different size, %dx%d v %dx%d") %
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()));
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(), bl::ret<MaskPixelT>(bl::_1 & bl::_2));
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
[](MaskPixelT const& l, MaskPixelT const& r) -> MaskPixelT { return l & r; });
}

/**
* \brief XOR a bitmask into a Mask
*/
template<typename MaskPixelT>
void Mask<MaskPixelT>::operator^=(MaskPixelT const val) {
transform_pixels(_getRawView(), _getRawView(), bl::ret<MaskPixelT>(bl::_1 ^ val));
transform_pixels(_getRawView(), _getRawView(),
[&val](MaskPixelT const& l) -> MaskPixelT { return l ^ val; });
}

/**
Expand All @@ -1134,7 +1128,8 @@ void Mask<MaskPixelT>::operator^=(Mask const &rhs) {
str(boost::format("Images are of different size, %dx%d v %dx%d") %
this->getWidth() % this->getHeight() % rhs.getWidth() % rhs.getHeight()));
}
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(), bl::ret<MaskPixelT>(bl::_1 ^ bl::_2));
transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
[](MaskPixelT const& l, MaskPixelT const& r) -> MaskPixelT { return l ^ r; });
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/image/MaskedImage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* LSST Data Management System
* Copyright 2008, 2009, 2010 LSST Corporation.
* Copyright 2008-2016 AURA/LSST.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
Expand Down Expand Up @@ -30,7 +30,6 @@
#include <sys/stat.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
#include "boost/lambda/lambda.hpp"
#pragma clang diagnostic pop
#include "boost/regex.hpp"
#include "boost/filesystem/path.hpp"
Expand All @@ -42,7 +41,6 @@
#include "lsst/afw/image/fits/fits_io.h"
#include "lsst/afw/fits.h"

namespace bl = boost::lambda;
namespace image = lsst::afw::image;

/** Constructors
Expand Down

0 comments on commit 0f8c499

Please sign in to comment.