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

extensions for array

parent 9205bb97
No related branches found
No related tags found
No related merge requests found
#include <extensions/array.hpp>
/*
* 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_ */
#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 );
}
#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_
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