1 | // Copyright (C) 2023 Ahmad Samir <a.samirh78@gmail.com> |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef Q20VECTOR_H |
5 | #define Q20VECTOR_H |
6 | |
7 | #include <QtCore/qtconfigmacros.h> |
8 | |
9 | #include <algorithm> |
10 | #include <vector> |
11 | #if __has_include(<memory_resource>) |
12 | # include <memory_resource> |
13 | #endif |
14 | |
15 | // |
16 | // W A R N I N G |
17 | // ------------- |
18 | // |
19 | // This file is not part of the Qt API. Types and functions defined in this |
20 | // file can reliably be replaced by their std counterparts, once available. |
21 | // You may use these definitions in your own code, but be aware that we |
22 | // will remove them once Qt depends on the C++ version that supports |
23 | // them in namespace std. There will be NO deprecation warning, the |
24 | // definitions will JUST go away. |
25 | // |
26 | // If you can't agree to these terms, don't use these definitions! |
27 | // |
28 | // We mean it. |
29 | // |
30 | |
31 | QT_BEGIN_NAMESPACE |
32 | |
33 | namespace q20 { |
34 | // like std::erase/std::erase_if for std::vector |
35 | #if defined(__cpp_lib_erase_if) && __cpp_lib_erase_if >= 202002L // the one returning size_type |
36 | using std::erase; |
37 | using std::erase_if; |
38 | #else |
39 | |
40 | // Make it more specialized than the compiler's, so that our implementation is preferred over |
41 | // the compiler's (which may be present, but return void instead of the number of erased elements). |
42 | |
43 | template <typename T, typename U> |
44 | constexpr typename std::vector<T, std::allocator<T>>::size_type |
45 | erase(std::vector<T, std::allocator<T>> &c, const U &value) |
46 | { |
47 | const auto origSize = c.size(); |
48 | auto it = std::remove(c.begin(), c.end(), value); |
49 | c.erase(it, c.end()); |
50 | return origSize - c.size(); |
51 | } |
52 | |
53 | template <typename T, typename Pred> |
54 | constexpr typename std::vector<T, std::allocator<T>>::size_type |
55 | erase_if(std::vector<T, std::allocator<T>> &c, Pred pred) |
56 | { |
57 | const auto origSize = c.size(); |
58 | auto it = std::remove_if(c.begin(), c.end(), pred); |
59 | c.erase(it, c.end()); |
60 | return origSize - c.size(); |
61 | } |
62 | |
63 | #ifdef __cpp_lib_polymorphic_allocator |
64 | template <typename T, typename U> |
65 | constexpr typename std::vector<T, std::pmr::polymorphic_allocator<T>>::size_type |
66 | erase(std::vector<T, std::pmr::polymorphic_allocator<T>> &c, const U &value) |
67 | { |
68 | const auto origSize = c.size(); |
69 | auto it = std::remove(c.begin(), c.end(), value); |
70 | c.erase(it, c.end()); |
71 | return origSize - c.size(); |
72 | } |
73 | |
74 | template <typename T, typename Pred> |
75 | constexpr typename std::vector<T, std::pmr::polymorphic_allocator<T>>::size_type |
76 | erase_if(std::vector<T, std::pmr::polymorphic_allocator<T>> &c, Pred pred) |
77 | { |
78 | const auto origSize = c.size(); |
79 | auto it = std::remove_if(c.begin(), c.end(), pred); |
80 | c.erase(it, c.end()); |
81 | return origSize - c.size(); |
82 | } |
83 | #endif // __cpp_lib_polymorphic_allocator |
84 | |
85 | #endif // __cpp_lib_erase_if |
86 | } // namespace q20 |
87 | |
88 | QT_END_NAMESPACE |
89 | |
90 | #endif /* Q20VECTOR_H */ |
91 | |