Skip to content
Snippets Groups Projects
Commit a23ac8c9 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

add container transformation function

parent 9e62f5c5
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
#define __ALGORITHM_HEADER_WRAPPER_
 
#include <bits/../algorithm>
#include <functional>
#include "extensions/algorithm.hpp"
 
#endif /* __ALGORITHM_HEADER_WRAPPER_ */
......@@ -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_ */
......@@ -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);
}
......@@ -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_
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment