Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/q20vector.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2023 Ahmad Samir <a.samirh78@gmail.com>
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef Q20VECTOR_H
0005 #define Q20VECTOR_H
0006 
0007 #include <QtCore/qtconfigmacros.h>
0008 
0009 #include <algorithm>
0010 #include <vector>
0011 #if __has_include(<memory_resource>)
0012 #  include <memory_resource>
0013 #endif
0014 
0015 //
0016 //  W A R N I N G
0017 //  -------------
0018 //
0019 // This file is not part of the Qt API. Types and functions defined in this
0020 // file can reliably be replaced by their std counterparts, once available.
0021 // You may use these definitions in your own code, but be aware that we
0022 // will remove them once Qt depends on the C++ version that supports
0023 // them in namespace std. There will be NO deprecation warning, the
0024 // definitions will JUST go away.
0025 //
0026 // If you can't agree to these terms, don't use these definitions!
0027 //
0028 // We mean it.
0029 //
0030 
0031 QT_BEGIN_NAMESPACE
0032 
0033 namespace q20 {
0034 // like std::erase/std::erase_if for std::vector
0035 #if defined(__cpp_lib_erase_if) && __cpp_lib_erase_if >= 202002L // the one returning size_type
0036 using std::erase;
0037 using std::erase_if;
0038 #else
0039 
0040 // Make it more specialized than the compiler's, so that our implementation is preferred over
0041 // the compiler's (which may be present, but return void instead of the number of erased elements).
0042 
0043 template <typename T, typename U>
0044 constexpr typename std::vector<T, std::allocator<T>>::size_type
0045 erase(std::vector<T, std::allocator<T>> &c, const U &value)
0046 {
0047     const auto origSize = c.size();
0048     auto it = std::remove(c.begin(), c.end(), value);
0049     c.erase(it, c.end());
0050     return origSize - c.size();
0051 }
0052 
0053 template <typename T, typename Pred>
0054 constexpr typename std::vector<T, std::allocator<T>>::size_type
0055 erase_if(std::vector<T, std::allocator<T>> &c, Pred pred)
0056 {
0057     const auto origSize = c.size();
0058     auto it = std::remove_if(c.begin(), c.end(), pred);
0059     c.erase(it, c.end());
0060     return origSize - c.size();
0061 }
0062 
0063 #ifdef __cpp_lib_polymorphic_allocator
0064 template <typename T, typename U>
0065 constexpr typename std::vector<T, std::pmr::polymorphic_allocator<T>>::size_type
0066 erase(std::vector<T, std::pmr::polymorphic_allocator<T>> &c, const U &value)
0067 {
0068     const auto origSize = c.size();
0069     auto it = std::remove(c.begin(), c.end(), value);
0070     c.erase(it, c.end());
0071     return origSize - c.size();
0072 }
0073 
0074 template <typename T, typename Pred>
0075 constexpr typename std::vector<T, std::pmr::polymorphic_allocator<T>>::size_type
0076 erase_if(std::vector<T, std::pmr::polymorphic_allocator<T>> &c, Pred pred)
0077 {
0078     const auto origSize = c.size();
0079     auto it = std::remove_if(c.begin(), c.end(), pred);
0080     c.erase(it, c.end());
0081     return origSize - c.size();
0082 }
0083 #endif // __cpp_lib_polymorphic_allocator
0084 
0085 #endif // __cpp_lib_erase_if
0086 } // namespace q20
0087 
0088 QT_END_NAMESPACE
0089 
0090 #endif /* Q20VECTOR_H */