Newer
Older
/***************************************************************************
EllpackIndexMultimap_impl.h - description
-------------------
begin : Sep 9, 2015
copyright : (C) 2015 by Tomas Oberhuber et al.
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#include <TNL/Experimental/Multimaps/EllpackIndexMultimap.h>
template< typename Index,
typename Device,
typename LocalIndex >
EllpackIndexMultimap< Index, Device, LocalIndex >::
: keysRange( 0 ), maxValuesCount( 0 )
{
}
template< typename Index,
typename Device,
typename LocalIndex >
String
EllpackIndexMultimap< Index, Device, LocalIndex >::
getType()
return String( "EllpackIndexMultimap< ") +
String( TNL::getType< Index >() ) +
String( ", " ) +
Device :: getDeviceType() +
String( TNL::getType< LocalIndexType >() ) +
}
template< typename Index,
typename Device,
typename LocalIndex >
String
EllpackIndexMultimap< Index, Device, LocalIndex >::
getTypeVirtual() const
{
return this->getType();
}
template< typename Index,
typename Device,
typename LocalIndex >
EllpackIndexMultimap< Index, Device, LocalIndex >::
setKeysRange( const IndexType& keysRange )
TNL_ASSERT( keysRange >= 0, );
this->keysRange = keysRange;
}
template< typename Index,
typename Device,
typename LocalIndex >
EllpackIndexMultimap< Index, Device, LocalIndex >::
getKeysRange() const
{
return this->keysRange;
}
template< typename Index,
typename Device,
typename LocalIndex >
bool
EllpackIndexMultimap< Index, Device, LocalIndex >::
Jakub Klinkovský
committed
allocate( const LocalIndexType& maxValuesCount )
TNL_ASSERT( maxValuesCount >= 0, );
Jakub Klinkovský
committed
this->maxValuesCount = maxValuesCount;
Jakub Klinkovský
committed
if( ! this->values.setSize( this->keysRange * this->maxValuesCount ) )
Jakub Klinkovský
committed
return false;
Jakub Klinkovský
committed
if( ! this->valuesCounts.setSize( this->keysRange ) )
return false;
this->valuesCounts.setValue( maxValuesCount );
Jakub Klinkovský
committed
return true;
}
template< typename Index,
typename Device,
typename LocalIndex >
bool
EllpackIndexMultimap< Index, Device, LocalIndex >::
allocate( const ValuesAllocationVectorType& valuesCounts )
{
TNL_ASSERT( valuesCounts.getSize() == this->keysRange,
std::cerr << "valuesCounts.getSize() = " << valuesCounts.getSize()
<< "this->keysRange = " << this->keysRange
<< std::endl; );
Jakub Klinkovský
committed
this->maxValuesCount = valuesCounts.max();
TNL_ASSERT( this->maxValuesCount >= 0,
std::cerr << "this->maxValuesCount = " << this->maxValuesCount << std::endl; );
Jakub Klinkovský
committed
if( ! this->values.setSize( this->keysRange * this->maxValuesCount ) )
Jakub Klinkovský
committed
return false;
Jakub Klinkovský
committed
if( ! this->valuesCounts.setSize( this->keysRange ) )
return false;
this->valuesCounts = valuesCounts;
Jakub Klinkovský
committed
return true;
}
template< typename Index,
typename Device,
typename LocalIndex >
typename EllpackIndexMultimap< Index, Device, LocalIndex >::ValuesAccessorType
EllpackIndexMultimap< Index, Device, LocalIndex >::
getValues( const IndexType& inputIndex )
{
TNL_ASSERT( inputIndex < this->getKeysRange(),
std::cerr << "inputIndex = " << inputIndex << std::endl
<< "this->getKeysRange() = " << this->getKeysRange()
<< std::endl; );
TNL_ASSERT( this->getKeysRange() * this->maxValuesCount == this->values.getSize() && this->getKeysRange() == this->valuesCounts.getSize(),
std::cerr << "The map has not been reallocated after calling setKeysRange()." << std::endl
<< "this->getKeysRange() = " << this->getKeysRange() << std::endl
<< "this->maxValuesCount = " << this->maxValuesCount << std::endl
<< "this->values.getSize() = " << this->values.getSize() << std::endl
<< "this->valuesCounts.getSize() = " << this->valuesCounts.getSize() << std::endl; );
Jakub Klinkovský
committed
return ValuesAccessorType( this->values.getData(), this->valuesCounts.getData(), inputIndex, this->maxValuesCount );
}
template< typename Index,
typename Device,
typename LocalIndex >
typename EllpackIndexMultimap< Index, Device, LocalIndex >::ConstValuesAccessorType
EllpackIndexMultimap< Index, Device, LocalIndex >::
getValues( const IndexType& inputIndex ) const
{
TNL_ASSERT( inputIndex < this->getKeysRange(),
std::cerr << "inputIndex = " << inputIndex << std::endl
<< "this->getKeysRange() = " << this->getKeysRange()
<< std::endl; );
TNL_ASSERT( this->getKeysRange() * this->maxValuesCount == this->values.getSize() && this->getKeysRange() == this->valuesCounts.getSize(),
std::cerr << "The map has not been reallocated after calling setKeysRange()." << std::endl
<< "this->getKeysRange() = " << this->getKeysRange() << std::endl
<< "this->maxValuesCount = " << this->maxValuesCount << std::endl
<< "this->values.getSize() = " << this->values.getSize() << std::endl
<< "this->valuesCounts.getSize() = " << this->valuesCounts.getSize() << std::endl; );
Jakub Klinkovský
committed
return ConstValuesAccessorType( this->values.getData(), this->valuesCounts.getData(), inputIndex, this->maxValuesCount );
template< typename Index,
typename Device,
typename LocalIndex >
bool
EllpackIndexMultimap< Index, Device, LocalIndex >::
operator==( const EllpackIndexMultimap< Index, Device, LocalIndex >& other ) const
{
Jakub Klinkovský
committed
if( ! ( this->keysRange == other.keysRange &&
this->maxValuesCount == other.maxValuesCount &&
this->valuesCounts == other.valuesCounts ) )
return false;
// compare values for each key separately - the sizes may vary
for( IndexType i = 0; i < this->keysRange; i++ )
if( this->getValues( i ) != other.getValues( i ) )
return false;
return true;
}
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;
Jakub Klinkovský
committed
if( ! this->valuesCounts.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;
Jakub Klinkovský
committed
if( ! this->valuesCounts.load( file ) )
return false;
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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;
}