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

[WIP] Adding new constructors to Array.

parent 4d322fa4
Loading
Loading
Loading
Loading
+44 −8
Original line number Diff line number Diff line
@@ -79,6 +79,24 @@ class Array : public Object
             const IndexType& begin = 0,
             const IndexType& size = 0 );

      /**
       * 
       * @param list
       */
      Array( const std::initializer_list< Value >& list );

      /**
       * 
       * @param list
       */
      Array( const std::list< Value >& list );

      /**
       * 
       * @param vector
       */
      Array( const std::vector< Value >& vector );

      /** \brief Returns type of array Value, Device type and the type of Index. */
      static String getType();

@@ -242,27 +260,45 @@ class Array : public Object
      bool operator != ( const ArrayT& array ) const;

      /**
       * \brief Sets the array values.
       * \brief Sets the array elements to given value.
       *
       * Sets all the array values to \e v.
       *
       * \param v Reference to a value.
       */
      void setValue( const Value& v,
                     const Index begin = 0,
                     const Index end = this->getSize() );

      /**
       * \brief Sets the array elements using given lambda function.
       *
       * Sets all the array values to \e v.
       *
       * \param v Reference to a value.
       */
      void setValue( const Value& v );
      template< typename Fuction >
      void setValues( Functions& f,
                      const Index begin = 0,
                      const Index end = this->getSize() );

      /**
       * \brief Checks if there is an element with value \e v in this array.
       *
       * \param v Reference to a value.
       */
      bool containsValue( const Value& v ) const;
      bool containsValue( const Value& v,
                          const Index begin = 0,
                          const Index end = this->getSize() ) const;

      /**
       * \brief Checks if all elements in this array have the same value \e v.
       *
       * \param v Reference to a value.
       */
      bool containsOnlyValue( const Value& v ) const;
      bool containsOnlyValue( const Value& v,
                              const Index begin = 0,
                              const Index end = this->getSize() ) const;

      /**
       * \brief Returns true if non-zero size is set.
@@ -308,10 +344,10 @@ class Array : public Object
      void releaseData() const;

      /** \brief Number of elements in array. */
      mutable Index size;
      mutable Index size = 0;

      /** \brief Pointer to data. */
      mutable Value* data;
      mutable Value* data = nullptr;

      /**
       * \brief Pointer to the originally allocated data.
@@ -322,7 +358,7 @@ class Array : public Object
       * by TNL) are bind then this pointer is zero since no deallocation is
       * necessary.
       */
      mutable Value* allocationPointer;
      mutable Value* allocationPointer = nullptr;

      /**
       * \brief Counter of objects sharing this array or some parts of it.
@@ -330,7 +366,7 @@ class Array : public Object
       * The reference counter is allocated after first sharing of the data between
       * more arrays. This is to avoid unnecessary dynamic memory allocation.
       */
      mutable int* referenceCounter;
      mutable int* referenceCounter = nullptr;
};

template< typename Value, typename Device, typename Index >
+69 −6
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#include <TNL/Assert.h>
#include <TNL/File.h>
#include <TNL/Math.h>
#include <TNL/ParallelFor.h>
#include <TNL/param-types.h>
#include <TNL/Containers/Algorithms/ArrayOperations.h>
#include <TNL/Containers/Algorithms/ArrayIO.h>
@@ -93,6 +94,45 @@ Array( Array< Value, Device, Index >& array,
   }
}

template< typename Value,
          typename Device,
          typename Index >
Array< Value, Device, Index >::
Array( const std::initializer_list< Value >& list )
: size( 0 ),
  data( 0 ),
  allocationPointer( 0 ),
  referenceCounter( 0 )
{
   this->setSize( list.size() );
}

template< typename Value,
          typename Device,
          typename Index >
Array< Value, Device, Index >::
Array( const std::list< Value >& list )
: size( 0 ),
  data( 0 ),
  allocationPointer( 0 ),
  referenceCounter( 0 )
{
   this->setSize( list.size() );
}

template< typename Value,
          typename Device,
          typename Index >
Array< Value, Device, Index >::
Array( const std::vector< Value >& vector )
: size( 0 ),
  data( 0 ),
  allocationPointer( 0 ),
  referenceCounter( 0 )
{
   this->setSize( vector.size() );
}

template< typename Value,
          typename Device,
          typename Index >
@@ -433,10 +473,29 @@ bool Array< Value, Device, Index >::operator != ( const ArrayT& array ) const
template< typename Value,
          typename Device,
          typename Index >
void Array< Value, Device, Index >::setValue( const Value& e )
void Array< Value, Device, Index >::setValue( const Value& e,
                                              const Index begin,
                                              const Index end )
{
   TNL_ASSERT_TRUE( this->getData(), "Attempted to set a value of an empty array." );
   Algorithms::ArrayOperations< Device >::setMemory( this->getData(), e, this->getSize() );
   Algorithms::ArrayOperations< Device >::setMemory( &this->getData()[ begin ], e, end - begin );
}

template< typename Value,
          typename Device,
          typename Index >
   template< typename Function >
void Array< Value, Device, Index >::setValues( Function& f,
                                               const Index begin,
                                               const Index end )
{
   TNL_ASSERT_TRUE( this->getData(), "Attempted to set a value of an empty array." );
   auto evaluate = [=] __cuda_callable__ ( Index i )
   {
      this->data[ i ] = f( i );
   }

   ParallelFor< DeviceType >( begin, end, evaluate );
}

template< typename Value,
@@ -444,9 +503,11 @@ template< typename Value,
          typename Index >
bool
Array< Value, Device, Index >::
containsValue( const Value& v ) const
containsValue( const Value& v,
               const Index begin,
               const Index end ) const
{
   return Algorithms::ArrayOperations< Device >::containsValue( this->data, this->size, v );
   return Algorithms::ArrayOperations< Device >::containsValue( &this->getData()[ begin ], end - begin, v );
}

template< typename Value,
@@ -454,9 +515,11 @@ template< typename Value,
          typename Index >
bool
Array< Value, Device, Index >::
containsOnlyValue( const Value& v ) const
containsOnlyValue( const Value& v,
                   const Index begin,
                   const Index end ) const
{
   return Algorithms::ArrayOperations< Device >::containsOnlyValue( this->data, this->size, v );
   return Algorithms::ArrayOperations< Device >::containsOnlyValue( &this->getData()[ begin ], end - begin, v );
}

template< typename Value,