00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 #ifndef fb_freq_INCLUDED
00085 #define fb_freq_INCLUDED
00086
00087 #include "defs.h"
00088 #ifndef ERRORS_INCLUDED
00089 #include "errors.h"
00090 #endif
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 enum FB_FREQ_TYPE {
00104 FB_FREQ_TYPE_EXACT = 1,
00105 FB_FREQ_TYPE_GUESS = 0,
00106 FB_FREQ_TYPE_UNKNOWN = -1,
00107 FB_FREQ_TYPE_UNINIT = -2,
00108 FB_FREQ_TYPE_ERROR = -3
00109 };
00110
00111 #define FB_FREQ_TYPE_COMBINE( type1, type2 ) \
00112 ( (type1) > (type2) ? (type2) : (type1) )
00113 #define FB_FREQ_TYPE_BETTER( type1, type2 ) ( (type1) > (type2) )
00114
00115 #define FB_FREQ_TYPE_IS_EXACT( type ) ( (type) > 0 )
00116 #define FB_FREQ_TYPE_IS_GUESS( type ) ( (type) == 0 )
00117 #define FB_FREQ_TYPE_IS_KNOWN( type ) ( (type) >= 0 )
00118 #define FB_FREQ_TYPE_NOT_KNOWN( type ) ( (type) < 0 )
00119 #define FB_FREQ_TYPE_INITIALIZED( type ) ( (type) >= FB_FREQ_TYPE_UNKNOWN )
00120
00121 #define FB_FREQ_TYPE_IS_VALID( type ) \
00122 ( (type) >= FB_FREQ_TYPE_ERROR && (type) <= FB_FREQ_TYPE_EXACT )
00123
00124
00125
00126
00127
00128
00129
00130
00131 const float FB_FREQ_EPSILON = 0.0001;
00132
00133 class FB_FREQ {
00134 private:
00135
00136 FB_FREQ_TYPE _type;
00137 float _value;
00138
00139
00140
00141
00142
00143
00144
00145 FB_FREQ( FB_FREQ_TYPE type, float value )
00146 : _type( type ), _value( value ) { }
00147
00148 public:
00149
00150
00151
00152 FB_FREQ()
00153 : _type( FB_FREQ_TYPE_UNINIT ),
00154 _value( (float) FB_FREQ_TYPE_UNINIT ) {}
00155
00156 FB_FREQ( float value, bool exact )
00157 : _type( exact ? FB_FREQ_TYPE_EXACT : FB_FREQ_TYPE_GUESS ),
00158 _value( value )
00159 { Is_True( value >= 0.0, ( "FB_FREQ: negative value %f", value ) ); }
00160
00161 FB_FREQ( INT64 value )
00162 : _type( FB_FREQ_TYPE_EXACT ),
00163 _value( (float) value )
00164 { Is_True( value >= 0, ( "FB_FREQ: negative value %lld", value ) ); }
00165
00166 FB_FREQ( FB_FREQ_TYPE type )
00167 : _type( type ),
00168 _value( type >= 0 ? 0.0 : (float) type ) {
00169 Is_True( FB_FREQ_TYPE_IS_VALID( type ),
00170 ( "FB_FREQ: invalid type %d", type ) );
00171 }
00172
00173
00174
00175 float Value() const { return _value; }
00176
00177
00178
00179 bool Known() const { return FB_FREQ_TYPE_IS_KNOWN( _type ); }
00180 bool Guess() const { return FB_FREQ_TYPE_IS_GUESS( _type ); }
00181 bool Exact() const { return FB_FREQ_TYPE_IS_EXACT( _type ); }
00182 bool Uninitialized() const { return ( _type == FB_FREQ_TYPE_UNINIT ); }
00183 bool Initialized() const { return FB_FREQ_TYPE_INITIALIZED( _type ); }
00184 bool Error() const { return ( _type == FB_FREQ_TYPE_ERROR ); }
00185 bool Zero() const { return ( _value < FB_FREQ_EPSILON &&
00186 _value > - FB_FREQ_EPSILON ); }
00187
00188 bool Better( const FB_FREQ freq ) const {
00189 return FB_FREQ_TYPE_BETTER( _type, freq._type );
00190 }
00191
00192
00193
00194 FB_FREQ operator+= ( const FB_FREQ freq ) {
00195 _type = FB_FREQ_TYPE_COMBINE( _type, freq._type );
00196 if ( FB_FREQ_TYPE_NOT_KNOWN( _type ) )
00197 _value = (float) _type;
00198 else
00199 _value += freq._value;
00200 return *this;
00201 }
00202
00203 FB_FREQ operator-= ( const FB_FREQ freq ) {
00204 _type = FB_FREQ_TYPE_COMBINE( _type, freq._type );
00205 if ( FB_FREQ_TYPE_NOT_KNOWN( _type ) )
00206 _value = (float) _type;
00207 else {
00208 _value -= freq._value;
00209 if ( _value < 0 )
00210 if ( - _value < FB_FREQ_EPSILON
00211 || - _value < FB_FREQ_EPSILON * freq._value )
00212 _value = 0;
00213 else {
00214 DevWarn( "FB_FREQ: subtraction of larger from smaller value" );
00215 _type = FB_FREQ_TYPE_ERROR;
00216 _value = (float) _type;
00217 }
00218 }
00219 return *this;
00220 }
00221
00222 FB_FREQ operator*= ( const FB_FREQ freq ) {
00223 if ( Zero() && Exact() )
00224 ;
00225 else if ( freq.Zero() && freq.Exact() ) {
00226 _type = FB_FREQ_TYPE_EXACT;
00227 _value = 0.0;
00228 } else {
00229 _type = FB_FREQ_TYPE_COMBINE( _type, freq._type );
00230 if ( FB_FREQ_TYPE_NOT_KNOWN( _type ) )
00231 _value = (float) _type;
00232 else
00233 _value *= freq._value;
00234 }
00235 return *this;
00236 }
00237
00238
00239 FB_FREQ operator*= ( const float scale ) {
00240 Is_True ( scale >= 0.0, ( "FB_FREQ: negative scale %f", scale ) );
00241 return ( *this *= FB_FREQ( FB_FREQ_TYPE_GUESS, scale ) );
00242 }
00243
00244
00245
00246 FB_FREQ operator/= ( const FB_FREQ freq ) {
00247 if ( Zero() && Exact() )
00248 ;
00249 else if ( freq.Zero() ) {
00250 DevWarn("FB_FREQ: division by zero");
00251 _type = FB_FREQ_TYPE_ERROR;
00252 _value = (float) _type;
00253 } else {
00254 _type = FB_FREQ_TYPE_COMBINE( _type, freq._type );
00255 if ( FB_FREQ_TYPE_NOT_KNOWN( _type ) )
00256 _value = (float) _type;
00257 else {
00258 if ( _value != freq._value )
00259 _type = FB_FREQ_TYPE_COMBINE( _type, FB_FREQ_TYPE_GUESS );
00260 _value /= freq._value;
00261 }
00262 }
00263 return *this;
00264 }
00265
00266
00267 FB_FREQ operator/= ( const float scale ) {
00268 Is_True ( scale >= 0.0, ( "FB_FREQ: negative scale %f", scale ) );
00269 return ( *this /= FB_FREQ( FB_FREQ_TYPE_GUESS, scale ) );
00270 }
00271
00272
00273
00274 friend FB_FREQ operator+ ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00275 FB_FREQ_TYPE type = FB_FREQ_TYPE_COMBINE( freq1._type, freq2._type );
00276 if ( FB_FREQ_TYPE_NOT_KNOWN( type ) )
00277 return FB_FREQ( type );
00278 return FB_FREQ( type, freq1._value + freq2._value );
00279 }
00280
00281 friend FB_FREQ operator- ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00282 FB_FREQ_TYPE type = FB_FREQ_TYPE_COMBINE( freq1._type, freq2._type );
00283 if ( FB_FREQ_TYPE_NOT_KNOWN( type ) )
00284 return FB_FREQ( type );
00285 float value = freq1._value - freq2._value;
00286 if ( value >= 0 )
00287 return FB_FREQ( type, value );
00288 if ( - value < FB_FREQ_EPSILON
00289 || - value < freq2._value * FB_FREQ_EPSILON )
00290 return FB_FREQ( type, 0 );
00291 DevWarn( "FB_FREQ: subtraction of larger from smaller value" );
00292 return FB_FREQ( FB_FREQ_TYPE_ERROR );
00293 }
00294
00295 friend FB_FREQ operator* ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00296 if ( ( freq1.Zero() && freq1.Exact() ) ||
00297 ( freq2.Zero() && freq2.Exact() ) )
00298 return FB_FREQ( FB_FREQ_TYPE_EXACT, 0.0 );
00299 FB_FREQ_TYPE type = FB_FREQ_TYPE_COMBINE( freq1._type, freq2._type );
00300 if ( FB_FREQ_TYPE_NOT_KNOWN( type ) )
00301 return FB_FREQ( type );
00302 return FB_FREQ( type, freq1._value * freq2._value );
00303 }
00304
00305
00306 friend FB_FREQ operator* ( const FB_FREQ freq, const float scale ) {
00307 Is_True ( scale >= 0.0, ( "FB_FREQ: negative scale %f", scale ) );
00308 return ( freq * FB_FREQ( FB_FREQ_TYPE_GUESS, scale ) );
00309 }
00310
00311
00312
00313 friend FB_FREQ operator/ ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00314 if ( freq1.Zero() && freq1.Exact() )
00315 return FB_FREQ( FB_FREQ_TYPE_EXACT, 0.0 );
00316 if ( freq2.Zero() ) {
00317 DevWarn("FB_FREQ: division by zero");
00318 return FB_FREQ( FB_FREQ_TYPE_ERROR );
00319 }
00320 FB_FREQ_TYPE type = FB_FREQ_TYPE_COMBINE( freq1._type, freq2._type );
00321 if ( FB_FREQ_TYPE_NOT_KNOWN( type ) )
00322 return FB_FREQ( type );
00323 if ( freq1._value != freq2._value )
00324 type = FB_FREQ_TYPE_COMBINE( type, FB_FREQ_TYPE_GUESS );
00325 float value = freq1._value / freq2._value;
00326 return FB_FREQ( type, value );
00327 }
00328
00329
00330 friend FB_FREQ operator/ ( const FB_FREQ freq, float scale ) {
00331 Is_True ( scale >= 0.0, ( "FB_FREQ: negative scale %f", scale ) );
00332 return ( freq / FB_FREQ( FB_FREQ_TYPE_GUESS, scale ) );
00333 }
00334
00335 friend bool operator== ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00336 if ( freq1._value > freq2._value )
00337 return ( freq1._value < FB_FREQ_EPSILON
00338 || freq1._value * ( 1 - FB_FREQ_EPSILON ) < freq2._value );
00339 else if ( freq1._value < freq2._value )
00340 return ( freq2._value < FB_FREQ_EPSILON
00341 || freq2._value * ( 1 - FB_FREQ_EPSILON ) < freq1._value );
00342 else return true;
00343 }
00344
00345 friend bool operator!= ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00346 if ( freq1._value > freq2._value )
00347 return ( freq1._value >= FB_FREQ_EPSILON
00348 && freq1._value * ( 1 - FB_FREQ_EPSILON ) >= freq2._value );
00349 else if ( freq1._value < freq2._value )
00350 return ( freq2._value >= FB_FREQ_EPSILON
00351 && freq2._value * ( 1 - FB_FREQ_EPSILON ) >= freq1._value );
00352 else return false;
00353 }
00354
00355 friend bool operator> ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00356 return ( freq1._value > freq2._value );
00357 }
00358
00359 friend bool operator< ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00360 return ( freq1._value < freq2._value );
00361 }
00362
00363 friend bool operator<= ( const FB_FREQ freq1, const FB_FREQ freq2 ) {
00364 return ( (freq1._value < freq2._value) || (freq1 == freq2) );
00365 }
00366
00367
00368
00369 void Print( FILE *fp ) const {
00370 switch ( _type ) {
00371 case FB_FREQ_TYPE_EXACT:
00372 fprintf( fp, "%g!", _value );
00373 break;
00374 case FB_FREQ_TYPE_GUESS:
00375 fprintf( fp, "%g?", _value );
00376 break;
00377 case FB_FREQ_TYPE_UNKNOWN:
00378 fprintf( fp, "unknown" );
00379 break;
00380 case FB_FREQ_TYPE_UNINIT:
00381 fprintf( fp, "uninitialized" );
00382 break;
00383 case FB_FREQ_TYPE_ERROR:
00384 fprintf( fp, "error" );
00385 break;
00386 default:
00387 Is_True( FALSE, ("FB_FREQ: Unexpected type %d", _type ));
00388 break;
00389 }
00390 }
00391
00392 INT Sprintf( char *buffer ) const {
00393 INT length = 0;
00394 switch ( _type ) {
00395 case FB_FREQ_TYPE_EXACT:
00396 length = sprintf( buffer, "%g!", _value );
00397 break;
00398 case FB_FREQ_TYPE_GUESS:
00399 length = sprintf( buffer, "%g?", _value );
00400 break;
00401 case FB_FREQ_TYPE_UNKNOWN:
00402 length = sprintf( buffer, "unknown" );
00403 break;
00404 case FB_FREQ_TYPE_UNINIT:
00405 length = sprintf( buffer, "uninitialized" );
00406 break;
00407 case FB_FREQ_TYPE_ERROR:
00408 length = sprintf( buffer, "error" );
00409 break;
00410 default:
00411 Is_True( FALSE, ("FB_FREQ: Unexpected type %d", _type ));
00412 break;
00413 }
00414 return length;
00415 }
00416 };
00417
00418
00419
00420
00421
00422 const FB_FREQ FB_FREQ_ZERO( 0.0, true );
00423 const FB_FREQ FB_FREQ_UNKNOWN( FB_FREQ_TYPE_UNKNOWN );
00424 const FB_FREQ FB_FREQ_UNINIT( FB_FREQ_TYPE_UNINIT );
00425 const FB_FREQ FB_FREQ_ERROR( FB_FREQ_TYPE_ERROR );
00426
00427
00428
00429 #endif //#ifndef fb_freq_INCLUDED