Commit b91b41d8 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber

Implemented StaticArray::operator= accepting both arrays and single...

Implemented StaticArray::operator= accepting both arrays and single StaticArray:::ValueType compatible type.
parent 9cf16a91
......@@ -24,7 +24,7 @@ namespace Algorithms {
template< typename LeftValue, typename RightValue = LeftValue >
struct assignArrayFunctor
{
__cuda_callable__ void operator()( int i, LeftValue* data, const RightValue* v ) const
__cuda_callable__ void operator()( int i, LeftValue* data, const RightValue& v ) const
{
data[ i ] = v[ i ];
}
......@@ -33,7 +33,7 @@ namespace Algorithms {
template< typename LeftValue, typename RightValue = LeftValue >
struct assignValueFunctor
{
__cuda_callable__ void operator()( int i, LeftValue* data, const RightValue v ) const
__cuda_callable__ void operator()( int i, LeftValue& data, const RightValue& v ) const
{
data[ i ] = v;
}
......@@ -52,10 +52,11 @@ template< typename StaticArray,
typename T >
struct StaticArrayAssignment< StaticArray, T, true >
{
__cuda_callable__
static void assign( StaticArray& a, const T& t )
{
static_assert( StaticArray::getSize() == T::getSize(), "Cannot assign static arrays with different size." );
StaticFor< 0, StaticArray::getSize() >::exec( detail::assignArrayFunctor< StaticArray, T >{}, a, t );
StaticFor< 0, StaticArray::getSize() >::exec( detail::assignArrayFunctor< typename StaticArray::ValueType, T >{}, a.getData(), t );
}
};
......@@ -67,6 +68,7 @@ template< typename StaticArray,
typename T >
struct StaticArrayAssignment< StaticArray, T, false >
{
__cuda_callable__
static void assign( StaticArray& a, const T& t )
{
StaticFor< 0, StaticArray::getSize() >::exec( detail::assignValueFunctor< StaticArray, T >{}, a, t );
......
......@@ -103,7 +103,6 @@ public:
*/
static String getType();
/**
* \brief Gets all data of this static array.
*/
......@@ -157,17 +156,24 @@ public:
const Value& z() const;
/**
* \brief Assigns another static \e array to this array, replacing its current contents.
* \brief Assigns another static \e array to this array.
*/
__cuda_callable__
StaticArray< Size, Value >& operator=( const StaticArray< Size, Value >& array );
/**
* \brief Assigns another static \e array to this array, replacing its current contents.
* \brief Assigns an object \e t of type \e T.
*
* T can be:
*
* 1. Static linear container implementing operator[] and having the same size.
* In this case, \e t is copied to this array elementwise.
* 2. An object that can be converted to \e Value type. In this case all elements
* are set to \e t.
*/
template< typename Array >
template< typename T >
__cuda_callable__
StaticArray< Size, Value >& operator=( const Array& array );
StaticArray< Size, Value >& operator=( const T& t );
/**
* \brief This function checks whether this static array is equal to another \e array.
......
......@@ -13,6 +13,7 @@
#include <TNL/param-types.h>
#include <TNL/Math.h>
#include <TNL/Containers/StaticArray.h>
#include <TNL/Containers/Algorithms/StaticArrayAssignment.h>
#include <TNL/StaticFor.h>
namespace TNL {
......@@ -263,11 +264,11 @@ StaticArray< Size, Value >& StaticArray< Size, Value >::operator=( const StaticA
}
template< int Size, typename Value >
template< typename Array >
template< typename T >
__cuda_callable__
StaticArray< Size, Value >& StaticArray< Size, Value >::operator=( const Array& array )
StaticArray< Size, Value >& StaticArray< Size, Value >::operator=( const T& v )
{
StaticFor< 0, Size >::exec( detail::assignArrayFunctor< Value, typename Array::ValueType >{}, data, array.getData() );
Algorithms::StaticArrayAssignment< StaticArray, T >::assign( *this, v );
return *this;
}
......
......@@ -216,6 +216,15 @@ TYPED_TEST( StaticArrayTest, AssignmentOperator )
StaticArray< size, char > u4( 127 );
u3 = u4;
EXPECT_TRUE( u3 == u4 );
// assignment of number
u3 = 0.0;
for( int i = 0; i < size; i++ )
u3[ i ] == 0.0;
u3 = 1.0;
for( int i = 0; i < size; i++ )
u3[ i ] == 1.0;
}
TYPED_TEST( StaticArrayTest, setValue )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment