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

Adding expression templates.

parent e6efd8c6
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
/***************************************************************************
                          ArrayOperations.h  -  description
                          ArrayAssignment.h  -  description
                             -------------------
    begin                : Jul 15, 2013
    copyright            : (C) 2013 by Tomas Oberhuber
    begin                : Apr 4, 2019
    copyright            : (C) 2019 by Tomas Oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

+87 −0
Original line number Diff line number Diff line
/***************************************************************************
                          VectorAssignment.h  -  description
                             -------------------
    begin                : Apr 4, 2019
    copyright            : (C) 2019 by Tomas Oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include<type_traits>
#include<utility>
#include <TNL/Containers/Algorithms/VectorOperations.h>

namespace TNL {
namespace Containers {
namespace Algorithms {

namespace Details {
/**
 * SFINAE for checking if T has getSize method
 */
template< typename T >
class HasSubscriptOperator
{
private:
    typedef char YesType[1];
    typedef char NoType[2];

    template< typename C > static YesType& test( decltype(std::declval< C >()[0]) );
    template< typename C > static NoType& test(...);

public:
    static constexpr bool value = ( sizeof( test< T >(0) ) == sizeof( YesType ) );
};
} // namespace Details

template< typename Vector,
          typename T,
          bool hasSubscriptOperator = Details::HasSubscriptOperator< T >::value >
struct VectorAssignment{};

/**
 * \brief Specialization for assignment with subscript operator
 */
template< typename Vector,
          typename T >
struct VectorAssignment< Vector, T, true >
{
   static void resize( Vector& v, const T& t )
   {
      v.setSize( t.getSize() );
   }

   static void assign( Vector& v, const T& t )
   {
      TNL_ASSERT_EQ( v.getSize(), t.getSize(), "The sizes of the vectors must be equal." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] = t[ i ];
   };
};

/**
 * \brief Specialization for array-value assignment for other types. We assume
 * that T is convertible to Vector::ValueType.
 */
template< typename Vector,
          typename T >
struct VectorAssignment< Vector, T, false >
{
   static void resize( Vector& v, const T& t )
   {
   };

   static void assign( Vector& v, const T& t )
   {
      TNL_ASSERT_GT( v.getSize(), 0, "Cannot assign value to empty vector." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] = t;
   };
};

} // namespace Algorithms
} // namespace Containers
} // namespace TNL
+33 −26
Original line number Diff line number Diff line
@@ -30,6 +30,12 @@ class StaticArray
   typedef int     IndexType;
   enum { size = Size };

   /**
    * \brief Gets size of this array.
    */
   __cuda_callable__
   static constexpr int getSize();

   /**
    * \brief Basic constructor.
    *
@@ -72,11 +78,6 @@ class StaticArray
    */
   static String getType();

   /**
    * \brief Gets size of this array.
    */
   __cuda_callable__
   inline int getSize() const;

   /**
    * \brief Gets all data of this static array.
@@ -188,6 +189,12 @@ class StaticArray< 1, Value >
   typedef int     IndexType;
   enum { size = 1 };

   /**
    * \brief Gets size of this array.
    */
   __cuda_callable__
   static constexpr int getSize();

   /** \brief See StaticArray::StaticArray().*/
   __cuda_callable__
   inline StaticArray();
@@ -210,10 +217,6 @@ class StaticArray< 1, Value >
   /** \brief See StaticArray::getType().*/
   static String getType();

   /** \brief See StaticArray::getSize().*/
   __cuda_callable__
   inline int getSize() const;

   /** \brief See StaticArray::getData().*/
   __cuda_callable__
   inline Value* getData();
@@ -293,6 +296,12 @@ class StaticArray< 2, Value >
   typedef int     IndexType;
   enum { size = 2 };

   /**
    * \brief Gets size of this array.
    */
   __cuda_callable__
   static constexpr int getSize();

   /** \brief See StaticArray::StaticArray().*/
   __cuda_callable__
   inline StaticArray();
@@ -324,10 +333,6 @@ class StaticArray< 2, Value >
   /** \brief See StaticArray::getType().*/
   static String getType();

   /** \brief See StaticArray::getSize().*/
   __cuda_callable__
   inline int getSize() const;

   /** \brief See StaticArray::getData().*/
   __cuda_callable__
   inline Value* getData();
@@ -414,6 +419,12 @@ class StaticArray< 3, Value >
   typedef int     IndexType;
   enum { size = 3 };

   /**
    * \brief Gets size of this array.
    */
   __cuda_callable__
   static constexpr int getSize();

   /** \brief See StaticArray::StaticArray().*/
   __cuda_callable__
   inline StaticArray();
@@ -446,10 +457,6 @@ class StaticArray< 3, Value >
   /** \brief See StaticArray::getType().*/
   static String getType();

   /** \brief See StaticArray::getSize().*/
   __cuda_callable__
   inline int getSize() const;

   /** \brief See StaticArray::getData().*/
   __cuda_callable__
   inline Value* getData();
+8 −8
Original line number Diff line number Diff line
@@ -16,6 +16,13 @@
namespace TNL {
namespace Containers {

template< typename Value >
__cuda_callable__
constexpr int StaticArray< 1, Value >::getSize()
{
   return size;
}

template< typename Value >
__cuda_callable__
inline StaticArray< 1, Value >::StaticArray()
@@ -54,13 +61,6 @@ String StaticArray< 1, Value >::getType()
          String( " >" );
}

template< typename Value >
__cuda_callable__
inline int StaticArray< 1, Value >::getSize() const
{
   return size;
}

template< typename Value >
__cuda_callable__
inline Value* StaticArray< 1, Value >::getData()
+8 −8
Original line number Diff line number Diff line
@@ -17,6 +17,13 @@
namespace TNL {
namespace Containers {

template< typename Value >
__cuda_callable__
constexpr int StaticArray< 2, Value >::getSize()
{
   return size;
}

template< typename Value >
__cuda_callable__
inline StaticArray< 2, Value >::StaticArray()
@@ -66,13 +73,6 @@ String StaticArray< 2, Value >::getType()
          String( " >" );
}

template< typename Value >
__cuda_callable__
inline int StaticArray< 2, Value >::getSize() const
{
   return size;
}

template< typename Value >
__cuda_callable__
inline Value* StaticArray< 2, Value >::getData()
Loading