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