home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 41 / IOPROG_41.ISO / soft / c++ / NUMCPP11.ZIP / funcobj.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-09  |  3.3 KB  |  111 lines

  1. //===================================================================
  2. // funcobj.hpp
  3. //
  4. // Version 1.1
  5. //
  6. // Written by:
  7. //   Brent Worden
  8. //   WordenWare
  9. //   email:  Brent@Worden.org
  10. //
  11. // Copyright (c) 1998-1999 WordenWare
  12. //
  13. // Created:  August 28, 1998
  14. // Revised:  April 10, 1999
  15. //===================================================================
  16.  
  17. #ifndef _FUNCOBJ_HPP_
  18. #define _FUNCOBJ_HPP_
  19.  
  20. #include <functional>
  21.  
  22. #include "numerics.h"
  23.  
  24. NUM_BEGIN
  25.  
  26. template<class T>
  27. class BiggerThan
  28. //-------------------------------------------------------------------
  29. // Predicate object used to test if a given value is larger than
  30. // testValue.
  31. //-------------------------------------------------------------------
  32. {
  33. public:
  34.     BiggerThan(const T& x)
  35.     //---------------------------------------------------------------
  36.     // Create a BiggerThan object with test value x.
  37.     //---------------------------------------------------------------
  38.     : _testValue(x) {}
  39.  
  40.     bool operator()(const T& value)
  41.     //---------------------------------------------------------------
  42.     // Return true if value is greater than this object, false
  43.     // otherwise.
  44.     //---------------------------------------------------------------
  45.     { return value > _testValue; }
  46.  
  47. private:
  48.     T _testValue; // test value
  49. };
  50.  
  51. template<class T>
  52. class IncGen
  53. //-------------------------------------------------------------------
  54. // Generator object used to create a sequence of values starting at
  55. // init and increasing by some increment.
  56. //-------------------------------------------------------------------
  57. {
  58. public:
  59.     IncGen(const T& init, const T& inc = T(1))
  60.     //---------------------------------------------------------------
  61.     // Create a sequence starting at init, with increment inc.
  62.     //---------------------------------------------------------------
  63.     : _start(init), _increment(inc) {}
  64.  
  65.     const T& operator()()
  66.     //---------------------------------------------------------------
  67.     // Return next element in sequence.
  68.     //---------------------------------------------------------------
  69.     { _start += _increment; return _start - _increment; }
  70.  
  71. private:
  72.     T _start;     // next element in sequence.
  73.     T _increment; // increment
  74. };
  75.  
  76. template<class T>
  77. class ScaleValue
  78. //-------------------------------------------------------------------
  79. // Function object to scale values by a given factor.
  80. //-------------------------------------------------------------------
  81. {
  82. public:
  83.     ScaleValue(const T& s)
  84.     //---------------------------------------------------------------
  85.     // Create a scaleValue object with scaling factor s.
  86.     //---------------------------------------------------------------
  87.     : _scale(s) {}
  88.  
  89.     T operator()(const T& value)
  90.     //---------------------------------------------------------------
  91.     // Return the scaled value.
  92.     //---------------------------------------------------------------
  93.     { return value * _scale; }
  94.  
  95. private:
  96.     T _scale; // scaling factor
  97. };
  98.  
  99. NUM_END
  100.  
  101. #endif
  102.  
  103. //===================================================================
  104. // Revision History
  105. //
  106. // Version 1.0 - 08/28/1998 - New.
  107. // Version 1.1 - 04/10/1999 - Added Numerics namespace.
  108. //                            Added scaleValue object.
  109. //                            Added increment parameter to incGen.
  110. //===================================================================
  111.