Commit 65cea6ad authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Added save, load and print methods to EllpackIndexMultimap

parent bcc09354
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ template< typename Index = int,
          typename Device = Devices::Host,
          typename LocalIndex = Index >
class EllpackIndexMultimap
   : public virtual Object
{
   public:
      using DeviceType                 = Device;
@@ -46,6 +47,18 @@ class EllpackIndexMultimap

      ConstValuesAccessorType getValues( const IndexType& inputIndex ) const;

      bool operator==( const EllpackIndexMultimap< Index, Device, LocalIndex >& other ) const;

      bool save( File& file ) const;

      bool load( File& file );

      using Object::load;

      using Object::save;

      void print( std::ostream& str ) const;

   protected:
      Containers::Vector< IndexType, DeviceType, IndexType > values;

@@ -53,6 +66,11 @@ class EllpackIndexMultimap
      LocalIndexType maxValuesCount;
};

template< typename Index,
          typename Device,
          typename LocalIndex >
std::ostream& operator << ( std::ostream& str, const EllpackIndexMultimap< Index, Device, LocalIndex >& multimap );

} // namespace TNL

#include <TNL/Experimental/Multimaps/EllpackIndexMultimap_impl.h>
+1 −1
Original line number Diff line number Diff line
@@ -191,7 +191,7 @@ print( std::ostream& str ) const
   if( this->getSize() > 0 )
   {
      str << this->getValue( 0 );
      for( Index i = 1; i < this->getSize(); i++ )
      for( typename std::remove_const< Index >::type i = 1; i < this->getSize(); i++ )
         str << ", " << this->getValue( i );
   }
   str << " ]";
+74 −0
Original line number Diff line number Diff line
@@ -129,5 +129,79 @@ getValues( const IndexType& inputIndex ) const
   return ConstValuesAccessorType( this->values.getData(), inputIndex, this->maxValuesCount );
}

template< typename Index,
          typename Device,
          typename LocalIndex >
bool
EllpackIndexMultimap< Index, Device, LocalIndex >::
operator==( const EllpackIndexMultimap< Index, Device, LocalIndex >& other ) const
{
   return ( this->keysRange == other.keysRange &&
            this->maxValuesCount == other.maxValuesCount &&
            this->values == other.values );
}

template< typename Index,
          typename Device,
          typename LocalIndex >
bool
EllpackIndexMultimap< Index, Device, LocalIndex >::
save( File& file ) const
{
   if( ! Object::save( file ) )
      return false;
   if( ! file.write( &this->keysRange ) )
      return false;
   if( ! file.write( &this->maxValuesCount ) )
      return false;
   if( ! this->values.save( file ) )
      return false;
   return true;
}

template< typename Index,
          typename Device,
          typename LocalIndex >
bool
EllpackIndexMultimap< Index, Device, LocalIndex >::
load( File& file )
{
   if( ! Object::load( file ) )
      return false;
   if( ! file.read( &this->keysRange ) )
      return false;
   if( ! file.read( &this->maxValuesCount ) )
      return false;
   if( ! this->values.load( file ) )
      return false;
   return true;
}

template< typename Index,
          typename Device,
          typename LocalIndex >
void
EllpackIndexMultimap< Index, Device, LocalIndex >::
print( std::ostream& str ) const
{
   str << "[ ";
   if( this->getKeysRange() > 0 )
   {
      str << this->getValues( 0 );
      for( Index i = 1; i < this->getKeysRange(); i++ )
         str << ",\n  " << this->getValues( i );
   }
   str << " ]";
}

template< typename Index,
          typename Device,
          typename LocalIndex >
std::ostream& operator << ( std::ostream& str, const EllpackIndexMultimap< Index, Device, LocalIndex >& multimap )
{
   multimap.print( str );
   return str;
}

} // namespace TNL
+39 −0
Original line number Diff line number Diff line
@@ -107,6 +107,45 @@ TEST( MultimapTest, TestSettingValues )
      }
   }
}

TEST( MultimapTest, TestSaveAndLoad )
{
   using MultimapType = TNL::EllpackIndexMultimap< IndexType, Device, LocalIndexType >;

   IndexType inputs = 10;
   LocalIndexType allocatedValues = 2;

   MultimapType map, map2;
   map.setKeysRange( inputs );
   ASSERT_EQ( map.getKeysRange(), inputs );

   typename MultimapType::ValuesAllocationVectorType allocationRanges;
   ASSERT_TRUE( allocationRanges.setSize( inputs ) );
   allocationRanges.setValue( allocatedValues );
   ASSERT_TRUE( map.allocate( allocationRanges ) );

   for( IndexType i = 0; i < inputs; i++ ) {
      auto values = map.getValues( i );
      for( LocalIndexType o = 0; o < allocatedValues; o++ )
         values.setValue( o, i + o );
   }

   ASSERT_TRUE( map.save( "multimap-test.tnl" ) );
   ASSERT_TRUE( map2.load( "multimap-test.tnl" ) );

   EXPECT_EQ( map, map2 );
   EXPECT_EQ( map.getKeysRange(), map2.getKeysRange() );

   for( IndexType i = 0; i < inputs; i++ ) {
      auto values = map.getValues( i );
      auto values2 = map2.getValues( i );

      for( LocalIndexType o = 0; o < allocatedValues; o++ ) {
         ASSERT_EQ( values[ o ], i + o );
         ASSERT_EQ( values2[ o ], i + o );
      }
   }
}
#endif

int main( int argc, char* argv[] )