]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/bits/forward_list.tcc
PR libstdc++/90105 make forward_list::sort stable
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / forward_list.tcc
index b41fbbb52f2c9433a156781989a4cb4adcb76c60..088111e3330e5409738d02e2b8946c6b084aeb1d 100644 (file)
@@ -1,6 +1,6 @@
 // <forward_list.tcc> -*- C++ -*-
 
-// Copyright (C) 2008-2018 Free Software Foundation, Inc.
+// Copyright (C) 2008-2019 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -278,11 +278,18 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
          return iterator(const_cast<_Node_base*>(__pos._M_node));
       }
 
+#if __cplusplus > 201703L
+# define _GLIBCXX20_ONLY(__expr) __expr
+#else
+# define _GLIBCXX20_ONLY(__expr)
+#endif
+
   template<typename _Tp, typename _Alloc>
-    void
+    auto
     forward_list<_Tp, _Alloc>::
-    remove(const _Tp& __val)
+    remove(const _Tp& __val) -> __remove_return_type
     {
+      size_type __removed __attribute__((__unused__)) = 0;
       _Node_base* __curr = &this->_M_impl._M_head;
       _Node_base* __extra = nullptr;
 
@@ -293,6 +300,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
              if (__tmp->_M_valptr() != std::__addressof(__val))
                {
                  this->_M_erase_after(__curr);
+                 _GLIBCXX20_ONLY( __removed++ );
                  continue;
                }
              else
@@ -302,46 +310,62 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        }
 
       if (__extra)
-       this->_M_erase_after(__extra);
+       {
+         this->_M_erase_after(__extra);
+         _GLIBCXX20_ONLY( __removed++ );
+       }
+      return _GLIBCXX20_ONLY( __removed );
     }
 
   template<typename _Tp, typename _Alloc>
     template<typename _Pred>
-      void
+      auto
       forward_list<_Tp, _Alloc>::
-      remove_if(_Pred __pred)
+      remove_if(_Pred __pred) -> __remove_return_type
       {
+       size_type __removed __attribute__((__unused__)) = 0;
        _Node_base* __curr = &this->_M_impl._M_head;
        while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
          {
            if (__pred(*__tmp->_M_valptr()))
-             this->_M_erase_after(__curr);
+             {
+               this->_M_erase_after(__curr);
+               _GLIBCXX20_ONLY( __removed++ );
+             }
            else
              __curr = __curr->_M_next;
          }
+       return _GLIBCXX20_ONLY( __removed );
       }
 
   template<typename _Tp, typename _Alloc>
     template<typename _BinPred>
-      void
+      auto
       forward_list<_Tp, _Alloc>::
-      unique(_BinPred __binary_pred)
+      unique(_BinPred __binary_pred) -> __remove_return_type
       {
        iterator __first = begin();
        iterator __last = end();
        if (__first == __last)
-         return;
+         return _GLIBCXX20_ONLY(0);
+       size_type __removed __attribute__((__unused__)) = 0;
        iterator __next = __first;
        while (++__next != __last)
        {
          if (__binary_pred(*__first, *__next))
-           erase_after(__first);
+           {
+             erase_after(__first);
+             _GLIBCXX20_ONLY( __removed++ );
+           }
          else
            __first = __next;
          __next = __first;
        }
+        return _GLIBCXX20_ONLY( __removed );
       }
 
+#undef _GLIBCXX20_ONLY
+
   template<typename _Tp, typename _Alloc>
     template<typename _Comp>
       void
@@ -375,7 +399,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       auto __iy = __ly.cbegin();
       while (__ix != __lx.cend() && __iy != __ly.cend())
        {
-         if (*__ix != *__iy)
+         if (!(*__ix == *__iy))
            return false;
          ++__ix;
          ++__iy;
@@ -445,9 +469,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                        __p = static_cast<_Node*>(__p->_M_next);
                        --__psize;
                      }
-                   else if (__comp(*__p->_M_valptr(), *__q->_M_valptr()))
+                   else if (!__comp(*__q->_M_valptr(), *__p->_M_valptr()))
                      {
-                       // First node of p is lower; e must come from p.
+                       // First node of q is not lower; e must come from p.
                        __e = __p;
                        __p = static_cast<_Node*>(__p->_M_next);
                        --__psize;