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.

222 lines
7.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/algorithms/std/ext.hpp
  3. *
  4. * Purpose: Extensions to standard algorithms.
  5. *
  6. * Created: 17th January 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2009, 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/algorithms/std/ext.hpp
  40. *
  41. * \brief [C++ only] Extensions to standard algorithms
  42. * (\ref group__library__algorithms "Algorithms" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_ALGORITHMS_STD_HPP_EXT
  45. #define STLSOFT_INCL_STLSOFT_ALGORITHMS_STD_HPP_EXT
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_ALGORITHMS_STD_HPP_EXT_MAJOR 3
  48. # define STLSOFT_VER_STLSOFT_ALGORITHMS_STD_HPP_EXT_MINOR 2
  49. # define STLSOFT_VER_STLSOFT_ALGORITHMS_STD_HPP_EXT_REVISION 5
  50. # define STLSOFT_VER_STLSOFT_ALGORITHMS_STD_HPP_EXT_EDIT 72
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  56. # include <stlsoft/stlsoft.h>
  57. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  58. #ifdef STLSOFT_UNITTEST
  59. # if defined(STLSOFT_COMPILER_IS_BORLAND)
  60. # include <memory.h> // for memcpy()
  61. # else /* ? compiler */
  62. # include <string.h> // for memcpy()
  63. # endif /* compiler */
  64. #endif /* STLSOFT_UNITTEST */
  65. /* /////////////////////////////////////////////////////////////////////////
  66. * Namespace
  67. */
  68. #ifndef _STLSOFT_NO_NAMESPACE
  69. namespace stlsoft
  70. {
  71. #endif /* _STLSOFT_NO_NAMESPACE */
  72. /* /////////////////////////////////////////////////////////////////////////
  73. * Algorithms
  74. */
  75. /** \brief Copies elements from one range to another that satisfy a predicate
  76. *
  77. * \ingroup group__library__algorithms
  78. *
  79. * Copies each of the N elements in the source range [first, last) to the
  80. * destination range - of available length [dest, dest + N) - for which the
  81. * expression pred(*(i + n)) hold true, where i is the nth element in the
  82. * range
  83. *
  84. * \param first The start of the (unordered) sequence
  85. * \param last The (one past the) end point of the sequence
  86. * \param dest The output iterator to which the copies are written
  87. * \param pred The predicate used to determine whether the element in the input
  88. * sequence is to be written to the output iterator
  89. */
  90. template< ss_typename_param_k I
  91. , ss_typename_param_k O
  92. , ss_typename_param_k UP
  93. >
  94. // [[synesis:function:algorithm: copy_if(T<I> first, T<I> last, T<O> dest, T<UP> pred)]]
  95. inline O copy_if(I first, I last, O dest, UP pred)
  96. {
  97. for(; first != last; ++first)
  98. {
  99. if(pred(*first))
  100. {
  101. *dest++ = *first;
  102. }
  103. }
  104. return dest;
  105. }
  106. /** \brief Applies the function to all items in a range that satisfy a predicate
  107. *
  108. * \ingroup group__library__algorithms
  109. *
  110. * Applies the function f to each of the N elements in the source range [first, last)
  111. * for which the expression pred(*(i + n)) hold true, where i is the nth element in the
  112. * range
  113. *
  114. * \param first The start of the (unordered) sequence
  115. * \param last The (one past the) end point of the sequence
  116. * \param func The function type to be applied to each selected element
  117. * \param pred The predicate used to determine whether the function is to be applied to
  118. * the element in the input sequence
  119. */
  120. template< ss_typename_param_k I
  121. , ss_typename_param_k UF
  122. , ss_typename_param_k UP
  123. >
  124. // [[synesis:function:algorithm: for_each_if(T<I> first, T<I> last, T<UF> func, T<UP> pred)]]
  125. inline UF for_each_if(I first, I last, UF func, UP pred)
  126. {
  127. for(; first != last; ++first)
  128. {
  129. if(pred(*first))
  130. {
  131. func(*first);
  132. }
  133. }
  134. return func;
  135. }
  136. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  137. template< ss_typename_param_k I
  138. , ss_typename_param_k UF
  139. , ss_typename_param_k UP
  140. >
  141. // [[synesis:function:algorithm: for_each_ifnot(T<I> first, T<I> last, T<UF> func, T<UP> pred)]]
  142. inline UF for_each_ifnot(I first, I last, UF func, UP pred)
  143. {
  144. for(; first != last; ++first)
  145. {
  146. if(!pred(*first))
  147. {
  148. func(*first);
  149. }
  150. }
  151. return func;
  152. }
  153. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  154. /** \brief Sets the value of all items in a range that satisfy a predicate.
  155. *
  156. * \ingroup group__library__algorithms
  157. *
  158. * Assigns the value v to each of the N elements in the range [first, last) for
  159. * which the expression pred(*(i + n)) hold true, where i is the nth element in
  160. * the range
  161. *
  162. * \param first The start of the sequence
  163. * \param last The last of the sequence
  164. * \param value The value to be assigned to each selected element
  165. * \param pred The predicate used to determine whether the value is to be assigned
  166. * to the element in the input sequence
  167. */
  168. template< ss_typename_param_k O
  169. , ss_typename_param_k V
  170. , ss_typename_param_k UP
  171. >
  172. // [[synesis:function:algorithm: fill_if(T<O> first, T<O> last, T<V> const& value, T<UP> pred)]]
  173. inline void fill_if(O first, O last, V const& value, UP pred)
  174. {
  175. for(; first != last; ++first)
  176. {
  177. if(pred(*first))
  178. {
  179. *first = value;
  180. }
  181. }
  182. }
  183. ////////////////////////////////////////////////////////////////////////////
  184. // Unit-testing
  185. #ifdef STLSOFT_UNITTEST
  186. # include "./unittest/ext_unittest_.h"
  187. #endif /* STLSOFT_UNITTEST */
  188. /* ////////////////////////////////////////////////////////////////////// */
  189. #ifndef _STLSOFT_NO_NAMESPACE
  190. } // namespace stlsoft
  191. #endif /* _STLSOFT_NO_NAMESPACE */
  192. /* ////////////////////////////////////////////////////////////////////// */
  193. #endif /* !STLSOFT_INCL_STLSOFT_ALGORITHMS_STD_HPP_EXT */
  194. /* ///////////////////////////// end of file //////////////////////////// */