Sleipnir
src/compactmatrix.cpp
00001 /*****************************************************************************
00002 * This file is provided under the Creative Commons Attribution 3.0 license.
00003 *
00004 * You are free to share, copy, distribute, transmit, or adapt this work
00005 * PROVIDED THAT you attribute the work to the authors listed below.
00006 * For more information, please see the following web page:
00007 * http://creativecommons.org/licenses/by/3.0/
00008 *
00009 * This file is a component of the Sleipnir library for functional genomics,
00010 * authored by:
00011 * Curtis Huttenhower (chuttenh@princeton.edu)
00012 * Mark Schroeder
00013 * Maria D. Chikina
00014 * Olga G. Troyanskaya (ogt@princeton.edu, primary contact)
00015 *
00016 * If you use this library, the included executable tools, or any related
00017 * code in your work, please cite the following publication:
00018 * Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and
00019 * Olga G. Troyanskaya.
00020 * "The Sleipnir library for computational functional genomics"
00021 *****************************************************************************/
00022 #include "stdafx.h"
00023 #include "compactmatrix.h"
00024 
00025 namespace Sleipnir {
00026 
00028 // CCompactMatrixBase
00030 
00031 void CCompactMatrixBase::Initialize( unsigned char cValues, bool fClear ) {
00032     size_t  iWords;
00033 
00034     if( m_fMemory && m_aiData )
00035         delete[] m_aiData;
00036     m_fMemory = true;
00037     for( m_cBits = 0,--cValues; cValues; ++m_cBits,cValues >>= 1 );
00038     m_aiData = new size_t[ iWords = CountWords( ) ];
00039     //printf("size %d\n", iWords);
00040     if( fClear )
00041         memset( m_aiData, 0, iWords * sizeof(*m_aiData) ); }
00042 
00044 // CCompactMatrixImpl
00046 
00060 bool CCompactMatrix::Open( std::istream& istm ) {
00061     uint32_t    iBytes;
00062     size_t      iWords;
00063 
00064     if( m_fMemory && m_aiData )
00065         delete[] m_aiData;
00066     m_fMemory = true;
00067     istm.read( (char*)&m_iSize, sizeof(m_iSize) );
00068     istm.read( (char*)&m_cBits, sizeof(m_cBits) );
00069     istm.read( (char*)&iBytes, sizeof(iBytes) );
00070     m_aiData = new size_t[ iWords = CountWords( ) ];
00071     istm.read( (char*)m_aiData, iWords *= sizeof(*m_aiData) );
00072     istm.seekg( iBytes - iWords, ios_base::cur );
00073 
00074     return true; }
00075 
00095 const unsigned char* CCompactMatrix::Open( const unsigned char* pbData ) {
00096     uint32_t    iBytes;
00097 
00098     if( m_fMemory && m_aiData )
00099         delete[] m_aiData;
00100     m_fMemory = false;
00101     m_iSize = *(uint32_t*)pbData;
00102     pbData += sizeof(m_iSize);
00103     m_cBits = *(unsigned char*)pbData;
00104     pbData += sizeof(m_cBits);
00105     iBytes = *(uint32_t*)pbData;
00106     pbData += sizeof(iBytes);
00107     m_aiData = (size_t*)pbData;
00108     pbData += iBytes;
00109 
00110     return pbData; }
00111 
00122 void CCompactMatrix::Save( std::ostream& ostm ) const {
00123     static const char   abPad[] = "\0\0\0\0\0\0\0\0";
00124     uint32_t        iPad, iBytes;
00125     size_t          iWords;
00126 
00127     ostm.write( (char*)&m_iSize, sizeof(m_iSize) );
00128     ostm.write( (char*)&m_cBits, sizeof(m_cBits) );
00129     iBytes = ( iWords = CountWords( ) ) * sizeof(*m_aiData);
00130     // Pad for 64-bit memmapping compatibility
00131     iBytes += ( iPad = ( iBytes % 8 ) );
00132     ostm.write( (char*)&iBytes, sizeof(iBytes) );
00133     ostm.write( (char*)m_aiData, iWords * sizeof(*m_aiData) );
00134     ostm.write( abPad, iPad ); }
00135 
00136 }