📄 eointbounds.h
字号:
/** Simple bounds = minimum */ eoIntBelowBound(long int _min=0) : repMinimum(_min) {} // accessors virtual long int minimum() const { return repMinimum; } virtual long int maximum() const { throw std::logic_error("Trying to get maximum of eoIntBelowBound"); } virtual long int range() const { throw std::logic_error("Trying to get range of eoIntBelowBound"); } virtual double uniform(eoRng & _rng = eo::rng) const { throw std::logic_error("Trying to generate uniform values in eoIntBelowBound"); } virtual long int random(eoRng & _rng = eo::rng) const { throw std::logic_error("Trying to generate uniform values in eoIntBelowBound"); } // description virtual bool isBounded(void) const {return false;} virtual bool hasNoBoundAtAll(void) const {return false;} virtual bool isMinBounded(void) const {return true;} virtual bool isMaxBounded(void) const {return false;} // says if a given double is within the bounds virtual bool isInBounds(double _r) const { if (_r < repMinimum) return false; return true; } // folds a value into bounds virtual void foldsInBounds(double & _r) const { // easy as a pie: symmetry w.r.t. minimum if (_r < repMinimum) // nothing to do otherwise _r = 2*repMinimum - _r; return ; } // truncates to the bounds virtual void truncate(double & _r) const { if (_r < repMinimum) _r = repMinimum; return; } // methods from eoPersistent /** * Read object. * @param _is A std::istream. * but reading should not be done here, because of bound problems * see eoIntVectorBounds */ virtual void readFrom(std::istream& _is) { throw std::runtime_error("Should not use eoIntBelowBound::readFrom"); } /** * Write object. It's called printOn since it prints the object on a stream. * @param _os A std::ostream. */ virtual void printOn(std::ostream& _os) const { _os << "[" << repMinimum << ",+inf]"; } /** for memory managements - ugly */ virtual eoIntBounds * dup() const { return new eoIntBelowBound(*this); }private : long int repMinimum;};/**An eoIntBound bounded from above only*/class eoIntAboveBound : public eoIntBounds{public : virtual ~eoIntAboveBound(){} /** Simple bounds = minimum */ eoIntAboveBound(long int _max=0) : repMaximum(_max) {} // accessors virtual long int maximum() const { return repMaximum; } virtual long int minimum() const { throw std::logic_error("Trying to get minimum of eoIntAboveBound"); } virtual long int range() const { throw std::logic_error("Trying to get range of eoIntAboveBound"); } virtual double uniform(eoRng & _rng = eo::rng) const { throw std::logic_error("Trying to generate uniform values in eoIntAboveBound"); } virtual long int random(eoRng & _rng = eo::rng) const { throw std::logic_error("Trying to generate uniform values in eoIntAboveBound"); } // description virtual bool isBounded(void) const {return false;} virtual bool hasNoBoundAtAll(void) const {return false;} virtual bool isMinBounded(void) const {return false;} virtual bool isMaxBounded(void) const {return true;} // says if a given double is within the bounds virtual bool isInBounds(double _r) const { if (_r > repMaximum) return false; return true; } // folds a value into bounds virtual void foldsInBounds(double & _r) const { // easy as a pie: symmetry w.r.t. maximum if (_r > repMaximum) // nothing to do otherwise _r = 2*repMaximum - _r; return ; } // truncates to the bounds virtual void truncate(double & _r) const { if (_r > repMaximum) _r = repMaximum; return; } // methods from eoPersistent /** * Read object. * @param _is A std::istream. * but reading should not be done here, because of bound problems * see eoIntVectorBounds */ virtual void readFrom(std::istream& _is) { throw std::runtime_error("Should not use eoIntAboveBound::readFrom"); } /** * Write object. It's called printOn since it prints the object on a stream. * @param _os A std::ostream. */ virtual void printOn(std::ostream& _os) const { _os << "[-inf," << repMaximum << "]"; } /** for memory managements - ugly */ virtual eoIntBounds * dup() const { return new eoIntAboveBound(*this); }private : long int repMaximum;};//////////////////////// tentative for a general BOUND class that is constructed from a string/** A class that encapsulate all possible eoIntBounds. * Mandatory in order to read through the parser */class eoGeneralIntBounds : public eoIntBounds{public: /** Ctor: from a string, chooses the type of bound */ eoGeneralIntBounds(std::string _s = "[-infinity,+infinity]") { repBound = getBoundsFromString(_s); } /** Need a Cpy Ctor because we are allocating memory */ eoGeneralIntBounds(const eoGeneralIntBounds & _b) : eoIntBounds(_b) { // replicate the embedded bound (I'm pretty sure there is another // way to do that !!! bool minBounded = _b.isMinBounded(); bool maxBounded = _b.isMaxBounded(); long int minimum, maximum; const eoIntBounds & bb = _b.theBounds(); if (minBounded) minimum = bb.minimum(); if (maxBounded) maximum = bb.maximum(); if (minBounded && maxBounded) repBound = new eoIntInterval(minimum, maximum); else if (!minBounded && !maxBounded) // no bound at all repBound = new eoIntNoBounds; else if (!minBounded && maxBounded) repBound = new eoIntAboveBound(maximum); else if (minBounded && !maxBounded) repBound = new eoIntBelowBound(minimum); } eoGeneralIntBounds& operator=(const eoGeneralIntBounds& _b) { // replicate the embedded bound (I'm pretty sure there is another // way to do that !!! bool minBounded = _b.isMinBounded(); bool maxBounded = _b.isMaxBounded(); long int minimum, maximum; const eoIntBounds & bb = _b.theBounds(); if (minBounded) minimum = bb.minimum(); if (maxBounded) maximum = bb.maximum(); // first delete the embedded bounds if necessary if (repBound) delete repBound; // now reallocate if (minBounded && maxBounded) repBound = new eoIntInterval(minimum, maximum); else if (!minBounded && !maxBounded) // no bound at all repBound = new eoIntNoBounds; else if (!minBounded && maxBounded) repBound = new eoIntAboveBound(maximum); else if (minBounded && !maxBounded) repBound = new eoIntBelowBound(minimum); return (*this); } /** Need a Dtor because we allocate an actual bound */ ~eoGeneralIntBounds() { delete repBound; } ///// and now all methods from the embedded bounds /** Self-Test: true if ***both*** a min and a max */ virtual bool isBounded(void) const {return repBound->isBounded();} /** Self-Test: true if no min ***and*** no max * hence no further need to test/truncate/fold anything */ virtual bool hasNoBoundAtAll(void) const {return repBound->hasNoBoundAtAll();} /** Self-Test: bounded from below??? */ virtual bool isMinBounded(void) const {return repBound->isMinBounded();} /** Self-Test: bounded from above??? */ virtual bool isMaxBounded(void) const {return repBound->isMaxBounded();} /** Test on a value: is it in bounds? */ virtual bool isInBounds(double _x) const {return repBound->isInBounds(_x);} /** Put value back into bounds - by folding back and forth */ virtual void foldsInBounds(double & _x) const {return repBound->foldsInBounds(_x);} /** Put value back into bounds - by truncating to a boundary value */ virtual void truncate(double & _x) const {return repBound->truncate(_x);} /** get minimum value * @std::exception if does not exist */ virtual long int minimum() const {return repBound->minimum();} /** get maximum value * @std::exception if does not exist */ virtual long int maximum() const {return repBound->maximum();} /** get range * @std::exception if unbounded */ virtual long int range() const {return repBound->range();} /** random generator of uniform doubles in bounds * @std::exception if unbounded */ virtual double uniform(eoRng & _rng = eo::rng) const {return repBound->uniform();} /** random generator of uniform ints in bounds * @std::exception if unbounded */ virtual long int random(eoRng & _rng = eo::rng) const {return repBound->random();} /** for memory managements - ugly */ virtual eoIntBounds * dup() const {return repBound->dup();} /** for efficiency, it's better to use the embedded boud directly */ const eoIntBounds & theBounds() const { return *repBound;} /** don't forget the printOn method - * again that of the embedded bound */ virtual void printOn(std::ostream& _os) const { repBound->printOn(_os); } /** no readFrom ??? Have to check that later */ virtual void readFrom(std::istream& _is) { std::string s; _is >> s; if (repBound) delete repBound; repBound = getBoundsFromString(s); }private: // reading from a string eoIntBounds * getBoundsFromString(std::string); eoIntBounds * repBound;};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -