diff --git a/alib2std/src/algorithm b/alib2std/src/algorithm index 08d9642f79e20752051ef48a71d089f5043ee75f..dcf693aac1e911764774c5347fce3a693b0aac2d 100644 --- a/alib2std/src/algorithm +++ b/alib2std/src/algorithm @@ -2,6 +2,7 @@ #define __ALGORITHM_HEADER_WRAPPER_ #include <bits/../algorithm> +#include <functional> #include "extensions/algorithm.hpp" #endif /* __ALGORITHM_HEADER_WRAPPER_ */ diff --git a/alib2std/src/extensions/algorithm.hpp b/alib2std/src/extensions/algorithm.hpp index 168543815628cef2c718352a39375fd06950a748..43b11b334c7da94522b7dd8e638ac7e3a55b0c4b 100644 --- a/alib2std/src/extensions/algorithm.hpp +++ b/alib2std/src/extensions/algorithm.hpp @@ -29,6 +29,13 @@ bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) return excludes(first1, last1, first2, last2, std::less<decltype(*first1)>()); } +template<class ResType, class InType, template < typename ... > class ContainerType, class Callback > +ContainerType<ResType> transform(const ContainerType<InType> & in, Callback transform) { + ContainerType<ResType> res; + std::transform ( in.begin ( ), in.end ( ), std::inserter ( res, res.begin ( ) ), transform ); + return res; +} + } /* namespace std */ #endif /* ALGORITHM_HPP_ */ diff --git a/alib2std/test-src/extensions/AlgorithmTest.cpp b/alib2std/test-src/extensions/AlgorithmTest.cpp index aa30eb250c0e9a2e83a1c23ae0f93a43e7c72538..7037d6dc073f2e02ebe31955c93c59fa8bde6f9a 100644 --- a/alib2std/test-src/extensions/AlgorithmTest.cpp +++ b/alib2std/test-src/extensions/AlgorithmTest.cpp @@ -49,3 +49,14 @@ void AlgorithmTest::testExclude() { } } +void AlgorithmTest::testTransform() { + std::set<int> in = {1, 2, 3, 4}; + std::set<long> ref = {2, 3, 4, 5}; + +/* std::function<long(const int&)> lamb = [](const int& elem) { return (long) elem + 1; }; + std::set<long> out = std::transform<long>(in, lamb); //*/ + + std::set<long> out = std::transform<long>(in, [](const int& elem) { return (long) elem + 1; }); + + CPPUNIT_ASSERT(ref == out); +} diff --git a/alib2std/test-src/extensions/AlgorithmTest.h b/alib2std/test-src/extensions/AlgorithmTest.h index 45b6f3efcebaa2399816eaf5853fcb10cbc82012..0483ba815718109e632092ed92a11387daa62c81 100644 --- a/alib2std/test-src/extensions/AlgorithmTest.h +++ b/alib2std/test-src/extensions/AlgorithmTest.h @@ -7,6 +7,7 @@ class AlgorithmTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE( AlgorithmTest ); CPPUNIT_TEST( testExclude ); + CPPUNIT_TEST( testTransform ); CPPUNIT_TEST_SUITE_END(); public: @@ -14,6 +15,7 @@ public: void tearDown(); void testExclude(); + void testTransform(); }; #endif // ALGORITHM_TEST_H_