set_assign.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  2. // vim:tabstop=4:shiftwidth=4:expandtab:
  3. /*
  4. * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any
  8. * damages arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute
  12. * it freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must
  15. * not claim that you wrote the original software. If you use this
  16. * software in a product, an acknowledgement in the product
  17. * documentation would be appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must
  19. * not be misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source
  21. * distribution.
  22. *
  23. * This file is part of Stones of Nvwa:
  24. * http://sourceforge.net/projects/nvwa
  25. *
  26. */
  27. /*!
  28. * \file set_assign.h
  29. * \ingroup QxMemLeak
  30. *
  31. * Definition of template functions set_assign_union and set_assign_difference.
  32. *
  33. * \version 1.5, 2004/07/26
  34. * \author Wu Yongwei
  35. *
  36. */
  37. #ifndef QT_NO_DEBUG
  38. #ifndef _QX_MODE_RELEASE
  39. #if _QX_USE_MEM_LEAK_DETECTION
  40. #ifndef _SET_ASSIGN_H
  41. #define _SET_ASSIGN_H
  42. #ifdef _MSC_VER
  43. #pragma once
  44. #endif
  45. #include <algorithm>
  46. namespace qx {
  47. namespace memory {
  48. template <class _Container, class _InputIter>
  49. _Container& set_assign_union(_Container& __dest,
  50. _InputIter __first,
  51. _InputIter __last)
  52. {
  53. typename _Container::iterator __first_dest = __dest.begin();
  54. typename _Container::iterator __last_dest = __dest.end();
  55. while (__first_dest != __last_dest && __first != __last)
  56. {
  57. if (*__first_dest < *__first)
  58. ++__first_dest;
  59. else if (*__first < *__first_dest)
  60. {
  61. __dest.insert(__first_dest, *__first);
  62. ++__first;
  63. }
  64. else // *__first_dest == *__first
  65. {
  66. ++__first_dest;
  67. ++__first;
  68. }
  69. }
  70. if (__first != __last)
  71. std::copy(__first, __last, inserter(__dest, __last_dest));
  72. return __dest;
  73. }
  74. template <class _Container, class _InputIter, class _Compare>
  75. _Container& set_assign_union(_Container& __dest,
  76. _InputIter __first,
  77. _InputIter __last,
  78. _Compare __comp)
  79. {
  80. typename _Container::iterator __first_dest = __dest.begin();
  81. typename _Container::iterator __last_dest = __dest.end();
  82. while (__first_dest != __last_dest && __first != __last)
  83. {
  84. if (__comp(*__first_dest, *__first))
  85. ++__first_dest;
  86. else if (__comp(*__first, *__first_dest))
  87. {
  88. __dest.insert(__first_dest, *__first);
  89. ++__first;
  90. }
  91. else // *__first_dest is equivalent to *__first
  92. {
  93. ++__first_dest;
  94. ++__first;
  95. }
  96. }
  97. if (__first != __last)
  98. std::copy(__first, __last, inserter(__dest, __last_dest));
  99. return __dest;
  100. }
  101. template <class _Container, class _InputIter>
  102. _Container& set_assign_difference(_Container& __dest,
  103. _InputIter __first,
  104. _InputIter __last)
  105. {
  106. typename _Container::iterator __first_dest = __dest.begin();
  107. typename _Container::iterator __last_dest = __dest.end();
  108. while (__first_dest != __last_dest && __first != __last)
  109. {
  110. if (*__first_dest < *__first)
  111. ++__first_dest;
  112. else if (*__first < *__first_dest)
  113. ++__first;
  114. else // *__first_dest == *__first
  115. {
  116. __dest.erase(__first_dest++);
  117. ++__first;
  118. }
  119. }
  120. return __dest;
  121. }
  122. template <class _Container, class _InputIter, class _Compare>
  123. _Container& set_assign_difference(_Container& __dest,
  124. _InputIter __first,
  125. _InputIter __last,
  126. _Compare __comp)
  127. {
  128. typename _Container::iterator __first_dest = __dest.begin();
  129. typename _Container::iterator __last_dest = __dest.end();
  130. while (__first_dest != __last_dest && __first != __last)
  131. {
  132. if (__comp(*__first_dest, *__first))
  133. ++__first_dest;
  134. else if (__comp(*__first, *__first_dest))
  135. ++__first;
  136. else // *__first_dest is equivalent to *__first
  137. {
  138. __dest.erase(__first_dest++);
  139. ++__first;
  140. }
  141. }
  142. return __dest;
  143. }
  144. } // namespace memory
  145. } // namespace qx
  146. #endif // _SET_ASSIGN_H
  147. #endif // _QX_USE_MEM_LEAK_DETECTION
  148. #endif // _QX_MODE_RELEASE
  149. #endif // QT_NO_DEBUG