Sleipnir
src/trie.h
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 #ifndef TRIE_H
00023 #define TRIE_H
00024 
00025 #include "triei.h"
00026 
00027 namespace Sleipnir {
00028 
00029 template<class tType> class CTrieIterator;
00030 
00053 template<class tType>
00054 class CTrie : public CTrieImpl<tType> {
00055 public:
00060     typedef CTrieIterator<tType>    iterator;
00061 
00079     CTrie( const std::vector<unsigned char>& vecbSizes, const tType& Default ) : CTrieImpl<tType>( Default ) {
00080 
00081         m_vecbSizes.resize( vecbSizes.size( ) );
00082         std::copy( vecbSizes.begin( ), vecbSizes.end( ), m_vecbSizes.begin( ) ); }
00083 
00113     CTrie( const IDataset* pData, const tType& Default, bool fAnswers = true ) : CTrieImpl<tType>( Undef ) {
00114         size_t                  i, j, k, iExp;
00115         vector<unsigned char>   vecbDatum;
00116 
00117         for( i = j = 0; i < pData->GetExperiments( ); ++i )
00118             if( !pData->IsHidden( i ) )
00119                 j++;
00120         m_vecbSizes.resize( j );
00121         for( i = j = 0; i < pData->GetExperiments( ); ++i )
00122             if( !pData->IsHidden( i ) )
00123                 m_vecbSizes[ j++ ] = (unsigned char)pData->GetBins( i ) + 1;
00124 
00125         vecbDatum.resize( GetSize( ) );
00126         for( i = 0; i < pData->GetGenes( ); ++i )
00127             for( j = ( i + 1 ); j < pData->GetGenes( ); ++j ) {
00128                 if( !pData->IsExample( i, j ) || ( fAnswers && ( pData->GetDiscrete( i, j, 0 ) == -1 ) ) )
00129                     continue;
00130                 for( iExp = k = 0; k < pData->GetExperiments( ); ++k )
00131                     if( !pData->IsHidden( k ) )
00132                         vecbDatum[ iExp++ ] = (unsigned char)( pData->GetDiscrete( i, j, k ) + 1 );
00133                 Set( vecbDatum )++; } }
00134 
00135     ~CTrie( ) {
00136 
00137         Delete( m_aabData ); }
00138 
00156     tType& Set( const std::vector<unsigned char>& vecbData ) {
00157         size_t              iDepth;
00158         unsigned char       b;
00159         unsigned char***    paabData;
00160 
00161         for( iDepth = 0,paabData = &m_aabData; iDepth < m_vecbSizes.size( );
00162             paabData = (unsigned char***)&(*paabData)[ vecbData[ iDepth++ ] ] )
00163             if( !*paabData ) {
00164                 *paabData = new unsigned char*[ m_vecbSizes[ iDepth ] ];
00165                 for( b = 0; b < m_vecbSizes[ iDepth ]; ++b )
00166                     if( ( iDepth + 1 ) == m_vecbSizes.size( ) )
00167                         *(tType*)&(*paabData)[ b ] = m_Undef;
00168                     else
00169                         (*paabData)[ b ] = NULL; }
00170 
00171         return *(tType*)paabData; }
00172 
00190     const tType Get( const std::vector<unsigned char>& vecbData ) const {
00191         size_t          iDepth;
00192         unsigned char** aabData;
00193 
00194         for( iDepth = 0,aabData = m_aabData; iDepth < m_vecbSizes.size( );
00195             aabData = (unsigned char**)aabData[ vecbData[ iDepth++ ] ] )
00196             if( !aabData )
00197                 return m_Undef;
00198 
00199         return (tType)aabData; }
00200 
00208     size_t GetSize( ) const {
00209 
00210         return m_vecbSizes.size( ); }
00211 
00225     unsigned char GetSize( size_t iDepth ) const {
00226 
00227         return m_vecbSizes[ iDepth ]; }
00228 };
00229 
00248 template<class tType>
00249 class CTrieIterator : protected CTrieIteratorImpl<tType> {
00250 public:
00261     CTrieIterator( const CTrieIterator& Iter ) : CTrieIteratorImpl( Iter ) { }
00262 
00270     CTrieIterator( const CTrie<tType>& Trie ) : CTrieIteratorImpl( Trie ) { }
00271 
00282     bool IsDone( ) const {
00283 
00284         return ( m_vecpaaPosition.empty( ) || !m_vecpaaPosition[ m_vecpaaPosition.size( ) - 1 ] ); }
00285 
00293     const std::vector<unsigned char>& GetPosition( ) const {
00294 
00295         return m_vecbPosition; }
00296 
00307     const tType& Get( ) const {
00308 
00309         return Set( ); }
00310 
00321     tType& Set( ) const {
00322 
00323         return *GetValue( ); }
00324 
00335     bool Next( ) {
00336         size_t  iDepth;
00337         bool    fReset;
00338 
00339         if( IsDone( ) )
00340             return false;
00341 
00342         fReset = false;
00343         iDepth = m_vecbPosition.size( ) - 1;
00344         while( true ) {
00345             if( NextDown( iDepth, fReset ) )
00346                 return true;
00347             fReset = true;
00348             if( ( iDepth = NextUp( ) ) == -1 )
00349                 return false; } }
00350 };
00351 
00352 }
00353 
00354 #endif // TRIE_H