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
00058 #ifndef WITHUE_H
00059 #define WITHUE_H
00060
00061 #include "machine_dependent_types.h"
00062 #include "UnknownValueException.h"
00063 #include "WithUnknown.h"
00064 #include "WithError.h"
00065
00067 #define UE_HEAD std::string("#UE")
00068
00069 RS_BEGIN_NAMESPACE
00070
00088 template<typename T>
00089 class WithUE : protected WithUnknown<WithError<T> > {
00090 public:
00092 WithUE();
00093
00095 WithUE(const T& def_value);
00096
00098 WithUE(const WithUnknown<T>& obj);
00099
00101 WithUE(const WithError<T>& obj);
00102
00104 WithUE(const WithUnknown<WithError<T> >& obj);
00105
00107 WithUE(const WithUE<T>& obj);
00108
00110 WithUE(const T& def_value, const T& def_plus, const T& def_minus);
00111
00114 WithUE(const ErrorType c, const T& value,
00115 const Float64 quant_step1 = 0.1, const T& distance = T(),
00116 const Float64 quant_step2 = 0.02);
00117
00119 WithUE<T>& operator= (const WithUE<T>& op);
00120
00123 WithUE<T>& operator++ ();
00124 WithUE<T>& operator-- ();
00125 WithUE<T> operator- () const;
00126 WithUE<T>& operator+= (const WithUE<T>& op);
00127 WithUE<T>& operator-= (const WithUE<T>& op);
00128 WithUE<T>& operator*= (const WithUE<T>& op);
00129 WithUE<T>& operator/= (const WithUE<T>& op);
00130 WithUE<T> operator+ (const WithUE<T>& op) const;
00131 WithUE<T> operator- (const WithUE<T>& op) const;
00132 WithUE<T> operator* (const WithUE<T>& op) const;
00133 WithUE<T> operator/ (const WithUE<T>& op) const;
00135
00140 bool operator== (const WithUE<T>& op) const;
00141 bool operator!= (const WithUE<T>& op) const;
00142 bool operator< (const WithUE<T>& op) const;
00143 bool operator<= (const WithUE<T>& op) const;
00144 bool operator> (const WithUE<T>& op) const;
00145 bool operator>= (const WithUE<T>& op) const;
00146 bool operator! () const;
00148
00151 WithUE<T>& improve (const WithUE<T>& op);
00152
00155 WithUE<T>& disimprove (const WithUE<T>& op);
00156
00158 WithUE<T>& makeUnknown();
00159
00161 bool isKnown() const;
00162
00164 bool isUnknown() const;
00165
00168 T getValue() const throw(UnknownValueException);
00169
00172 WithError<T> getValueWithError() const throw(UnknownValueException);
00173
00175 WithUnknown<T> getValueWithUnknown() const;
00176
00179 T getMin() const throw(UnknownValueException);
00180
00183 T getMax() const throw(UnknownValueException);
00184
00188 T getPlus() const throw(UnknownValueException);
00189
00193 T getMinus() const throw(UnknownValueException);
00194 };
00195
00196 template<typename T>
00197 WithUE<T>::WithUE()
00198 : WithUnknown<WithError<T> >() {
00199 }
00200
00201 template<typename T>
00202 WithUE<T>::WithUE(const T& def_value)
00203 : WithUnknown<WithError<T> >(WithError<T> (def_value)) {
00204 }
00205
00206 template<typename T>
00207 WithUE<T>::WithUE(const WithUnknown<T>& def_value)
00208 : WithUnknown<WithError<T> >() {
00209 if (def_value.isKnown()) {
00210 WithUnknown<WithError<T> >::operator=(WithError<T>(def_value.getValue()));
00211 }
00212 }
00213
00214 template<typename T>
00215 WithUE<T>::WithUE(const WithError<T>& def_value)
00216 : WithUnknown<WithError<T> >(def_value) {
00217 }
00218
00219 template<typename T>
00220 WithUE<T>::WithUE(const WithUnknown<WithError<T> >& obj)
00221 : WithUnknown<WithError<T> >(obj) {
00222 }
00223
00224 template<typename T>
00225 WithUE<T>::WithUE(const WithUE<T>& wc)
00226 : WithUnknown<WithError<T> >(wc) {
00227 }
00228
00229 template<typename T>
00230 WithUE<T>::WithUE(const ErrorType c, const T& value,
00231 const Float64 quant_step1,
00232 const T& distance, const Float64 quant_step2)
00233 : WithUnknown<WithError<T> >(WithError<T>(c, value, quant_step1,
00234 distance, quant_step2)) {
00235 }
00236
00237 template<typename T>
00238 WithUE<T>::WithUE(const T& def_value, const T& def_plus, const T& def_minus)
00239 : WithUnknown<WithError<T> >(WithError<T> (def_value, def_plus, def_minus, T())) {
00240 }
00241
00242 template<typename T>
00243 WithUE<T>& WithUE<T>::operator= (const WithUE<T>& op) {
00244 if (&op != this) {
00245 if (op.isKnown()) {
00246 WithUnknown<WithError<T> >::operator=(op.getValueWithError());
00247 } else {
00248 WithUnknown<WithError<T> >::makeUnknown();
00249 }
00250 }
00251 return *this;
00252 }
00253
00254 template <typename T>
00255 WithUE<T>& WithUE<T>::operator+= (const WithUE<T>& op) {
00256 WithUnknown<WithError<T> >::operator+= (op);
00257 return *this;
00258 }
00259
00260 template <typename T>
00261 WithUE<T>& WithUE<T>::operator-= (const WithUE<T>& op) {
00262 WithUnknown<WithError<T> >::operator-= (op);
00263 return *this;
00264 }
00265
00266 template <typename T>
00267 WithUE<T>& WithUE<T>::operator*= (const WithUE<T>& op) {
00268 WithUnknown<WithError<T> >::operator*= (op);
00269 return *this;
00270 }
00271
00272 template <typename T>
00273 WithUE<T>& WithUE<T>::operator/= (const WithUE<T>& op) {
00274 WithUnknown<WithError<T> >::operator/= (op);
00275 return *this;
00276 }
00277
00278 template<typename T>
00279 WithUE<T> WithUE<T>::operator+ (const WithUE<T>& op) const {
00280 return WithUE<T>(WithUnknown<WithError<T> >::operator+ (op));
00281 }
00282
00283 template<typename T>
00284 WithUE<T> WithUE<T>::operator- (const WithUE<T>& op) const {
00285 return WithUE<T>(WithUnknown<WithError<T> >::operator- (op));
00286 }
00287
00288 template<typename T>
00289 WithUE<T> WithUE<T>::operator* (const WithUE<T>& op) const {
00290 return WithUE<T>(WithUnknown<WithError<T> >::operator* (op));
00291 }
00292
00293 template<typename T>
00294 WithUE<T> WithUE<T>::operator/ (const WithUE<T>& op) const {
00295 return WithUE<T>(WithUnknown<WithError<T> >::operator/ (op));
00296 }
00297
00298 template<typename T>
00299 bool WithUE<T>::operator== (const WithUE<T>& op) const {
00300 if ( &op != this)
00301 return WithUnknown<WithError<T> >::operator== (op);
00302 return true;
00303 }
00304
00305 template<typename T>
00306 bool WithUE<T>::operator!= (const WithUE<T>& op) const {
00307 return !(*this == op);
00308 }
00309
00310 template<typename T>
00311 bool WithUE<T>::operator< (const WithUE<T>& op) const {
00312 return WithUnknown<WithError<T> >::operator< (op);
00313 }
00314
00315 template<typename T>
00316 bool WithUE<T>::operator<= (const WithUE<T>& op) const {
00317 return WithUnknown<WithError<T> >::operator<= (op);
00318 }
00319
00320 template<typename T>
00321 bool WithUE<T>::operator> (const WithUE<T>& op) const {
00322 return WithUnknown<WithError<T> >::operator> (op);
00323 }
00324
00325 template<typename T>
00326 bool WithUE<T>::operator>= (const WithUE<T>& op) const {
00327 return WithUnknown<WithError<T> >::operator>= (op);
00328 }
00329
00330 template<typename T>
00331 bool WithUE<T>::operator! () const {
00332 return WithUnknown<WithError<T> >::operator! ();
00333 }
00334
00335 template <typename T>
00336 WithUE<T>& WithUE<T>::operator++ () {
00337 WithUnknown<WithError<T> >::operator++ ();
00338 return *this;
00339 }
00340
00341 template <typename T>
00342 WithUE<T>& WithUE<T>::operator-- () {
00343 WithUnknown<WithError<T> >::operator-- ();
00344 return *this;
00345 }
00346
00347 template <typename T>
00348 WithUE<T> WithUE<T>::operator- () const {
00349 return WithUE<T>(WithUnknown<WithError<T> >::operator- ());
00350 }
00351
00352 template<typename T>
00353 WithUE<T>& WithUE<T>::improve(const WithUE<T>& op) {
00354 if (isKnown() && op.isKnown())
00355 WithUnknown<WithError<T> >::operator=(WithUnknown<WithError<T> >::getValue().improve(op.getValueWithError()));
00356 else if (op.isKnown()) {
00357 WithUnknown<WithError<T> >::operator=(op.getValue());
00358 }
00359 return *this;
00360 }
00361
00362 template<typename T>
00363 WithUE<T>& WithUE<T>::disimprove(const WithUE<T>& op) {
00364 if (isKnown() && op.isKnown()) {
00365 WithUnknown<WithError<T> >::operator=(WithUnknown<WithError<T> >::getValue().disimprove(op.getValueWithError()));
00366 } else if (op.isKnown()) {
00367 WithUnknown<WithError<T> >::operator=(op.getValue());
00368 }
00369 return *this;
00370 }
00371
00372 template<typename T>
00373 WithUE<T>& WithUE<T>::makeUnknown() {
00374 WithUnknown<WithError<T> >::makeUnknown();
00375 return *this;
00376 }
00377
00378 template<typename T>
00379 bool WithUE<T>::isKnown() const {
00380 return WithUnknown<WithError<T> >::isKnown();
00381 }
00382
00383 template<typename T>
00384 bool WithUE<T>::isUnknown() const {
00385 return WithUnknown<WithError<T> >::isUnknown();
00386 }
00387
00388 template<typename T>
00389 T WithUE<T>::getValue() const throw(UnknownValueException) {
00390 return WithUnknown<WithError<T> >::getValue().getValue();
00391 }
00392
00393 template<typename T>
00394 T WithUE<T>::getMin() const throw(UnknownValueException) {
00395 return WithUnknown<WithError<T> >::getValue().getMin();
00396 }
00397
00398 template<typename T>
00399 T WithUE<T>::getMax() const throw(UnknownValueException) {
00400 return WithUnknown<WithError<T> >::getValue().getMax();
00401 }
00402
00403 template<typename T>
00404 T WithUE<T>::getMinus() const throw(UnknownValueException) {
00405 return WithUnknown<WithError<T> >::getValue().getMinus();
00406 }
00407
00408 template<typename T>
00409 T WithUE<T>::getPlus() const throw (UnknownValueException) {
00410 return WithUnknown<WithError<T> >::getValue().getPlus();
00411 }
00412
00413 template<typename T>
00414 WithError<T>
00415 WithUE<T>::getValueWithError() const throw (UnknownValueException) {
00416 return WithUnknown<WithError<T> >::getValue();
00417 }
00418
00419 template<typename T>
00420 WithUnknown<T> WithUE<T>::getValueWithUnknown() const {
00421 if (isKnown())
00422 return WithUnknown<T>(getValue());
00423 return WithUnknown<T>();
00424 }
00425
00426 template<typename T>
00427 WithUE<T> operator- (const T& op1, const WithUE<T>& op2) {
00428 return -(op2) + op1;
00429 }
00430
00431 template<typename T>
00432 WithUE<T> operator- (const WithUnknown<T>& op1, const WithUE<T>& op2) {
00433 return -(op2) + op1;
00434 }
00435
00436 template<typename T>
00437 WithUE<T> operator- (const WithError<T>& op1, const WithUE<T>& op2) {
00438 return -(op2) + op1;
00439 }
00440
00441 template<typename T>
00442 WithUE<T> operator- (const WithUnknown<WithError<T> >& op1,
00443 const WithUE<T>& op2) {
00444 return -(op2) + op1;
00445 }
00446
00447 template<typename T>
00448 WithUE<T> operator+ (const T& op1, const WithUE<T>& op2) {
00449 return op2 + op1;
00450 }
00451
00452 template<typename T>
00453 WithUE<T> operator+ (const WithUnknown<T>& op1, const WithUE<T>& op2) {
00454 return op2 + op1;
00455 }
00456
00457 template<typename T>
00458 WithUE<T> operator+ (const WithError<T>& op1, const WithUE<T>& op2) {
00459 return op2 + op1;
00460 }
00461
00462 template<typename T>
00463 WithUE<T> operator+ (const WithUnknown<WithError<T> >& op1,
00464 const WithUE<T>& op2) {
00465 return op2 + op1;
00466 }
00467
00468 template<typename T>
00469 WithUE<T> operator* (const T& op1, const WithUE<T>& op2) {
00470 return op2 * op1;
00471 }
00472
00473 template<typename T>
00474 WithUE<T> operator* (const WithUnknown<T>& op1, const WithUE<T>& op2) {
00475 return op2 * op1;
00476 }
00477
00478 template<typename T>
00479 WithUE<T> operator* (const WithError<T>& op1, const WithUE<T>& op2) {
00480 return op2 * op1;
00481 }
00482
00483 template<typename T>
00484 WithUE<T> operator* (const WithUnknown<WithError<T> >& op1,
00485 const WithUE<T>& op2) {
00486 return op2 * op1;
00487 }
00488
00489 template<typename T>
00490 WithUE<T> operator/ (const T& op1, const WithUE<T>& op2) {
00491 return WithUE<T>(op1) / op2;
00492 }
00493
00494 template<typename T>
00495 WithUE<T> operator/ (const WithUnknown<T>& op1, const WithUE<T>& op2) {
00496 return WithUE<T>(op1) / op2;
00497 }
00498
00499 template<typename T>
00500 WithUE<T> operator/ (const WithError<T>& op1, const WithUE<T>& op2) {
00501 return WithUE<T>(op1) / op2;
00502 }
00503
00504 template<typename T>
00505 WithUE<T> operator/ (const WithUnknown<WithError<T> >& op1,
00506 const WithUE<T>& op2) {
00507 return WithUE<T>(op1) / op2;
00508 }
00509
00510 template<typename T>
00511 bool operator==(const T& op1, const WithUE<T> op2) {
00512 return op2 == op1;
00513 }
00514
00515 template<typename T>
00516 bool operator==(const WithUnknown<T>& op1, const WithUE<T> op2) {
00517 return op2 == op1;
00518 }
00519
00520 template<typename T>
00521 bool operator==(const WithError<T>& op1, const WithUE<T> op2) {
00522 return op2 == op1;
00523 }
00524
00525 template<typename T>
00526 bool operator==(const WithUnknown<WithError<T> >& op1, const WithUE<T> op2) {
00527 return op2 == op1;
00528 }
00529
00530 template<typename T>
00531 bool operator!=(const T& op1, const WithUE<T> op2) {
00532 return op2 != op1;
00533 }
00534
00535 template<typename T>
00536 bool operator!=(const WithUnknown<T>& op1, const WithUE<T> op2) {
00537 return op2 != op1;
00538 }
00539
00540 template<typename T>
00541 bool operator!=(const WithError<T>& op1, const WithUE<T> op2) {
00542 return op2 != op1;
00543 }
00544
00545 template<typename T>
00546 bool operator!=(const WithUnknown<WithError<T> >& op1, const WithUE<T> op2) {
00547 return op2 != op1;
00548 }
00549
00550 template<typename T>
00551 bool operator<(const T& op1, const WithUE<T> op2) {
00552 return op2 > op1;
00553 }
00554
00555 template<typename T>
00556 bool operator<(const WithUnknown<T>& op1, const WithUE<T> op2) {
00557 return op2 > op1;
00558 }
00559
00560 template<typename T>
00561 bool operator<(const WithError<T>& op1, const WithUE<T> op2) {
00562 return op2 > op1;
00563 }
00564
00565 template<typename T>
00566 bool operator<(const WithUnknown<WithError<T> >& op1, const WithUE<T> op2) {
00567 return op2 > op1;
00568 }
00569
00570 template<typename T>
00571 bool operator<=(const T& op1, const WithUE<T> op2) {
00572 return op2 >= op1;
00573 }
00574
00575 template<typename T>
00576 bool operator<=(const WithUnknown<T>& op1, const WithUE<T> op2) {
00577 return op2 >= op1;
00578 }
00579
00580 template<typename T>
00581 bool operator<=(const WithError<T>& op1, const WithUE<T> op2) {
00582 return op2 >= op1;
00583 }
00584
00585 template<typename T>
00586 bool operator<=(const WithUnknown<WithError<T> >& op1, const WithUE<T> op2) {
00587 return op2 >= op1;
00588 }
00589
00590 template<typename T>
00591 bool operator>(const T& op1, const WithUE<T> op2) {
00592 return op2 < op1;
00593 }
00594
00595 template<typename T>
00596 bool operator>(const WithUnknown<T>& op1, const WithUE<T> op2) {
00597 return op2 < op1;
00598 }
00599
00600 template<typename T>
00601 bool operator>(const WithError<T>& op1, const WithUE<T> op2) {
00602 return op2 < op1;
00603 }
00604
00605 template<typename T>
00606 bool operator>(const WithUnknown<WithError<T> >& op1, const WithUE<T> op2) {
00607 return op2 < op1;
00608 }
00609
00610 template<typename T>
00611 bool operator>=(const T& op1, const WithUE<T> op2) {
00612 return op2 <= op1;
00613 }
00614
00615 template<typename T>
00616 bool operator>=(const WithUnknown<T>& op1, const WithUE<T> op2) {
00617 return op2 <= op1;
00618 }
00619
00620 template<typename T>
00621 bool operator>=(const WithError<T>& op1, const WithUE<T> op2) {
00622 return op2 <= op1;
00623 }
00624
00625 template<typename T>
00626 bool operator>=(const WithUnknown<WithError<T> >& op1, const WithUE<T> op2) {
00627 return op2 <= op1;
00628 }
00629
00630 template<typename T>
00631 std::ostream& operator <<(std::ostream& os, const WithUE<T>& obj) {
00632 #ifdef UE_HEAD
00633 if (obj.isKnown())
00634 return os << UE_HEAD << "(" << toString(obj.getValueWithError()) << ")";
00635 else
00636 return os << UE_HEAD << "(" << toString(obj.getValueWithUnknown()) << ")";
00637 #else
00638 if (obj.isKnown())
00639 return os << toString(obj.getValueWithError());
00640 else
00641 return os << toString(obj.getValueWithUnknown());
00642 #endif
00643 }
00644
00645 template<typename T>
00646 const std::string toString(const WithUE<T>& obj) {
00647 #ifdef UE_HEAD
00648 if (obj.isKnown())
00649 return UE_HEAD + "(" + toString(obj.getValueWithError()) + ")";
00650 else
00651 return UE_HEAD + "(" + toString(obj.getValueWithUnknown()) + ")";
00652 #else
00653 if (obj.isKnown())
00654 return toString(obj.getValueWithError());
00655 else
00656 return toString(obj.getValueWithUnknown());
00657 #endif
00658 }
00659
00660 template<typename T>
00661 WithUE<T> sqr(const WithUE<T>& op) {
00662 if ( op.isUnknown() )
00663 return WithUE<T>();
00664 else
00665 return WithUE<T>(sqr(op.getValueWithError()));
00666 }
00667
00668 template<typename T>
00669 WithUE<T> sqrt(const WithUE<T>& op) {
00670 if ( op.isUnknown() )
00671 return WithUE<T>();
00672 else
00673 return WithUE<T>(sqrt(op.getValueWithError()));
00674 }
00675
00676
00677 RS_END_NAMESPACE
00678
00679 #endif // !WITHUE_H