[range] Proposal to add make_view

Hi, I propose to add a simple helper function to create an iterator range of boost::transform_iterators. This would effectively create a view over a different range or a container. Currently this is possible to do with considerable code duplication: make_iterator_range( make_transform_iterator(vec.begin(), fun), make_transform_iterator(vec.end(), fun)); Here "fun" could be a rather complex lambda or bind expression. What I propose is to wrap these calls into a simple generator make_view. The above code would look much better: make_view(vec.begin(), vec.end(), fun); or make_view(vec, fun); I've attached a sample implementation of the make_view function. Do you think this could be useful? #ifndef BOOST_RANGE_VIEW_HPP_INCLUDED_ #define BOOST_RANGE_VIEW_HPP_INCLUDED_ #include <boost/iterator/transform_iterator.hpp> #include <boost/range/iterator_range.hpp> #include <boost/range/begin.hpp> #include <boost/range/end.hpp> #include <boost/range/iterator.hpp> namespace boost { template< typename IteratorT, typename FunT > inline iterator_range< transform_iterator< FunT, IteratorT >
make_view(IteratorT const& begin, IteratorT const& end, FunT const& fun) { return make_iterator_range( make_transform_iterator(begin, fun), make_transform_iterator(end, fun)); }
template< typename RangeT, typename FunT > inline iterator_range< transform_iterator< FunT, typename range_iterator< const RangeT >::type >
make_view(RangeT const& range, FunT const& fun) { return make_view(begin(range), end(range), fun); }
} // namespace boost #endif // BOOST_RANGE_VIEW_HPP_INCLUDED_

AMDG Andrey Semashev wrote:
Hi,
I propose to add a simple helper function to create an iterator range of boost::transform_iterators. This would effectively create a view over a different range or a container.
Currently this is possible to do with considerable code duplication:
make_iterator_range( make_transform_iterator(vec.begin(), fun), make_transform_iterator(vec.end(), fun));
Here "fun" could be a rather complex lambda or bind expression. What I propose is to wrap these calls into a simple generator make_view. The above code would look much better:
make_view(vec.begin(), vec.end(), fun);
or
make_view(vec, fun);
I've attached a sample implementation of the make_view function. Do you think this could be useful?
I want this utility on a regular basis. In Christ, Steven Watanabe

On Wed, Apr 2, 2008 at 12:23 AM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
Andrey Semashev wrote:
Hi,
I propose to add a simple helper function to create an iterator range of boost::transform_iterators. This would effectively create a view over a different range or a container.
Currently this is possible to do with considerable code duplication:
make_iterator_range( make_transform_iterator(vec.begin(), fun), make_transform_iterator(vec.end(), fun));
Here "fun" could be a rather complex lambda or bind expression. What I propose is to wrap these calls into a simple generator make_view. The above code would look much better:
make_view(vec.begin(), vec.end(), fun);
or
make_view(vec, fun);
I've attached a sample implementation of the make_view function. Do you think this could be useful?
I want this utility on a regular basis.
I like this too. Should this be in put in Boost.Range? Or should this be a Utility? -- Dean Michael C. Berris Software Engineer, Friendster, Inc. [http://blog.cplusplus-soup.com] [mikhailberis@gmail.com] [+63 928 7291459] [+1 408 4049523]

Dean Michael Berris wrote:
On Wed, Apr 2, 2008 at 12:23 AM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
Andrey Semashev wrote:
Hi,
I propose to add a simple helper function to create an iterator range of boost::transform_iterators. This would effectively create a view over a different range or a container.
Currently this is possible to do with considerable code duplication:
make_iterator_range( make_transform_iterator(vec.begin(), fun), make_transform_iterator(vec.end(), fun));
Here "fun" could be a rather complex lambda or bind expression. What I propose is to wrap these calls into a simple generator make_view. The above code would look much better:
make_view(vec.begin(), vec.end(), fun);
or
make_view(vec, fun);
I've attached a sample implementation of the make_view function. Do you think this could be useful? I want this utility on a regular basis.
I like this too. Should this be in put in Boost.Range? Or should this be a Utility?
Added Ticket #1756. I think, Boost.Range is a better place because the view is an iterator range after all. Moreover, as I pointed out in the ticket, a more elaborate implementation could optimize function object duplication that we get with transform_iterator, thus using only Boost.Range components and making a pure extension of the library. However, I have no strong feelings about this.
participants (4)
-
Andrey Semashev
-
Dean Michael Berris
-
shunsuke
-
Steven Watanabe