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.

339 lines
10 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: rangelib/salgorithms.hpp
  3. *
  4. * Purpose: Range-adapted Sequence algorithms.
  5. *
  6. * Created: 19th July 2005
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-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 rangelib/salgorithms.hpp Range-adapted Sequence algorithms.
  40. *
  41. * This file includes the definition of the following algorithms:
  42. *
  43. * - sr_accumulate()
  44. * - sr_accumulate()
  45. * - sr_copy()
  46. * - sr_copy_if()
  47. * - sr_count()
  48. * - sr_count_if()
  49. * - sr_distance()
  50. * - sr_equal()
  51. * - sr_exists()
  52. * - sr_exists_if()
  53. * - sr_fill()
  54. * - sr_fill_n()
  55. * - sr_for_each()
  56. * - sr_generate()
  57. * - sr_max_element()
  58. * - sr_min_element()
  59. * - sr_replace()
  60. * - sr_replace_if()
  61. */
  62. #ifndef RANGELIB_INCL_RANGELIB_HPP_SALGORITHMS
  63. #define RANGELIB_INCL_RANGELIB_HPP_SALGORITHMS
  64. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  65. # define RANGELIB_VER_RANGELIB_HPP_SALGORITHMS_MAJOR 1
  66. # define RANGELIB_VER_RANGELIB_HPP_SALGORITHMS_MINOR 1
  67. # define RANGELIB_VER_RANGELIB_HPP_SALGORITHMS_REVISION 3
  68. # define RANGELIB_VER_RANGELIB_HPP_SALGORITHMS_EDIT 17
  69. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  70. /* /////////////////////////////////////////////////////////////////////////
  71. * Auto-generation and compatibility
  72. */
  73. /*
  74. [Incompatibilies-start]
  75. STLSOFT_COMPILER_IS_DMC:
  76. STLSOFT_COMPILER_IS_MSVC: _MSC_VER < 1200
  77. STLSOFT_COMPILER_IS_MWERKS: (__MWERKS__ & 0xFF00) < 0x3000
  78. [Incompatibilies-end]
  79. */
  80. /* /////////////////////////////////////////////////////////////////////////
  81. * Includes
  82. */
  83. #ifndef RANGELIB_INCL_RANGELIB_HPP_RANGELIB
  84. # include <rangelib/rangelib.hpp>
  85. #endif /* !RANGELIB_INCL_RANGELIB_HPP_RANGELIB */
  86. #ifndef RANGELIB_INCL_RANGELIB_HPP_ALGORITHMS
  87. # include <rangelib/algorithms.hpp>
  88. #endif /* !RANGELIB_INCL_RANGELIB_HPP_ALGORITHMS */
  89. #ifndef RANGELIB_INCL_RANGELIB_HPP_SEQUENCE_RANGE
  90. # include <rangelib/sequence_range.hpp>
  91. #endif /* !RANGELIB_INCL_RANGELIB_HPP_SEQUENCE_RANGE */
  92. #if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
  93. # ifndef STLSOFT_INCL_STLSOFT_META_DETECTOR_HPP_HAS_CONST_ITERATOR
  94. # include <stlsoft/meta/detector/has_const_iterator.hpp>
  95. # endif /* !STLSOFT_INCL_STLSOFT_META_DETECTOR_HPP_HAS_CONST_ITERATOR */
  96. # ifndef STLSOFT_INCL_STLSOFT_META_DETECTOR_HPP_HAS_ITERATOR
  97. # include <stlsoft/meta/detector/has_iterator.hpp>
  98. # endif /* !STLSOFT_INCL_STLSOFT_META_DETECTOR_HPP_HAS_ITERATOR */
  99. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  100. /* /////////////////////////////////////////////////////////////////////////
  101. * Namespace
  102. */
  103. #ifndef RANGELIB_NO_NAMESPACE
  104. # if defined(_STLSOFT_NO_NAMESPACE) || \
  105. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  106. /* There is no stlsoft namespace, so must define ::rangelib */
  107. namespace rangelib
  108. {
  109. # else
  110. /* Define stlsoft::rangelib_project */
  111. namespace stlsoft
  112. {
  113. namespace rangelib_project
  114. {
  115. # endif /* _STLSOFT_NO_NAMESPACE */
  116. #endif /* !RANGELIB_NO_NAMESPACE */
  117. /* /////////////////////////////////////////////////////////////////////////
  118. * Functions
  119. */
  120. template< ss_typename_param_k S
  121. , ss_typename_param_k T
  122. >
  123. inline T sr_accumulate(S &s, T val)
  124. {
  125. #if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
  126. STLSOFT_STATIC_ASSERT(stlsoft::has_iterator<S>::value || stlsoft::has_const_iterator<S>::value);
  127. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  128. return r_accumulate(sequence_range<S>(s), val);
  129. }
  130. template< ss_typename_param_k S
  131. , ss_typename_param_k T
  132. , ss_typename_param_k P
  133. >
  134. inline T sr_accumulate(S &s, T val, P pred)
  135. {
  136. #if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
  137. STLSOFT_STATIC_ASSERT(stlsoft::has_iterator<S>::value || stlsoft::has_const_iterator<S>::value);
  138. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  139. return r_accumulate(sequence_range<S>(s), val, pred);
  140. }
  141. template< ss_typename_param_k S
  142. , ss_typename_param_k O
  143. >
  144. inline O sr_copy(S &s, O o)
  145. {
  146. return r_copy(sequence_range<S>(s), o);
  147. }
  148. template< ss_typename_param_k S
  149. , ss_typename_param_k O
  150. , ss_typename_param_k P
  151. >
  152. inline O sr_copy_if(S &s, O o, P pred)
  153. {
  154. return r_copy_if(sequence_range<S>(s), o, pred);
  155. }
  156. template< ss_typename_param_k S
  157. , ss_typename_param_k V
  158. >
  159. inline size_t sr_count(S &s, V const& val)
  160. {
  161. #if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
  162. STLSOFT_STATIC_ASSERT(stlsoft::has_iterator<S>::value || stlsoft::has_const_iterator<S>::value);
  163. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  164. return r_count(sequence_range<S>(s), val);
  165. }
  166. template< ss_typename_param_k S
  167. , ss_typename_param_k P
  168. >
  169. inline size_t sr_count_if(S &s, P pred)
  170. {
  171. #if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
  172. STLSOFT_STATIC_ASSERT(stlsoft::has_iterator<S>::value || stlsoft::has_const_iterator<S>::value);
  173. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  174. return r_count_if(sequence_range<S>(s), pred);
  175. }
  176. template <ss_typename_param_k S>
  177. inline ss_ptrdiff_t sr_distance(S &s)
  178. {
  179. return r_distance(sequence_range<S>(s));
  180. }
  181. template< ss_typename_param_k S1
  182. , ss_typename_param_k S2
  183. >
  184. inline ss_bool_t sr_equal(S1 &s1, S2 &s2)
  185. {
  186. STLSOFT_ASSERT(sr_distance(s1) <= sr_distance(s2));
  187. return r_equal(sequence_range<S1>(s1), sequence_range<S2>(s2));
  188. }
  189. template< ss_typename_param_k S
  190. , ss_typename_param_k T
  191. >
  192. inline ss_bool_t sr_exists(S &s, T const& val)
  193. {
  194. return r_exists(sequence_range<S>(s), val);
  195. }
  196. template< ss_typename_param_k S
  197. , ss_typename_param_k P
  198. >
  199. inline ss_bool_t sr_exists_if(S &s, P pred)
  200. {
  201. return r_exists_if(sequence_range<S>(s), pred);
  202. }
  203. template< ss_typename_param_k S
  204. , ss_typename_param_k T
  205. >
  206. inline void sr_fill(S &s, T const& val)
  207. {
  208. r_fill(sequence_range<S>(s), val);
  209. }
  210. template< ss_typename_param_k S
  211. , ss_typename_param_k N
  212. , ss_typename_param_k T
  213. >
  214. inline void sr_fill_n(S &s, N n, T const& val)
  215. {
  216. STLSOFT_ASSERT(n <= r_distance(s));
  217. r_fill_n(sequence_range<S>(s), n, val);
  218. }
  219. template< ss_typename_param_k S
  220. , ss_typename_param_k F
  221. >
  222. inline F sr_for_each(S &s, F f)
  223. {
  224. #if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
  225. STLSOFT_STATIC_ASSERT(stlsoft::has_iterator<S>::value || stlsoft::has_const_iterator<S>::value);
  226. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  227. return r_for_each(sequence_range<S>(s), f);
  228. }
  229. template< ss_typename_param_k S
  230. , ss_typename_param_k F
  231. >
  232. inline void sr_generate(S &s, F f)
  233. {
  234. r_generate(sequence_range<S>(s), f);
  235. }
  236. template <ss_typename_param_k S>
  237. inline ss_typename_type_ret_k S::value_type sr_max_element(S &s)
  238. {
  239. STLSOFT_ASSERT(sr_distance(s) > 0);
  240. return r_max_element(sequence_range<S>(s));
  241. }
  242. template< ss_typename_param_k S
  243. , ss_typename_param_k F
  244. >
  245. inline ss_typename_type_ret_k S::value_type sr_max_element(S &s, F f)
  246. {
  247. STLSOFT_ASSERT(sr_distance(s) > 0);
  248. return r_max_element(sequence_range<S>(s), f);
  249. }
  250. template <ss_typename_param_k S>
  251. inline ss_typename_type_ret_k S::value_type sr_min_element(S &s)
  252. {
  253. STLSOFT_ASSERT(sr_distance(s) > 0);
  254. return r_min_element(sequence_range<S>(s));
  255. }
  256. template< ss_typename_param_k S
  257. , ss_typename_param_k F
  258. >
  259. inline ss_typename_type_ret_k S::value_type sr_min_element(S &s, F f)
  260. {
  261. STLSOFT_ASSERT(sr_distance(s) > 0);
  262. return r_min_element(sequence_range<S>(s), f);
  263. }
  264. template< ss_typename_param_k S
  265. , ss_typename_param_k T
  266. >
  267. inline void sr_replace(S &s, T oldVal, T newVal)
  268. {
  269. r_replace(sequence_range<S>(s), oldVal, newVal);
  270. }
  271. template< ss_typename_param_k S
  272. , ss_typename_param_k P
  273. , ss_typename_param_k T
  274. >
  275. inline void sr_replace_if(S &s, P pred, T newVal)
  276. {
  277. r_replace_if(sequence_range<S>(s), pred, newVal);
  278. }
  279. /* ////////////////////////////////////////////////////////////////////// */
  280. #ifndef RANGELIB_NO_NAMESPACE
  281. # if defined(_STLSOFT_NO_NAMESPACE) || \
  282. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  283. } // namespace rangelib
  284. # else
  285. } // namespace rangelib_project
  286. } // namespace stlsoft
  287. # endif /* _STLSOFT_NO_NAMESPACE */
  288. #endif /* !RANGELIB_NO_NAMESPACE */
  289. /* ////////////////////////////////////////////////////////////////////// */
  290. #endif /* !RANGELIB_INCL_RANGELIB_HPP_SALGORITHMS */
  291. /* ///////////////////////////// end of file //////////////////////////// */