vector 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // <vector> -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file include/vector
  46. * This is a Standard C++ Library header.
  47. */
  48. #ifndef _GLIBCXX_VECTOR
  49. #define _GLIBCXX_VECTOR 1
  50. #pragma GCC system_header
  51. #include <bits/stl_algobase.h>
  52. #include <bits/allocator.h>
  53. #include <bits/stl_construct.h>
  54. #include <bits/stl_uninitialized.h>
  55. #include <bits/stl_vector.h>
  56. #include <bits/stl_bvector.h>
  57. #include <bits/refwrap.h>
  58. #include <bits/range_access.h>
  59. #ifndef _GLIBCXX_EXPORT_TEMPLATE
  60. # include <bits/vector.tcc>
  61. #endif
  62. #ifdef _GLIBCXX_DEBUG
  63. # include <debug/vector>
  64. #endif
  65. #if __cplusplus >= 201703L
  66. namespace std _GLIBCXX_VISIBILITY(default)
  67. {
  68. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  69. namespace pmr {
  70. template<typename _Tp> class polymorphic_allocator;
  71. template<typename _Tp>
  72. using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
  73. } // namespace pmr
  74. # ifdef _GLIBCXX_DEBUG
  75. namespace _GLIBCXX_STD_C::pmr {
  76. template<typename _Tp>
  77. using vector
  78. = _GLIBCXX_STD_C::vector<_Tp, std::pmr::polymorphic_allocator<_Tp>>;
  79. } // namespace _GLIBCXX_STD_C::pmr
  80. # endif
  81. _GLIBCXX_END_NAMESPACE_VERSION
  82. } // namespace std
  83. #endif // C++17
  84. #if __cplusplus > 201703L
  85. namespace std _GLIBCXX_VISIBILITY(default)
  86. {
  87. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  88. #define __cpp_lib_erase_if 202002L
  89. template<typename _Tp, typename _Alloc, typename _Predicate>
  90. _GLIBCXX20_CONSTEXPR
  91. inline typename vector<_Tp, _Alloc>::size_type
  92. erase_if(vector<_Tp, _Alloc>& __cont, _Predicate __pred)
  93. {
  94. using namespace __gnu_cxx;
  95. _GLIBCXX_STD_C::vector<_Tp, _Alloc>& __ucont = __cont;
  96. const auto __osz = __cont.size();
  97. const auto __end = __ucont.end();
  98. auto __removed = std::__remove_if(__ucont.begin(), __end,
  99. __ops::__pred_iter(std::ref(__pred)));
  100. if (__removed != __end)
  101. {
  102. __cont.erase(__niter_wrap(__cont.begin(), __removed),
  103. __cont.end());
  104. return __osz - __cont.size();
  105. }
  106. return 0;
  107. }
  108. template<typename _Tp, typename _Alloc, typename _Up>
  109. _GLIBCXX20_CONSTEXPR
  110. inline typename vector<_Tp, _Alloc>::size_type
  111. erase(vector<_Tp, _Alloc>& __cont, const _Up& __value)
  112. {
  113. using namespace __gnu_cxx;
  114. _GLIBCXX_STD_C::vector<_Tp, _Alloc>& __ucont = __cont;
  115. const auto __osz = __cont.size();
  116. const auto __end = __ucont.end();
  117. auto __removed = std::__remove_if(__ucont.begin(), __end,
  118. __ops::__iter_equals_val(__value));
  119. if (__removed != __end)
  120. {
  121. __cont.erase(__niter_wrap(__cont.begin(), __removed),
  122. __cont.end());
  123. return __osz - __cont.size();
  124. }
  125. return 0;
  126. }
  127. _GLIBCXX_END_NAMESPACE_VERSION
  128. } // namespace std
  129. #endif // C++20
  130. #endif /* _GLIBCXX_VECTOR */