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

Changed all Array constructors to make deep copy.

Restored Array/ArrayView save and load with file name.
parent 2f43de15
Loading
Loading
Loading
Loading
+19 −17
Original line number Diff line number Diff line
@@ -90,16 +90,9 @@ class Array
      /**
       * \brief Constructor with data pointer and size.
       *
       * In this case, the Array just encapsulates the pointer \e data. No
       * deallocation is done in destructor.
       *
       * This behavior of the Array is obsolete and \ref ArrayView should be used
       * instead.
       *
       * \param data Pointer to data.
       * \param size Number of array elements.
       * \param size Number of array elements to be copied to the array.
       */
      [[deprecated("Binding functionality of Array is deprecated, ArrayView should be used instead.")]]
      Array( Value* data,
             const IndexType& size );

@@ -111,19 +104,15 @@ class Array
      explicit Array( const Array& array );

      /**
       * \brief Bind constructor .
       *
       * The constructor does not make a deep copy, but binds to the supplied array.
       * This is also obsolete, \ref ArraView should be used instead.
       * \brief Copy constructor .
       *
       * \param array is an array that is to be bound.
       * \param begin is the first index which should be bound.
       * \param size is number of array elements that should be bound.
       * \param array is an array that is to be copied.
       * \param begin is the first index which should be copied.
       * \param size is number of array elements that should be copied.
       */
      [[deprecated("Binding functionality of Array is deprecated, ArrayView should be used instead.")]]
      Array( Array& array,
             const IndexType& begin,
             const IndexType& size = 0 );
             IndexType size = -1 );

      /**
       * \brief Move constructor.
@@ -551,6 +540,19 @@ class Array
      __cuda_callable__
      bool empty() const;

      /**
       * \brief Method for saving the object to a file \e fileName as a binary data.
       *
       * \param fileName file name.
       */
      void save( const String& fileName ) const;
      /**
       * Method for loading the object from a file \e fileName as a binary data.
       *
       * \param fileName file name
       */
      void load( const String& fileName );

      /** \brief Basic destructor. */
      ~Array();

+35 −21
Original line number Diff line number Diff line
@@ -56,11 +56,13 @@ template< typename Value,
Array< Value, Device, Index >::
Array( Value* data,
       const IndexType& size )
: size( size ),
  data( data ),
  allocationPointer( 0 ),
: size( 0 ),
  data( nullptr ),
  allocationPointer( nullptr ),
  referenceCounter( 0 )
{
   this->setSize( size );
   Algorithms::ArrayOperations< Device >::copyMemory( this->getData(), data, size );
}

template< typename Value,
@@ -73,6 +75,7 @@ Array( const Array< Value, Device, Index >& array )
  allocationPointer( nullptr ),
  referenceCounter( 0 )
{
   std::cerr << "==================" << std::endl;
   this->setSize( array.getSize() );
   Algorithms::ArrayOperations< Device >::copyMemory( this->getData(), array.getData(), array.getSize() );
}
@@ -83,30 +86,19 @@ template< typename Value,
Array< Value, Device, Index >::
Array( Array< Value, Device, Index >& array,
       const IndexType& begin,
       const IndexType& size )
       IndexType size )
: size( size ),
  data( &array.getData()[ begin ] ),
  allocationPointer( array.allocationPointer ),
  data( nullptr ),
  allocationPointer( nullptr ),
  referenceCounter( 0 )
{
   TNL_ASSERT_TRUE( array.getData(), "Empty arrays cannot be bound." );
   if( size == -1 )
      size = array.getSize() - begin;
   TNL_ASSERT_LT( begin, array.getSize(), "Begin of array is out of bounds." );
   TNL_ASSERT_LE( begin + size, array.getSize(), "End of array is out of bounds." );

   if( ! this->size )
      this->size = array.getSize() - begin;
   if( array.allocationPointer )
   {
      if( array.referenceCounter )
      {
         this->referenceCounter = array.referenceCounter;
         *this->referenceCounter += 1;
      }
      else
      {
         this->referenceCounter = array.referenceCounter = new int( 2 );
      }
   }
   this->setSize( size );
   Algorithms::ArrayOperations< Device >::copyMemory( this->getData(), &array.getData()[ begin ], size );
}

template< typename Value,
@@ -667,6 +659,28 @@ empty() const
   return ( data == nullptr );
}

template< typename Value,
          typename Device,
          typename Index >
void Array< Value, Device, Index >::save( const String& fileName ) const
{
   File file;
   file.open( fileName, std::ios_base::out );
   file << *this;
}

template< typename Value,
          typename Device,
          typename Index >
void
Array< Value, Device, Index >::
load( const String& fileName )
{
   File file;
   file.open( fileName, std::ios_base::in );
   file >> *this;
}

template< typename Value,
          typename Device,
          typename Index >
+14 −0
Original line number Diff line number Diff line
@@ -444,6 +444,20 @@ public:
   __cuda_callable__
   bool empty() const;

   /**
    * \brief Method for saving the object to a file \e fileName as a binary data.
    *
    * \param fileName file name.
    */
   void save( const String& fileName ) const;

   /**
    * Method for loading the object from a file \e fileName as a binary data.
    *
    * \param fileName file name
    */
   void load( const String& fileName );

protected:
   //! Pointer to allocated data
   Value* data = nullptr;
+22 −0
Original line number Diff line number Diff line
@@ -380,6 +380,28 @@ std::ostream& operator<<( std::ostream& str, const ArrayView< Value, Device, Ind
   return str;
}

template< typename Value,
          typename Device,
          typename Index >
void ArrayView< Value, Device, Index >::save( const String& fileName ) const
{
   File file;
   file.open( fileName, std::ios_base::out );
   file << *this;
}

template< typename Value,
          typename Device,
          typename Index >
void
ArrayView< Value, Device, Index >::
load( const String& fileName )
{
   File file;
   file.open( fileName, std::ios_base::in );
   file >> *this;
}

// Serialization of array views into binary files.
template< typename Value, typename Device, typename Index >
File& operator<<( File& file, const ArrayView< Value, Device, Index > view )
+1 −1
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ public:
    */
   Vector( Vector& vector,
           const IndexType& begin = 0,
           const IndexType& size = 0 );
           const IndexType& size = -1 );

   /**
    * \brief Move constructor.
Loading