Line data Source code
1 : #ifndef OPENDDS_DCPS_FIBONACCI_SEQUENCE_H 2 : #define OPENDDS_DCPS_FIBONACCI_SEQUENCE_H 3 : 4 : #if !defined (ACE_LACKS_PRAGMA_ONCE) 5 : # pragma once 6 : #endif /* ACE_LACKS_PRAGMA_ONCE */ 7 : 8 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 9 : 10 : namespace OpenDDS { 11 : namespace DCPS { 12 : 13 : template <typename T> 14 : class FibonacciSequence 15 : { 16 : public: 17 12 : explicit FibonacciSequence(const T& f1, const T& f1_minus_1 = T(0)) 18 8 : : f_n_(f1) 19 8 : , f_n_minus_1_(f1_minus_1) 20 8 : {} 21 : 22 71 : T get() const 23 : { 24 71 : return f_n_; 25 : } 26 : 27 42 : void advance(const T& fmax = T(0)) 28 : { 29 28 : const T f_n_plus_1 = f_n_minus_1_ + f_n_; 30 28 : f_n_minus_1_ = f_n_; 31 28 : f_n_ = f_n_plus_1; 32 28 : if (fmax != T(0)) { 33 4 : f_n_ = (std::min)(f_n_, fmax); 34 : } 35 28 : } 36 : 37 6 : void set(const T& f1, const T& f1_minus_1 = T(0)) 38 : { 39 4 : f_n_ = f1; 40 4 : f_n_minus_1_ = f1_minus_1; 41 4 : } 42 : 43 : private: 44 : T f_n_; 45 : T f_n_minus_1_; 46 : }; 47 : 48 : } // namespace DCPS 49 : } // namespace OpenDDS 50 : 51 : OPENDDS_END_VERSIONED_NAMESPACE_DECL 52 : 53 : #endif