Hereby I would like to draw your attention to a class (template) that I just submitted: ENH: Added NeighborhoodRange for efficient modern C++ style iteration. It aims to support modern C++ style iteration on a pixel neighborhood, in an efficient way.
Features:
- Can be used in C++11 range-based for loops, for example:
for (PixelType pixel : neighborhoodRange) … - Can be used to easily construct an std container of pixels, for example:
std::vector< PixelType > pixels(neighborhoodRange.begin(), neighborhoodRange.end()) - Can be passed directly to std algorithms (std::for_each, std::copy, etc.)
 - Can also be used with std C++ < numeric> functions, e.g., std::inner_product
 - Supports bidirectional iteration (both ++it and --it)
 
Performance related properties:
- No dynamic memory allocation (not even during construction)
 - No virtual functions (even its destructors are non-virtual)
 - No ‘mutable’ data members, making it easier to write thread-safe code.
 
itk::NeighborhoodRange would allow a significantly performance improvement of GaussianDerivativeImageFunction, as I observed from rerunning the test from
Removal of `virtual` keywords from ConstNeighborhoodIterator
On my Windows machine, I observed a reduction of runtime duration of more than 25%.
The patch includes unit tests (Core/Common/test/itkNeighborhoodRangeTest.cxx) which show various relevant use cases.
Your review and comments are appreciated: http://review.source.kitware.com/#/c/23326
. Instead of allowing the user to specify the neighborhood radius (typically Size<2> or Size<3>), what about allowing to specify directly the indices, relative to the center index? For example, a user might specify a cross shaped neigborhood by relative indices {{0, 0}, {-1, -1}, {1, -1}, {1, 1}, {-1, 1}}.
 The std::vector of relative indices should be owned by the user, not by the NeighborhoodRange object. It would kill performance if NeighborhoodRange would have to store a copy of the std::vector. (Seriously!) Instead, NeighborhoodRange would just have a reference (or a pointer) to the std::vector.

 What do you think?
 Especially iterator::operator*(), which now returns a (possibly non-const) reference to the current pixel: