00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef _CFL_TOOLHASHBUILDER_H
00016 #define _CFL_TOOLHASHBUILDER_H
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 class CHashBuilder
00086 {
00087 public:
00088
00089
00090
00091 typedef CHashBuilder Self;
00092
00093
00094
00095
00096 typedef unsigned char uchar;
00097
00098
00099
00100
00101 typedef unsigned int uint;
00102
00103
00104
00105
00106 typedef unsigned long ulong;
00107
00108
00109
00110
00111 typedef ulong ValueType;
00112
00113
00114
00115
00116
00117
00118
00119
00120 inline CHashBuilder() : m_hash( 0 )
00121 {
00122 ;
00123 }
00124
00125
00126
00127
00128 inline CHashBuilder( char c ) : m_hash( 0 )
00129 {
00130 (void) Add( c );
00131 }
00132
00133
00134
00135
00136 inline CHashBuilder( int i ) : m_hash( 0 )
00137 {
00138 (void) Add( i );
00139 }
00140
00141
00142
00143
00144 inline CHashBuilder( long int i ) : m_hash( 0 )
00145 {
00146 (void) Add( i );
00147 }
00148
00149
00150
00151
00152 inline CHashBuilder( unsigned char c ) : m_hash( 0 )
00153 {
00154 (void) Add( c );
00155 }
00156
00157
00158
00159
00160 inline CHashBuilder( uint i ) : m_hash( 0 )
00161 {
00162 (void) Add( i );
00163 }
00164
00165
00166
00167
00168 inline CHashBuilder( ulong i ) : m_hash( 0 )
00169 {
00170 (void) Add( i );
00171 }
00172
00173
00174
00175
00176 inline CHashBuilder( float f ) : m_hash( 0 )
00177 {
00178 (void) Add( f );
00179 }
00180
00181
00182
00183
00184 inline CHashBuilder( double d ) : m_hash( 0 )
00185 {
00186 (void) Add( d );
00187 }
00188
00189
00190
00191
00192 inline CHashBuilder( const char* s ) : m_hash( 0 )
00193 {
00194 (void) Add( s );
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204 inline Self& operator= ( char c )
00205 {
00206 Clear();
00207 return Add( c );
00208 }
00209
00210
00211
00212
00213 inline Self& operator= ( int i )
00214 {
00215 Clear();
00216 return Add( i );
00217 }
00218
00219
00220
00221
00222 inline Self& operator= ( long int i )
00223 {
00224 Clear();
00225 return Add( i );
00226 }
00227
00228
00229
00230
00231 inline Self& operator= ( unsigned char c )
00232 {
00233 Clear();
00234 return Add( c );
00235 }
00236
00237
00238
00239
00240 inline Self& operator= ( uint i )
00241 {
00242 Clear();
00243 return Add( i );
00244 }
00245
00246
00247
00248
00249 inline Self& operator= ( ulong i )
00250 {
00251 Clear();
00252 return Add( i );
00253 }
00254
00255
00256
00257
00258 inline Self& operator= ( float f )
00259 {
00260 Clear();
00261 return Add( f );
00262 }
00263
00264
00265
00266
00267 inline Self& operator= ( double d )
00268 {
00269 Clear();
00270 return Add( d );
00271 }
00272
00273
00274
00275
00276 inline Self& operator= ( const char* s )
00277 {
00278 Clear();
00279 return Add( s );
00280 }
00281
00282
00283
00284
00285 inline Self& operator, ( char chr )
00286 {
00287 return Add( chr );
00288 }
00289
00290
00291
00292
00293 inline Self& operator, ( uchar chr )
00294 {
00295 return Add( chr );
00296 }
00297
00298
00299
00300
00301 inline Self& operator, ( int i )
00302 {
00303 return Add( i );
00304 }
00305
00306
00307
00308
00309 inline Self& operator, ( uint i )
00310 {
00311 return Add( i );
00312 }
00313
00314
00315
00316
00317 inline Self& operator, ( long i )
00318 {
00319 return Add( i );
00320 }
00321
00322
00323
00324
00325 inline Self& operator, ( ulong i )
00326 {
00327 return Add( i );
00328 }
00329
00330
00331
00332
00333 inline Self& operator, ( float f )
00334 {
00335 return Add( f );
00336 }
00337
00338
00339
00340
00341 inline Self& operator, ( double d )
00342 {
00343 return Add( d );
00344 }
00345
00346
00347
00348
00349 inline Self& operator, ( const char* s )
00350 {
00351 return Add( s );
00352 }
00353
00354
00355
00356
00357 inline operator ValueType () const
00358 {
00359 return GetHash();
00360 }
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 inline ValueType GetHash() const
00371 {
00372 return m_hash;
00373 }
00374
00375
00376
00377
00378
00379
00380
00381
00382 inline void Clear()
00383 {
00384 m_hash = 0;
00385 }
00386
00387
00388
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
00407
00408 inline Self& Add( char chr )
00409 {
00410 return Add( uchar( chr ) );
00411 }
00412
00413
00414
00415
00416 inline Self& Add( int i )
00417 {
00418 return Add( uint( i ) );
00419 }
00420
00421
00422
00423
00424 inline Self& Add( uint i )
00425 {
00426 return Add( uchar( i / 256 ) ).Add( uchar( i % 256 ) );
00427 }
00428
00429
00430
00431
00432 inline Self& Add( long i )
00433 {
00434 return Add( ulong( i ) );
00435 }
00436
00437
00438
00439
00440 inline Self& Add( ulong i )
00441 {
00442 return Add( uint( i / 65536L ) ).Add( uint( i % 65536L ) );
00443 }
00444
00445
00446
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
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
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
00493
00494 ValueType m_hash;
00495 };
00496
00497
00498
00499 #endif // _CFL_TOOLHASHBUILDER_H
00500
00501
00502
00503
00504