diff --git a/alib2std/src/algorithm b/alib2std/src/algorithm new file mode 100644 index 0000000000000000000000000000000000000000..08d9642f79e20752051ef48a71d089f5043ee75f --- /dev/null +++ b/alib2std/src/algorithm @@ -0,0 +1,7 @@ +#ifndef __ALGORITHM_HEADER_WRAPPER_ +#define __ALGORITHM_HEADER_WRAPPER_ + +#include <bits/../algorithm> +#include "extensions/algorithm.hpp" + +#endif /* __ALGORITHM_HEADER_WRAPPER_ */ diff --git a/alib2std/src/extensions/algorithm.hpp b/alib2std/src/extensions/algorithm.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b8c6d1e3364c0cfd7548aa45516a2d1fabcc7781 --- /dev/null +++ b/alib2std/src/extensions/algorithm.hpp @@ -0,0 +1,43 @@ +/* + * algorithm.hpp + * + * Created on: May 28, 2015 + * Author: Jan Travnicek + */ + +#ifndef ALGORITHM_HPP_ +#define ALGORITHM_HPP_ + +namespace std { + +template<class InputIt1, class InputIt2> +bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { + while (first2 != last2 && first1 != last1) { + if ( *first2 < *first1 ) { + ++first2; + } else if( *first1 < *first2 ) { + ++first1; + } else { + return false; + } + } + return true; +} + +template<class InputIt1, class InputIt2, class Compare> +bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp) { + while (first2 != last2 && first1 != last1) { + if (comp(*first2, *first1)) { + ++first2; + } else if (comp(*first1, *first2)) { + ++first1; + } else { + return false; + } + } + return true; +} + +} /* namespace std */ + +#endif /* ALGORITHM_HPP_ */ diff --git a/alib2std/test-src/extensions/AlgorithmTest.cpp b/alib2std/test-src/extensions/AlgorithmTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa30eb250c0e9a2e83a1c23ae0f93a43e7c72538 --- /dev/null +++ b/alib2std/test-src/extensions/AlgorithmTest.cpp @@ -0,0 +1,51 @@ +#include "AlgorithmTest.h" +#include <algorithm> + + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AlgorithmTest, "bits" ); +CPPUNIT_TEST_SUITE_REGISTRATION( AlgorithmTest ); + +void AlgorithmTest::setUp() { +} + +void AlgorithmTest::tearDown() { +} + +void AlgorithmTest::testExclude() { + { + int array1[] = {1, 2, 3}; + int array2[] = {2, 3, 4}; + + std::cout << sizeof(array1) / sizeof(int); + + CPPUNIT_ASSERT(std::excludes(array1, array1 + sizeof(array1) / sizeof(int), array2, array2 + sizeof(array2) / sizeof(int)) == false); + } + + { + int array1[] = {1, 3, 5}; + int array2[] = {2, 4, 6, 8}; + + CPPUNIT_ASSERT(std::excludes(array1, array1 + sizeof(array1) / sizeof(int), array2, array2 + sizeof(array2) / sizeof(int)) == true); + CPPUNIT_ASSERT(std::excludes(array2, array2 + sizeof(array2) / sizeof(int), array1, array1 + sizeof(array1) / sizeof(int)) == true); + } + + { + int array1[] = {1, 3, 5, 8}; + int array2[] = {2, 4, 6, 8}; + + CPPUNIT_ASSERT(std::excludes(array1, array1 + sizeof(array1) / sizeof(int), array2, array2 + sizeof(array2) / sizeof(int)) == false); + CPPUNIT_ASSERT(std::excludes(array2, array2 + sizeof(array2) / sizeof(int), array1, array1 + sizeof(array1) / sizeof(int)) == false); + } + + { + int array1[] = {0}; + int array2[] = {2, 4, 6, 8}; + + CPPUNIT_ASSERT(std::excludes(array1, array1 + sizeof(array1) / sizeof(int), array2, array2 + sizeof(array2) / sizeof(int)) == true); + CPPUNIT_ASSERT(std::excludes(array2, array2 + sizeof(array2) / sizeof(int), array1, array1 + sizeof(array1) / sizeof(int)) == true); + + CPPUNIT_ASSERT(std::excludes(array1, array1, array2, array2) == true); + CPPUNIT_ASSERT(std::excludes(array2, array2, array1, array1) == true); + } +} + diff --git a/alib2std/test-src/extensions/AlgorithmTest.h b/alib2std/test-src/extensions/AlgorithmTest.h new file mode 100644 index 0000000000000000000000000000000000000000..45b6f3efcebaa2399816eaf5853fcb10cbc82012 --- /dev/null +++ b/alib2std/test-src/extensions/AlgorithmTest.h @@ -0,0 +1,19 @@ +#ifndef ALGORITHM_TEST_H_ +#define ALGORITHM_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class AlgorithmTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( AlgorithmTest ); + CPPUNIT_TEST( testExclude ); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testExclude(); +}; + +#endif // ALGORITHM_TEST_H_