/* * %injeqt copyright begin% * Copyright 2014 RafaƂ Malinowski (rafal.przemyslaw.malinowski@gmail.com) * %injeqt copyright end% * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #pragma once #include "internal.h" #include #include #include /** * @file * @brief Helper functions for containers. */ namespace injeqt { namespace internal { /** * @brief Functional interface to std::transform. * @tparam S type of source items * @tparam T type of result items * @param source source data * @param f transforming function */ template inline std::vector transform(const std::vector &source, std::function f) { auto result = std::vector{}; std::transform(std::begin(source), std::end(source), std::back_inserter(result), f); return result; } /** * @brief Extract data from multiple vectors into one vector. * @tparam S type of source items * @tparam T type of result items * @param f function returning vector for each item in source */ template inline std::vector extract(const std::vector &sources, std::function(const S &)> f) { auto result = std::vector{}; for (auto &&source : sources) { auto v = f(source); std::copy(std::begin(v), std::end(v), std::back_inserter(result)); } return result; } }}