[PATCH] min/max numeric overloads

Nathan Myers ncm-nospam@cantrip.org
Wed Feb 5 22:35:00 GMT 2003


I have attached a patch for include/bits/stl_algobase.h, to overload 
the definitions of std::min and std::max applied to numeric types, 
so that they do not produce unnecessary lvalue references (and
corresponding unnecessary memory references!) when called.

This patch was the point of the extensive testing in the patch to
min_max.cc; since the code is no longer common, each version needs 
its own tests.

Nathan Myers
ncm-nospam@cantrip.org
-------------- next part --------------
Index: stl_algobase.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/include/bits/stl_algobase.h,v
retrieving revision 1.20
diff -r1.20 stl_algobase.h
140c140
<    *  @return   The lesser of the parameters.
---
>    *  @return   The lesser of the parameters, according to operator<.
154a155,158
>  
>   inline int
>   min(int __a, int __b)
>     { if (__b < __a) return __b; return __a; }
155a160,217
>   inline unsigned int
>   min(unsigned int __a, unsigned int __b)
>     { if (__b < __a) return __b; return __a; }
>   
>   inline long
>   min(long __a, long __b)
>     { if (__b < __a) return __b; return __a; }
>   
>   inline unsigned long
>   min(unsigned long __a, unsigned long __b)
>     { if (__b < __a) return __b; return __a; }
>   
> #ifdef _GLIBCPP_USE_LONG_LONG
>   inline long long
>   min(long long __a, long long __b)
>     { if (__b < __a) return __b; return __a; }
>   
>   inline unsigned long long
>   min(unsigned long long __a, unsigned long long __b)
>     { if (__b < __a) return __b; return __a; }
> #endif
> 
>   inline short
>   min(short __a, short __b)
>     { if (__b < __a) return __b; return __a; }
>  
>   inline unsigned short
>   min(unsigned short __a, unsigned short __b)
>     { if (__b < __a) return __b; return __a; }
>  
>   inline char
>   min(char __a, char __b)
>     { if (__b < __a) return __b; return __a; }
>  
>   inline unsigned char
>   min(unsigned char __a, unsigned char __b)
>     { if (__b < __a) return __b; return __a; }
>  
>   inline signed char
>   min(signed char __a, signed char __b)
>     { if (__b < __a) return __b; return __a; }
>  
>   inline wchar_t
>   min(wchar_t __a, wchar_t __b)
>     { if (__b < __a) return __b; return __a; }
>  
>   inline float
>   min(float __a, float __b)
>     { if (__b < __a) return __b; return __a; }
>  
>   inline double
>   min(double __a, double __b)
>     { if (__b < __a) return __b; return __a; }
>  
>   inline long double
>   min(long double __a, long double __b)
>     { if (__b < __a) return __b; return __a; }
>  
160c222
<    *  @return   The greater of the parameters.
---
>    *  @return   The greater of the parameters, according to operator<.
174a237,299
>  
>   inline int
>   max(int __a, int __b)
>     { if (__a < __b) return __b; return __a; }
> 
>   inline unsigned int
>   max(unsigned int __a, unsigned int __b)
>     { if (__a < __b) return __b; return __a; }
>   
>   inline long
>   max(long __a, long __b)
>     { if (__a < __b) return __b; return __a; }
>   
>   inline unsigned long
>   max(unsigned long __a, unsigned long __b)
>     { if (__a < __b) return __b; return __a; }
>   
> #ifdef _GLIBCPP_USE_LONG_LONG
>   inline long long
>   max(long long __a, long long __b)
>     { if (__a < __b) return __b; return __a; }
>   
>   inline unsigned long long
>   max(unsigned long long __a, unsigned long long __b)
>     { if (__a < __b) return __b; return __a; }
> #endif
> 
>   inline short
>   max(short __a, short __b)
>     { if (__a < __b) return __b; return __a; }
>  
>   inline unsigned short
>   max(unsigned short __a, unsigned short __b)
>     { if (__a < __b) return __b; return __a; }
>  
>   inline char
>   max(char __a, char __b)
>     { if (__a < __b) return __b; return __a; }
>  
>   inline unsigned char
>   max(unsigned char __a, unsigned char __b)
>     { if (__a < __b) return __b; return __a; }
>  
>   inline signed char
>   max(signed char __a, signed char __b)
>     { if (__a < __b) return __b; return __a; }
>  
>   inline wchar_t
>   max(wchar_t __a, wchar_t __b)
>     { if (__a < __b) return __b; return __a; }
>  
>   inline float
>   max(float __a, float __b)
>     { if (__a < __b) return __b; return __a; }
>  
>   inline double
>   max(double __a, double __b)
>     { if (__a < __b) return __b; return __a; }
>  
>   inline long double
>   max(long double __a, long double __b)
>     { if (__a < __b) return __b; return __a; }
>  
181c306
<    *  @return   The lesser of the parameters.
---
>    *  @return   The lesser of the parameters, according to comp.
193a319,395
>   template <typename _Compare>
>     inline int
>     min(int __a, int __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
> 
>   template <typename _Compare>
>     inline unsigned int
>     min(unsigned int __a, unsigned int __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>     
>   template <typename _Compare>
>     inline long
>     min(long __a, long __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>     
>   template <typename _Compare>
>     inline unsigned long
>     min(unsigned long __a, unsigned long __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>     
> #ifdef _GLIBCPP_USE_LONG_LONG
>   template <typename _Compare>
>     inline long long
>     min(long long __a, long long __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>     
>   template <typename _Compare>
>     inline unsigned long long
>     min(unsigned long long __a, unsigned long long __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
> #endif
> 
>   template <typename _Compare>
>     inline short
>     min(short __a, short __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline unsigned short
>     min(unsigned short __a, unsigned short __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline char
>     min(char __a, char __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline unsigned char
>     min(unsigned char __a, unsigned char __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline signed char
>     min(signed char __a, signed char __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline wchar_t
>     min(wchar_t __a, wchar_t __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline float
>     min(float __a, float __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline double
>     min(double __a, double __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline long double
>     min(long double __a, long double __b, _Compare __comp)
>       { if (__comp(__b, __a)) return __b; return __a; }
>    
199c401
<    *  @return   The greater of the parameters.
---
>    *  @return   The greater of the parameters, according to comp.
210a413,490
> 
>   template <typename _Compare>
>     inline int
>     max(int __a, int __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
> 
>   template <typename _Compare>
>     inline unsigned int
>     max(unsigned int __a, unsigned int __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>     
>   template <typename _Compare>
>     inline long
>     max(long __a, long __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>     
>   template <typename _Compare>
>     inline unsigned long
>     max(unsigned long __a, unsigned long __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>     
> #ifdef _GLIBCPP_USE_LONG_LONG
>   template <typename _Compare>
>     inline long long
>     max(long long __a, long long __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>     
>   template <typename _Compare>
>     inline unsigned long long
>     max(unsigned long long __a, unsigned long long __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
> #endif
> 
>   template <typename _Compare>
>     inline short
>     max(short __a, short __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline unsigned short
>     max(unsigned short __a, unsigned short __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline char
>     max(char __a, char __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline unsigned char
>     max(unsigned char __a, unsigned char __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline signed char
>     max(signed char __a, signed char __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline wchar_t
>     max(wchar_t __a, wchar_t __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline float
>     max(float __a, float __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline double
>     max(double __a, double __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    
>   template <typename _Compare>
>     inline long double
>     max(long double __a, long double __b, _Compare __comp)
>       { if (__comp(__a, __b)) return __b; return __a; }
>    


More information about the Libstdc++ mailing list