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.

172 lines
5.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/algorithms/bounded.hpp
  3. *
  4. * Purpose: Bounded algorithms.
  5. *
  6. * Created: 23rd October 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-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/bounded.hpp
  40. *
  41. * \brief [C++ only] Bounded algorithms
  42. * (\ref group__library__algorithms "Algorithms" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_ALGORITHMS_HPP_BOUNDED
  45. #define STLSOFT_INCL_STLSOFT_ALGORITHMS_HPP_BOUNDED
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_ALGORITHMS_HPP_BOUNDED_MAJOR 2
  48. # define STLSOFT_VER_STLSOFT_ALGORITHMS_HPP_BOUNDED_MINOR 1
  49. # define STLSOFT_VER_STLSOFT_ALGORITHMS_HPP_BOUNDED_REVISION 1
  50. # define STLSOFT_VER_STLSOFT_ALGORITHMS_HPP_BOUNDED_EDIT 22
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Compatibility
  54. */
  55. /*
  56. [Incompatibilies-start]
  57. STLSOFT_COMPILER_IS_WATCOM:
  58. [Incompatibilies-end]
  59. */
  60. /* /////////////////////////////////////////////////////////////////////////
  61. * Includes
  62. */
  63. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  64. # include <stlsoft/stlsoft.h>
  65. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  66. #ifndef STLSOFT_INCL_STLSOFT_ALGORITHM_STD_HPP_ALT
  67. # include <stlsoft/algorithms/std/alt.hpp>
  68. #endif /* !STLSOFT_INCL_STLSOFT_ALGORITHM_STD_HPP_ALT */
  69. #ifdef STLSOFT_CF_std_NAMESPACE
  70. # include <iterator> // for the iterator tags
  71. # include <utility> // for std::pair
  72. #endif /* STLSOFT_CF_std_NAMESPACE */
  73. #ifdef STLSOFT_UNITTEST
  74. # include <string.h> // for strcmp
  75. #endif /* STLSOFT_UNITTEST */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Namespace
  78. */
  79. #ifndef _STLSOFT_NO_NAMESPACE
  80. namespace stlsoft
  81. {
  82. #endif /* _STLSOFT_NO_NAMESPACE */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * Algorithms
  85. */
  86. /** \brief Copies N elements from the range <code>[src, src + n)</code> to
  87. * the range <code>[dest, dest + n)</code>.
  88. *
  89. * \ingroup group__library__algorithms
  90. *
  91. * \param src The iterator copied from
  92. * \param n The number of elements copied
  93. * \param dest The iterator copied to
  94. */
  95. template< ss_typename_param_k I
  96. , ss_typename_param_k O
  97. >
  98. // [[synesis:function:algorithm: copy_n(T<I> src, size_t n, T<O> dest)]]
  99. inline O copy_n(I src, ss_size_t n, O dest)
  100. {
  101. for(; n != 0; ++src, ++dest, --n)
  102. {
  103. *dest = *src;
  104. }
  105. return dest;
  106. }
  107. /** \brief Copies N elements from the range <code>[src, src + n)</code> to
  108. * the range <code>[dest, dest + n)</code>.
  109. *
  110. * \ingroup group__library__algorithms
  111. *
  112. * \param src The iterator copied from
  113. * \param n The number of elements copied
  114. * \param oldValue The value to search for
  115. * \param newValue The value to replace with
  116. */
  117. template< ss_typename_param_k I
  118. , ss_typename_param_k T
  119. >
  120. // [[synesis:function:algorithm: replace_n(T<I> src, size_t n, T<T> const& oldValue, T<T> const& newValue)]]
  121. inline void replace_n(I src, ss_size_t n, T const& oldValue, T const& newValue)
  122. {
  123. #if 0
  124. // NOTE: Why this is not valid!!!!!!!!
  125. std_replace(src, src + n, oldValue, newValue);
  126. #else /* ? 0 */
  127. for(; 0 != n; ++src, --n)
  128. {
  129. if(oldValue == *src)
  130. {
  131. *src = newValue;
  132. }
  133. }
  134. #endif /* 0 */
  135. }
  136. ////////////////////////////////////////////////////////////////////////////
  137. // Unit-testing
  138. #ifdef STLSOFT_UNITTEST
  139. # include "./unittest/bounded_unittest_.h"
  140. #endif /* STLSOFT_UNITTEST */
  141. /* ////////////////////////////////////////////////////////////////////// */
  142. #ifndef _STLSOFT_NO_NAMESPACE
  143. } // namespace stlsoft
  144. #endif /* _STLSOFT_NO_NAMESPACE */
  145. /* ////////////////////////////////////////////////////////////////////// */
  146. #endif /* !STLSOFT_INCL_STLSOFT_ALGORITHMS_HPP_BOUNDED */
  147. /* ///////////////////////////// end of file //////////////////////////// */