Filter Library Camera Interface Physics

ToolHashBuilder.h

00001 /*
00002  * ToolHashBuilder.h - incrementally build a hash from fundamental types.
00003  *
00004  * This file is part of the Camera Filter Library.
00005  * Computer Aided Measurement Environment for Realtime Atomic imaging (Camera)
00006  *
00007  * Copyright (C) 2005, Leiden Probe Microscopy.
00008  * Copyright (C) 2005, Universiteit Leiden.
00009  *
00010  * Authors: Martin J. Moene
00011  *
00012  * $Id: ToolHashBuilder.h 233 2005-06-27 08:55:41Z moene $
00013  */
00014 
00015 #ifndef _CFL_TOOLHASHBUILDER_H
00016 #define _CFL_TOOLHASHBUILDER_H
00017 
00018 /* -----------------------------------------------------------------------
00019 ** class declarations
00020 ** -----------------------------------------------------------------------
00021  */
00022 
00023 /**
00024  * \addtogroup cfl_dialog
00025  *
00026  * @{
00027  */
00028 
00029 /**
00030  * incrementally build a hash value from fundamental types.
00031  *
00032  * This class can incrementally build a hash value from fundamental types.
00033  * It is used to determine if settings on a dialog changed or have remained as
00034  * they where with respect to a previously taken snapshot.
00035  *
00036  * Example.
00037  * \code
00038  * char   character = 'a';
00039  * int    integer   =  0;
00040  * char*  string    = "";
00041  *
00042  * typedef CHashBuilder::ValueType HashType;
00043  *
00044  * HashType LastHash = 0;
00045  *
00046  * HashType ComputeHash()
00047  * {
00048  *    CHashBuilder NewHash;
00049  *
00050  *    return NewHash = character, integer, string;
00051  * }
00052  *
00053  * bool SomethingChanged()
00054  * {
00055  *    return ComputeHash() != LastHash;
00056  * }
00057  *
00058  * void MyInitialize()
00059  * {
00060  *    LastHash = ComputeHash();
00061  * }
00062  *
00063  * void MyAction()
00064  * {
00065  *    if ( SomethingChanged() )
00066  *    {
00067  *       // act on changes and save new hash value
00068  *
00069  *       LastHash = ComputeHash();
00070  *    }
00071  *    else
00072  *    {
00073  *       ; // no action required
00074  *    }
00075  * }
00076  * \endcode
00077  *
00078  * Class CHashBuilder was inspired on the article "A Checksum Algorithm"
00079  * by Joseph M. Newcomer \ref newcomer_checksumalgorithm "[CHECKSUM]".
00080  *
00081  * Class CHashBuilder uses the hash function by P.J. Weinberger from the dragon
00082  * book \ref dragon_1986 "[Aho et al., 1986]".
00083  *
00084  */
00085 class CHashBuilder
00086 {
00087 public:
00088    /**
00089     * this type.
00090     */
00091    typedef CHashBuilder Self;
00092 
00093    /**
00094     * unsigned character type;
00095     */
00096    typedef unsigned char uchar;
00097 
00098    /**
00099     * unsigned integer type.
00100     */
00101    typedef unsigned int  uint;
00102 
00103    /**
00104     * unsigned long integer type.
00105     */
00106    typedef unsigned long ulong;
00107 
00108    /**
00109     * the hash type.
00110     */
00111    typedef ulong ValueType;
00112 
00113    ///
00114    /// \name Construction
00115    /// @{
00116 
00117    /**
00118     * default constructor.
00119     */
00120    inline CHashBuilder() : m_hash( 0 )
00121    {
00122       ; // do nothing
00123    }
00124 
00125    /**
00126     * construct from a character.
00127     */
00128    inline CHashBuilder( char c ) : m_hash( 0 )
00129    {
00130       (void) Add( c );
00131    }
00132 
00133    /**
00134     * construct from an integer.
00135     */
00136    inline CHashBuilder( int i ) : m_hash( 0 )
00137    {
00138       (void) Add( i );
00139    }
00140 
00141    /**
00142     * construct from a long integer.
00143     */
00144    inline CHashBuilder( long int i ) : m_hash( 0 )
00145    {
00146       (void) Add( i );
00147    }
00148 
00149    /**
00150     * construct from an unsigned character.
00151     */
00152    inline CHashBuilder( unsigned char c ) : m_hash( 0 )
00153    {
00154       (void) Add( c );
00155    }
00156 
00157    /**
00158     * construct from  an unsigned integer.
00159     */
00160    inline CHashBuilder( uint i ) : m_hash( 0 )
00161    {
00162       (void) Add( i );
00163    }
00164 
00165    /**
00166     * construct from  an unsigned long integer.
00167     */
00168    inline CHashBuilder( ulong i ) : m_hash( 0 )
00169    {
00170       (void) Add( i );
00171    }
00172 
00173    /**
00174     * construct from a float.
00175     */
00176    inline CHashBuilder( float f ) : m_hash( 0 )
00177    {
00178       (void) Add( f );
00179    }
00180 
00181    /**
00182     * construct from a double.
00183     */
00184    inline CHashBuilder( double d ) : m_hash( 0 )
00185    {
00186       (void) Add( d );
00187    }
00188 
00189    /**
00190     * construct from  a C-string.
00191     */
00192    inline CHashBuilder( const char* s ) : m_hash( 0 )
00193    {
00194       (void) Add( s );
00195    }
00196 
00197    /// @}
00198    /// \name Operators
00199    /// @{
00200 
00201    /**
00202     * assign a character.
00203     */
00204    inline Self& operator= ( char c )
00205    {
00206       Clear();
00207       return Add( c );
00208    }
00209 
00210    /**
00211     * assign an integer.
00212     */
00213    inline Self& operator= ( int i )
00214    {
00215       Clear();
00216       return Add( i );
00217    }
00218 
00219    /**
00220     * assign a long integer.
00221     */
00222    inline Self& operator= ( long int i )
00223    {
00224       Clear();
00225       return Add( i );
00226    }
00227 
00228    /**
00229     * assign an unsigned character.
00230     */
00231    inline Self& operator= ( unsigned char c )
00232    {
00233       Clear();
00234       return Add( c );
00235    }
00236 
00237    /**
00238     * assign an unsigned integer.
00239     */
00240    inline Self& operator= ( uint i )
00241    {
00242       Clear();
00243       return Add( i );
00244    }
00245 
00246    /**
00247     * assign an unsigned long integer.
00248     */
00249    inline Self& operator= ( ulong i )
00250    {
00251       Clear();
00252       return Add( i );
00253    }
00254 
00255    /**
00256     * assign a float.
00257     */
00258    inline Self& operator= ( float f )
00259    {
00260       Clear();
00261       return Add( f );
00262    }
00263 
00264    /**
00265     * assign a double.
00266     */
00267    inline Self& operator= ( double d )
00268    {
00269       Clear();
00270       return Add( d );
00271    }
00272 
00273    /**
00274     * assign a C-string.
00275     */
00276    inline Self& operator= ( const char* s )
00277    {
00278       Clear();
00279       return Add( s );
00280    }
00281 
00282    /**
00283     * add a character.
00284     */
00285    inline Self& operator, ( char chr )
00286    {
00287       return Add( chr );
00288    }
00289 
00290    /**
00291     * add a unsigned character.
00292     */
00293    inline Self& operator, ( uchar chr )
00294    {
00295       return Add( chr );
00296    }
00297 
00298    /**
00299     * add an integer.
00300     */
00301    inline Self& operator, ( int i )
00302    {
00303       return Add( i );
00304    }
00305 
00306    /**
00307     * add an unsigned integer.
00308     */
00309    inline Self& operator, ( uint i )
00310    {
00311       return Add( i );
00312    }
00313 
00314    /**
00315     * add a long integer.
00316     */
00317    inline Self& operator, ( long i )
00318    {
00319       return Add( i );
00320    }
00321 
00322    /**
00323     * add an unsigned long integer.
00324     */
00325    inline Self& operator, ( ulong i )
00326    {
00327       return Add( i );
00328    }
00329 
00330    /**
00331     * add a float.
00332     */
00333    inline Self& operator, ( float f )
00334    {
00335       return Add( f );
00336    }
00337 
00338    /**
00339     * add a double.
00340     */
00341    inline Self& operator, ( double d )
00342    {
00343       return Add( d );
00344    }
00345 
00346    /**
00347     * add a C-string.
00348     */
00349    inline Self& operator, ( const char* s )
00350    {
00351       return Add( s );
00352    }
00353 
00354    /**
00355     * convert to ValueType, return hash value; alias for GetHash().
00356     */
00357    inline operator ValueType () const
00358    {
00359       return GetHash();
00360    }
00361 
00362 
00363    /// @}
00364    /// \name Accessors
00365    /// @{
00366 
00367    /**
00368     * the current hash value.
00369     */
00370    inline ValueType GetHash() const
00371    {
00372       return m_hash;
00373    }
00374 
00375    /// @}
00376    /// \name Mutators
00377    /// @{
00378 
00379    /**
00380     * clear the hash value.
00381     */
00382    inline void Clear()
00383    {
00384       m_hash = 0;
00385    }
00386 
00387    /**
00388     * \brief add an unsigned character to the hash (P.J. Weinberger's hash, hash_pjw).
00389     */
00390    inline Self& Add( unsigned char chr )
00391    {
00392       m_hash = ( m_hash << 4 ) + ( chr * 13 );
00393 
00394       ValueType g = m_hash & 0xf0000000;
00395 
00396       if ( g )
00397       {
00398          m_hash ^= ( g >> 24 );
00399          m_hash ^=   g;
00400       }
00401 
00402       return *this;
00403    }
00404 
00405    /**
00406     * add a character to the hash.
00407     */
00408    inline Self& Add( char chr )
00409    {
00410       return Add( uchar( chr ) );
00411    }
00412 
00413    /**
00414     * add an integer to the hash.
00415     */
00416    inline Self& Add( int i )
00417    {
00418       return Add( uint( i ) );
00419    }
00420 
00421    /**
00422     * add an unsigned integer to the hash.
00423     */
00424    inline Self& Add( uint i )
00425    {
00426       return Add( uchar( i / 256 ) ).Add( uchar( i % 256 ) );
00427    }
00428 
00429    /**
00430     * add a long to the hash.
00431     */
00432    inline Self& Add( long i )
00433    {
00434       return Add( ulong( i ) );
00435    }
00436 
00437    /**
00438     * add an unsigned long to the hash.
00439     */
00440    inline Self& Add( ulong i )
00441    {
00442       return Add( uint( i / 65536L ) ).Add( uint( i % 65536L ) );
00443    }
00444 
00445    /**
00446     * add a float to the hash.
00447     */
00448    inline Self& Add( float f )
00449    {
00450       uchar* begin = (uchar*) &f;
00451 
00452       for ( uchar* p = begin; p < begin + sizeof( float ); ++p )
00453       {
00454          Add( *p );
00455       }
00456 
00457       return *this;
00458    }
00459 
00460    /**
00461     * add a float to the hash.
00462     */
00463    inline Self& Add( double d )
00464    {
00465       uchar* begin = (uchar*) &d;
00466 
00467       for ( uchar* p = begin; p < begin + sizeof( double ); ++p )
00468       {
00469          Add( *p );
00470       }
00471 
00472       return *this;
00473    }
00474 
00475    /**
00476     * add a C-string to the hash.
00477     */
00478    inline Self& Add( const char* string )
00479    {
00480       for ( ; *string; ++string )
00481       {
00482          (void) Add( *string );
00483       }
00484 
00485       return *this;
00486    }
00487 
00488    /// @}
00489 
00490 private:
00491    /*
00492     * current hash value.
00493     */
00494    ValueType m_hash;
00495 };
00496 
00497 /// @} cfl_toolhashbuilder
00498 
00499 #endif // _CFL_TOOLHASHBUILDER_H
00500 
00501 /*
00502  * end of file
00503  */
00504 

Camera Filter Library documentation © 2004-2007 by Leiden Probe Microscopy