Commit 1324d90c authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

Added min and max for static vectors.

parent c5dc4266
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@

#pragma once

#include <TNL/Math.h>

namespace TNL {
   namespace Containers {
      namespace Expressions {
@@ -53,6 +55,27 @@ struct Division
      return a / b;
   }
};

template< typename T1, typename T2 >
struct Min
{
   __cuda_callable__
   static auto evaluate( const T1& a, const T2& b ) -> decltype( TNL::min( a , b ) )
   {
      return TNL::min( a, b );
   }
};

template< typename T1, typename T2 >
struct Max
{
   __cuda_callable__
   static auto evaluate( const T1& a, const T2& b ) -> decltype( TNL::max( a, b ) )
   {
      return TNL::max( a, b );
   }
};

      } //namespace Expressions
   } // namespace Containers
} // namespace TNL
 No newline at end of file
+52 −0
Original line number Diff line number Diff line
@@ -123,6 +123,58 @@ operator/( const StaticVector< Size, Real1 >& a, const StaticVector< Size, Real2
   return Expressions::BinaryExpressionTemplate< StaticVector< Size, Real1 >, StaticVector< Size, Real2 >, Expressions::Division >( a, b );
}

////
// Min
template< int Size, typename Real, typename ET >
__cuda_callable__
const Expressions::BinaryExpressionTemplate< StaticVector< Size, Real >, ET, Expressions::Min >
min( const StaticVector< Size, Real >& a, const ET& b )
{
   return Expressions::BinaryExpressionTemplate< StaticVector< Size, Real >, ET, Expressions::Min >( a, b );
}

template< typename ET, int Size, typename Real >
__cuda_callable__
const Expressions::BinaryExpressionTemplate< ET, StaticVector< Size, Real >, Expressions::Min >
min( const ET& a, const StaticVector< Size, Real >& b )
{
   return Expressions::BinaryExpressionTemplate< ET, StaticVector< Size, Real >, Expressions::Min >( a, b );
}

template< int Size, typename Real1, typename Real2 >
__cuda_callable__
const Expressions::BinaryExpressionTemplate< StaticVector< Size, Real1 >, StaticVector< Size, Real2 >, Expressions::Min >
min( const StaticVector< Size, Real1 >& a, const StaticVector< Size, Real2 >& b )
{
   return Expressions::BinaryExpressionTemplate< StaticVector< Size, Real1 >, StaticVector< Size, Real2 >, Expressions::Min >( a, b );
}

////
// Max
template< int Size, typename Real, typename ET >
__cuda_callable__
const Expressions::BinaryExpressionTemplate< StaticVector< Size, Real >, ET, Expressions::Max >
max( const StaticVector< Size, Real >& a, const ET& b )
{
   return Expressions::BinaryExpressionTemplate< StaticVector< Size, Real >, ET, Expressions::Max >( a, b );
}

template< typename ET, int Size, typename Real >
__cuda_callable__
const Expressions::BinaryExpressionTemplate< ET, StaticVector< Size, Real >, Expressions::Max >
max( const ET& a, const StaticVector< Size, Real >& b )
{
   return Expressions::BinaryExpressionTemplate< ET, StaticVector< Size, Real >, Expressions::Max >( a, b );
}

template< int Size, typename Real1, typename Real2 >
__cuda_callable__
const Expressions::BinaryExpressionTemplate< StaticVector< Size, Real1 >, StaticVector< Size, Real2 >, Expressions::Max >
max( const StaticVector< Size, Real1 >& a, const StaticVector< Size, Real2 >& b )
{
   return Expressions::BinaryExpressionTemplate< StaticVector< Size, Real1 >, StaticVector< Size, Real2 >, Expressions::Max >( a, b );
}

////
// Comparison operations - operator ==
template< int Size, typename Real, typename ET >
+19 −0
Original line number Diff line number Diff line
@@ -123,6 +123,25 @@ TYPED_TEST( StaticVectorTest, operators )
   EXPECT_EQ( ScalarProduct( u1, u2 ), 4 * size );
}

TYPED_TEST( StaticVectorTest, MinMax )
{
   using VectorType = typename TestFixture::VectorType;
   constexpr int size = VectorType::size;

   VectorType u1( 1 ), u2( 2 ), u3( 3 ), u4, u_min, u_max;
   for( int i = 0; i < size; i++ )
   {
      u4[ i ] = i;
      u_min[ i ] = TNL::min( i, 3 );
      u_max[ i ] = TNL::max( i, 3 );
   }

   EXPECT_TRUE( min( u1, u2 ) ==  u1 );
   EXPECT_TRUE( max( u1, u2 ) ==  u2 );
   EXPECT_TRUE( min( u3, u4 ) == u_min );
   EXPECT_TRUE( max( u3, u4 ) == u_max );
}

TYPED_TEST( StaticVectorTest, comparisons )
{
   using VectorType = typename TestFixture::VectorType;