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.

225 lines
7.5 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/algorithms/collection.hpp
  3. *
  4. * Purpose: Whole collection 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/collection.hpp
  40. *
  41. * \brief [C++ only] Whole collection algorithms
  42. * (\ref group__library__algorithms "Algorithms" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_ALGORITHMS_HPP_COLLECTION
  45. #define STLSOFT_INCL_STLSOFT_ALGORITHMS_HPP_COLLECTION
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_ALGORITHMS_HPP_COLLECTION_MAJOR 3
  48. # define STLSOFT_VER_STLSOFT_ALGORITHMS_HPP_COLLECTION_MINOR 1
  49. # define STLSOFT_VER_STLSOFT_ALGORITHMS_HPP_COLLECTION_REVISION 1
  50. # define STLSOFT_VER_STLSOFT_ALGORITHMS_HPP_COLLECTION_EDIT 69
  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. #ifndef STLSOFT_INCL_STLSOFT_ALGORITHM_STD_HPP_ALT
  59. # include <stlsoft/algorithms/std/alt.hpp>
  60. #endif /* !STLSOFT_INCL_STLSOFT_ALGORITHM_STD_HPP_ALT */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #ifndef _STLSOFT_NO_NAMESPACE
  65. namespace stlsoft
  66. {
  67. #endif /* _STLSOFT_NO_NAMESPACE */
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Algorithms
  70. */
  71. /** \brief Invokes std::for_each() on the range of items in a container
  72. *
  73. * \ingroup group__library__algorithms
  74. *
  75. * \param container The container instance
  76. * \param value The value that to assign to each element in the container
  77. */
  78. template< ss_typename_param_k C
  79. , ss_typename_param_k V
  80. >
  81. // [[synesis:function:algorithm: fill_all(T<C> &container, T<T> const& value)]]
  82. inline void fill_all(C &container, V const& value)
  83. {
  84. fill(container.begin(), container.end(), value);
  85. }
  86. #ifdef STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
  87. /** \brief Invokes std::for_each() on the range of items in an array
  88. *
  89. * \ingroup group__library__algorithms
  90. *
  91. * \param ar The array
  92. * \param value The value to set to each element in the array
  93. */
  94. // [[synesis:function:algorithm: fill_all(T-A<T, N> ar, T<T> const& value)]]
  95. template< ss_typename_param_k T
  96. , ss_size_t N
  97. , ss_typename_param_k V
  98. >
  99. inline void fill_all(T (&ar)[N], V const& value)
  100. {
  101. fill(&ar[0], &ar[N], value);
  102. }
  103. #endif /* STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
  104. /** \brief Invokes std::for_each() on the range of items in a container
  105. *
  106. * \ingroup group__library__algorithms
  107. *
  108. * \param container The container instance
  109. * \param func The function to be applied to each element in the container
  110. */
  111. template< ss_typename_param_k C
  112. , ss_typename_param_k UF
  113. >
  114. // [[synesis:function:algorithm: for_all(T<C> &container, T<UF> func)]]
  115. inline UF for_all(C &container, UF func)
  116. {
  117. return std_for_each(container.begin(), container.end(), func);
  118. }
  119. #ifdef STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
  120. /** \brief Invokes std::for_each() on the range of items in an array
  121. *
  122. * \ingroup group__library__algorithms
  123. *
  124. * \param ar The array
  125. * \param func The function to be applied to each element in the array
  126. */
  127. // [[synesis:function:algorithm: for_all(T-A<T, N> ar, T<UF> func)]]
  128. template< ss_typename_param_k T
  129. , ss_size_t N
  130. , ss_typename_param_k UF
  131. >
  132. inline UF for_all(T (&ar)[N], UF func)
  133. {
  134. return std_for_each(&ar[0], &ar[N], func);
  135. }
  136. #endif /* STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
  137. /** \brief Invokes std::for_each() on the reverse range of items in a container
  138. *
  139. * \ingroup group__library__algorithms
  140. *
  141. * \param container The container instance
  142. * \param func The function to be applied to each element in the container
  143. */
  144. // [[synesis:function:algorithm: for_all_r(T<C> &container, T<UF> func)]]
  145. template< ss_typename_param_k C
  146. , ss_typename_param_k UF
  147. >
  148. inline UF for_all_r(C &container, UF func)
  149. {
  150. return std_for_each(container.rbegin(), container.rend(), func);
  151. }
  152. /** \brief Invokes std::copy() on all the items in a container
  153. *
  154. * \ingroup group__library__algorithms
  155. *
  156. * \param container The container instance
  157. * \param dest The output iterator to which each element will be copied
  158. */
  159. // [[synesis:function:algorithm: copy_all(T<C> &container, T<O> dest)]]
  160. template< ss_typename_param_k C
  161. , ss_typename_param_k O
  162. >
  163. inline O copy_all(C &container, O dest)
  164. {
  165. return std_copy(container.begin(), container.end(), dest);
  166. }
  167. #ifdef STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
  168. /** \brief Invokes std::copy() on the range of items in an array
  169. *
  170. * \ingroup group__library__algorithms
  171. *
  172. * \param ar The array
  173. * \param dest The output iterator to which each element will be copied
  174. */
  175. template< ss_typename_param_k T
  176. , ss_size_t N
  177. , ss_typename_param_k O
  178. >
  179. // [[synesis:function:algorithm: copy_all(T-A<T, N> ar, T<O> dest)]]
  180. inline O copy_all(T (&ar)[N], O dest)
  181. {
  182. return std_copy(&ar[0], &ar[N], dest);
  183. }
  184. #endif /* STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
  185. ////////////////////////////////////////////////////////////////////////////
  186. // Unit-testing
  187. #ifdef STLSOFT_UNITTEST
  188. # include "./unittest/collection_unittest_.h"
  189. #endif /* STLSOFT_UNITTEST */
  190. /* ////////////////////////////////////////////////////////////////////// */
  191. #ifndef _STLSOFT_NO_NAMESPACE
  192. } // namespace stlsoft
  193. #endif /* _STLSOFT_NO_NAMESPACE */
  194. /* ////////////////////////////////////////////////////////////////////// */
  195. #endif /* !STLSOFT_INCL_STLSOFT_ALGORITHMS_HPP_COLLECTION */
  196. /* ///////////////////////////// end of file //////////////////////////// */