home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MINMAX.H < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  222 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  C macros for min() and max() on any C compiler.
  5. **
  6. **  C++ templates for min() and max() on any C++ compiler which supports
  7. **  templates.
  8. **
  9. **  Overloaded C++ functions for min() and max() on any C++ compiler which
  10. **  doesn't support templates.
  11. **
  12. **  If your compiler supports both C and C++, the appropriate behavior will
  13. **  be selected based on the value of the __cplusplus predefined macro.
  14. */
  15.  
  16. #ifndef MINMAX__H
  17. #define MINMAX__H
  18.  
  19. #undef min
  20. #undef max
  21.  
  22. /*
  23. **  The following 2 macros are used only for C++ compilers which lack
  24. **  templates. If you use a C++ compiler which supports templates, ignore
  25. **  the rest of this section. If you're compiling standard C, rather than
  26. **  C++, you may ignore the rest of this section.
  27. **
  28. **  Set SIGNED_DISTINCT to 1 for conforming C++ compilers that treat signed,
  29. **  unsigned, and "normal" types as 3 distinct entities. Set SIGNED_DISTINCT
  30. **  to 0 for older or non-conforming C++ compilers. To determine the proper
  31. **  setting for your compiler, compile MINMAX.H as a C++ source file using
  32. **  the "compile only" (usually -c) switch. If the compiler reports "already
  33. **  defined" errors, you probably need to change the value of SIGNED_DISTINCT
  34. **  to 0.
  35. **
  36. **  If you still get "already defined" errors after setting SIGNED_DISTINCT
  37. **  to 0, you may either have a truly broken compiler or have some obscure
  38. **  (e.g. Zortech/Symantec's -Ju or -Jm) switch set which causes this
  39. **  behavior. As a last resort, you can set UNSIGNED_DISTINCT to 0 as well.
  40. */
  41.  
  42. #define SIGNED_DISTINCT 1
  43. #define UNSIGNED_DISTINCT 1
  44.  
  45. /*
  46. **  We can only use templates with newer C++ compilers compiling C++ source
  47. */
  48.  
  49. #if defined(__cplusplus) && __cplusplus
  50.  #if  (defined(__SC__) && __SC__ >= 0x700) || \
  51.       (defined(_MSC_VER) && _MSC_VER > 800) || \
  52.       (defined(__WATCOMC__) && __WATCOMC__ >= 1000) || \
  53.       (defined(__BORLANDC__) && __BORLANDC__ >= 0x450)
  54.  
  55.   template<class T> inline T max(T a, T b) {return (a > b) ? a : b; };
  56.   template<class T> inline T min(T a, T b) {return (a < b) ? a : b; };
  57.  
  58.  #else /* no templates */
  59.  
  60.   /*
  61.   ** prototypes for overloaded max() functions
  62.   */
  63.  
  64.   inline char max(char a, char b)
  65.   {
  66.         return (a > b) ? a : b;
  67.   }
  68.   #if UNSIGNED_DISTINCT
  69.    inline unsigned char max(unsigned char a, unsigned char b)
  70.    {
  71.          return (a > b) ? a : b;
  72.    }
  73.   #endif
  74.   #if SIGNED_DISTINCT
  75.    inline signed char max(signed char a, signed char b)
  76.    {
  77.          return (a > b) ? a : b;
  78.    }
  79.   #endif
  80.  
  81.   inline short max(short a, short b)
  82.   {
  83.         return (a > b) ? a : b;
  84.   }
  85.   #if UNSIGNED_DISTINCT
  86.    inline unsigned short max(unsigned short a, unsigned short b)
  87.    {
  88.          return (a > b) ? a : b;
  89.    }
  90.   #endif
  91.   #if SIGNED_DISTINCT
  92.    inline signed short max(signed short a, signed short b)
  93.    {
  94.          return (a > b) ? a : b;
  95.    }
  96.   #endif
  97.  
  98.   inline int max(int a, int b)
  99.   {
  100.         return (a > b) ? a : b;
  101.   }
  102.   #if UNSIGNED_DISTINCT
  103.    inline unsigned int max(unsigned int a, unsigned int b)
  104.    {
  105.          return (a > b) ? a : b;
  106.    }
  107.   #endif
  108.   #if SIGNED_DISTINCT
  109.    inline signed int max(signed int a, signed int b)
  110.    {
  111.          return (a > b) ? a : b;
  112.    }
  113.   #endif
  114.  
  115.   inline long max(long a, long b)
  116.   {
  117.         return (a > b) ? a : b;
  118.   }
  119.   #if UNSIGNED_DISTINCT
  120.    inline unsigned long max(unsigned long a, unsigned long b)
  121.    {
  122.          return (a > b) ? a : b;
  123.    }
  124.   #endif
  125.   #if SIGNED_DISTINCT
  126.    inline signed long max(signed long a, signed long b)
  127.    {
  128.          return (a > b) ? a : b;
  129.    }
  130.   #endif
  131.  
  132.   inline float max(float a, float b) {return (a > b) ? a : b; }
  133.  
  134.   inline double max(double a, double b) {return (a > b) ? a : b; }
  135.  
  136.   /*
  137.   ** prototypes for overloaded min() functions
  138.   */
  139.  
  140.   inline char min(char a, char b)
  141.   {
  142.         return (a < b) ? a : b;
  143.   }
  144.   #if UNSIGNED_DISTINCT
  145.    inline unsigned char min(unsigned char a, unsigned char b)
  146.    {
  147.          return (a < b) ? a : b;
  148.    }
  149.   #endif
  150.   #if SIGNED_DISTINCT
  151.    inline signed char min(signed char a, signed char b)
  152.    {
  153.          return (a < b) ? a : b;
  154.    }
  155.   #endif
  156.  
  157.   inline short min(short a, short b)
  158.   {
  159.         return (a < b) ? a : b;
  160.   }
  161.   #if UNSIGNED_DISTINCT
  162.    inline unsigned short min(unsigned short a, unsigned short b)
  163.    {
  164.          return (a < b) ? a : b;
  165.    }
  166.   #endif
  167.   #if SIGNED_DISTINCT
  168.    inline signed short min(signed short a, signed short b)
  169.    {
  170.          return (a < b) ? a : b;
  171.    }
  172.   #endif
  173.  
  174.   inline int min(int a, int b)
  175.   {
  176.         return (a < b) ? a : b;
  177.   }
  178.   #if UNSIGNED_DISTINCT
  179.    inline unsigned int min(unsigned int a, unsigned int b)
  180.    {
  181.          return (a < b) ? a : b;
  182.    }
  183.   #endif
  184.   #if SIGNED_DISTINCT
  185.    inline signed int min(signed int a, signed int b)
  186.    {
  187.          return (a < b) ? a : b;
  188.    }
  189.   #endif
  190.  
  191.   inline long min(long a, long b)
  192.   {
  193.         return (a < b) ? a : b;
  194.   }
  195.   #if UNSIGNED_DISTINCT
  196.    inline unsigned long min(unsigned long a, unsigned long b)
  197.    {
  198.          return (a < b) ? a : b;
  199.    }
  200.   #endif
  201.   #if SIGNED_DISTINCT
  202.    inline signed long min(signed long a, signed long b)
  203.    {
  204.          return (a < b) ? a : b;
  205.    }
  206.   #endif
  207.  
  208.   inline float min(float a, float b) {return (a < b) ? a : b; }
  209.  
  210.   inline double min(double a, double b) {return (a < b) ? a : b; }
  211.  
  212.  #endif /* no templates */
  213.  
  214. #else /* standard C macros */
  215.  
  216.  #define min(x,y) (((x) <= (y)) ? (x) : (y))
  217.  #define max(x,y) (((x) >= (y)) ? (x) : (y))
  218.  
  219. #endif /* __cplusplus */
  220.  
  221. #endif /* MINMAX__H */
  222.