2015-10-05 00:33:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <type_traits>
|
2017-06-13 02:30:24 +00:00
|
|
|
#include <boost/iterator/transform_iterator.hpp>
|
|
|
|
|
2015-10-05 00:33:43 +00:00
|
|
|
|
|
|
|
namespace ext
|
|
|
|
{
|
2017-06-06 17:10:04 +00:00
|
|
|
/// \brief Strip type off top level reference and cv-qualifiers thus allowing storage in containers
|
|
|
|
template <typename T>
|
|
|
|
using unqualified_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
|
|
|
|
|
|
|
/** \brief Returns collection of the same container-type as the input collection,
|
2017-06-13 02:30:24 +00:00
|
|
|
* with each element transformed by the application of `mapper`.
|
|
|
|
*/
|
2017-06-06 17:10:04 +00:00
|
|
|
template <template <typename...> class Collection, typename... Params, typename Mapper>
|
|
|
|
auto map(const Collection<Params...> & collection, const Mapper mapper)
|
|
|
|
{
|
|
|
|
using value_type = unqualified_t<decltype(mapper(*std::begin(collection)))>;
|
|
|
|
|
2017-06-13 02:30:24 +00:00
|
|
|
return Collection<value_type>(
|
|
|
|
boost::make_transform_iterator(std::begin(collection), mapper),
|
|
|
|
boost::make_transform_iterator(std::end(collection), mapper));
|
2018-08-10 04:02:56 +00:00
|
|
|
}
|
2017-06-06 17:10:04 +00:00
|
|
|
|
|
|
|
/** \brief Returns collection of specified container-type,
|
2017-06-13 02:30:24 +00:00
|
|
|
* with each element transformed by the application of `mapper`.
|
|
|
|
* Allows conversion between different container-types, e.g. std::vector to std::list
|
|
|
|
*/
|
2017-06-06 17:10:04 +00:00
|
|
|
template <template <typename...> class ResultCollection, typename Collection, typename Mapper>
|
|
|
|
auto map(const Collection & collection, const Mapper mapper)
|
|
|
|
{
|
|
|
|
using value_type = unqualified_t<decltype(mapper(*std::begin(collection)))>;
|
|
|
|
|
2017-06-13 02:30:24 +00:00
|
|
|
return ResultCollection<value_type>(
|
|
|
|
boost::make_transform_iterator(std::begin(collection), mapper),
|
|
|
|
boost::make_transform_iterator(std::end(collection), mapper));
|
2018-08-10 04:02:56 +00:00
|
|
|
}
|
2017-06-06 17:10:04 +00:00
|
|
|
|
|
|
|
/** \brief Returns collection of specified type,
|
2017-06-13 02:30:24 +00:00
|
|
|
* with each element transformed by the application of `mapper`.
|
|
|
|
* Allows leveraging implicit conversion between the result of applying `mapper` and R::value_type.
|
|
|
|
*/
|
2017-06-06 17:10:04 +00:00
|
|
|
template <typename ResultCollection, typename Collection, typename Mapper>
|
|
|
|
auto map(const Collection & collection, const Mapper mapper)
|
|
|
|
{
|
2017-06-13 02:30:24 +00:00
|
|
|
return ResultCollection(
|
|
|
|
boost::make_transform_iterator(std::begin(collection), mapper),
|
|
|
|
boost::make_transform_iterator(std::end(collection), mapper));
|
2017-06-06 17:10:04 +00:00
|
|
|
}
|
2015-10-05 00:33:43 +00:00
|
|
|
}
|