You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.2 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/minmax.hpp (originally MLFwdEnm.h)
  3. *
  4. * Purpose: Definition of minimum() and maximum() template functions.
  5. *
  6. * Created: 11th April 2005
  7. * Updated: 10th August 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2010, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/util/minmax.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::minimum() and
  42. * stlsoft::maximum() function templates
  43. * (\ref group__library__utility "Utility" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_MINMAX
  46. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_MINMAX
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_MINMAX_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_MINMAX_MINOR 1
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_MINMAX_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_UTIL_HPP_MINMAX_EDIT 15
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  57. # include <stlsoft/stlsoft.h>
  58. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  59. /* /////////////////////////////////////////////////////////////////////////
  60. * Namespace
  61. */
  62. #ifndef _STLSOFT_NO_NAMESPACE
  63. namespace stlsoft
  64. {
  65. #endif /* _STLSOFT_NO_NAMESPACE */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Functions
  68. */
  69. /** \brief Determines the minimum of two values
  70. *
  71. * \ingroup group__library__utility
  72. *
  73. * \note This is supplied to avoid problems with libraries that \#define \c min
  74. */
  75. template <ss_typename_param_k T>
  76. inline T const& minimum(T const& lhs, T const& rhs)
  77. {
  78. return (lhs < rhs) ? lhs : rhs;
  79. }
  80. /** \brief Determines the maximum of two values
  81. *
  82. * \ingroup group__library__utility
  83. *
  84. * \note This is supplied to avoid problems with libraries that \#define \c min
  85. */
  86. template <ss_typename_param_k T>
  87. inline T const& maximum(T const& lhs, T const& rhs)
  88. {
  89. return (lhs < rhs) ? rhs : lhs;
  90. }
  91. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  92. template <
  93. ss_typename_param_k T
  94. >
  95. inline T const& maximum(
  96. T const& v0
  97. , T const& v1
  98. , T const& v2
  99. )
  100. {
  101. return maximum(maximum(v0, v1), v2);
  102. }
  103. template <
  104. ss_typename_param_k T
  105. >
  106. inline T const& minimum(
  107. T const& v0
  108. , T const& v1
  109. , T const& v2
  110. )
  111. {
  112. return minimum(minimum(v0, v1), v2);
  113. }
  114. template <
  115. ss_typename_param_k T
  116. >
  117. inline T const& maximum(
  118. T const& v0
  119. , T const& v1
  120. , T const& v2
  121. , T const& v3
  122. )
  123. {
  124. return maximum(maximum(v0, v1), maximum(v2, v3));
  125. }
  126. template <
  127. ss_typename_param_k T
  128. >
  129. inline T const& minimum(
  130. T const& v0
  131. , T const& v1
  132. , T const& v2
  133. , T const& v3
  134. )
  135. {
  136. return minimum(minimum(v0, v1), minimum(v2, v3));
  137. }
  138. template <
  139. ss_typename_param_k T
  140. >
  141. inline T const& maximum(
  142. T const& v0
  143. , T const& v1
  144. , T const& v2
  145. , T const& v3
  146. , T const& v4
  147. )
  148. {
  149. return maximum(maximum(maximum(v0, v1), maximum(v2, v3)), v4);
  150. }
  151. template <
  152. ss_typename_param_k T
  153. >
  154. inline T const& minimum(
  155. T const& v0
  156. , T const& v1
  157. , T const& v2
  158. , T const& v3
  159. , T const& v4
  160. )
  161. {
  162. return minimum(minimum(minimum(v0, v1), minimum(v2, v3)), v4);
  163. }
  164. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  165. /* ////////////////////////////////////////////////////////////////////// */
  166. #ifndef _STLSOFT_NO_NAMESPACE
  167. } // namespace stlsoft
  168. #endif /* _STLSOFT_NO_NAMESPACE */
  169. /* ////////////////////////////////////////////////////////////////////// */
  170. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_MINMAX */
  171. /* ///////////////////////////// end of file //////////////////////////// */