diff --git a/alib2std/src/alib/array b/alib2std/src/alib/array new file mode 100644 index 0000000000000000000000000000000000000000..f79b76c9098a48c458d8ff88f58e4614e28e0485 --- /dev/null +++ b/alib2std/src/alib/array @@ -0,0 +1 @@ +#include <extensions/array.hpp> diff --git a/alib2std/src/extensions/array.hpp b/alib2std/src/extensions/array.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c5ac15073a005f0ff7b4a92cb73ca8f5adce1241 --- /dev/null +++ b/alib2std/src/extensions/array.hpp @@ -0,0 +1,96 @@ +/* + * array.hpp + * + * Created on: Apr 1, 2013 + * Author: Jan Travnicek + */ + +#ifndef __ARRAY_HPP_ +#define __ARRAY_HPP_ + +#include <array> +#include <experimental/array> + +#include <ostream> +#include <sstream> +#include <string> + +#include "compare.hpp" + +namespace ext { + +template < class T, std::size_t N > +class array : public std::array < T, N > { +public: +#ifdef __clang__ + using std::array < T, N >::array; + using std::array < T, N >::operator =; +#else + array ( ) noexcept : std::array < T, N > ( ) { + } + + array ( const array & other ) = default; + + array ( array && other ) = default; + + using std::array < T, N >::array; + + array & operator = ( array && other ) = default; + + array & operator = ( const array & other ) = default; + + using std::array < T, N >::operator =; +#endif + + template < class ... Types > + array ( Types && ... args ) : std::array < T, N > ( std::experimental::make_array ( std::forward < Types > ( args ) ... ) ) { + } +}; + +template< class T, std::size_t N > +std::ostream& operator<<(std::ostream& out, const ext::array < T, N > & array) { + out << "["; + + bool first = true; + for(const T& item : array) { + if(!first) out << ", "; + first = false; + out << item; + } + + out << "]"; + return out; +} + +template< class T, std::size_t N > +struct compare < ext::array < T, N > > { + int operator ( ) ( const ext::array < T, N > & first, const ext::array < T, N > & second) const { + static compare<typename std::decay < T >::type > comp; + for(auto iterF = first.begin(), iterS = second.begin(); iterF != first.end(); ++iterF, ++iterS) { + int res = comp(*iterF, *iterS); + if(res != 0) return res; + } + return 0; + } +}; + +template< class T, std::size_t N > +std::string to_string ( const ext::array < T, N > & value ) { + std::stringstream ss; + ss << value; + return ss.str(); +} + +template < typename Base, typename ... Types > +constexpr array < typename std::remove_reference < Base >::type, sizeof ... ( Types ) + 1 > make_array ( Base && first, Types && ... other ) { + return array < typename std::remove_reference < Base >::type, sizeof ... ( Types ) + 1 > ( std::forward < Base > ( first ), std::forward < Types > ( other ) ... ); +} + +template < typename Base > +constexpr array < typename std::remove_reference < Base >::type, 0 > make_array ( ) { + return array < typename std::remove_reference < Base >::type, 0 > ( ); +} + +} /* namespace ext */ + +#endif /* __ARRAY_HPP_ */ diff --git a/alib2std/test-src/extensions/ArrayTest.cpp b/alib2std/test-src/extensions/ArrayTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbd9252f57002b27a2c8cadc45c13c4a617fd406 --- /dev/null +++ b/alib2std/test-src/extensions/ArrayTest.cpp @@ -0,0 +1,20 @@ +#include "ArrayTest.h" +#include <alib/map> +#include <alib/algorithm> +#include <alib/array> + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ArrayTest, "bits" ); +CPPUNIT_TEST_SUITE_REGISTRATION( ArrayTest ); + +void ArrayTest::setUp() { +} + +void ArrayTest::tearDown() { +} + +void ArrayTest::basicTest() { + ext::array < int, 3 > a = ext::make_array ( 1, 2, 3 ); + + CPPUNIT_ASSERT ( a [ 0 ] == 1 ); +} + diff --git a/alib2std/test-src/extensions/ArrayTest.h b/alib2std/test-src/extensions/ArrayTest.h new file mode 100644 index 0000000000000000000000000000000000000000..0a5b91617390771b4f1d29ba20c341125f084f07 --- /dev/null +++ b/alib2std/test-src/extensions/ArrayTest.h @@ -0,0 +1,19 @@ +#ifndef ARRAY_TEST_H_ +#define ARRAY_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class ArrayTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( ArrayTest ); + CPPUNIT_TEST( basicTest ); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void basicTest(); +}; + +#endif // ARRAY_TEST_H_