Skip to content

Commit a5673d6

Browse files
committed
[Imp] mpt/base/alloc.hpp: Add mpt::erase(std::vector, value) and mpt::erase_if(std::vector, pred), like C++20 std::erase and std::erase_if.
git-svn-id: https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@24783 56274372-70c3-4bfc-bfc3-4c3a0b034d27
1 parent 6933629 commit a5673d6

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/mpt/base/alloc.hpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66

77

8-
#include "mpt/base/namespace.hpp"
9-
8+
#include "mpt/base/detect.hpp"
109
#include "mpt/base/memory.hpp"
10+
#include "mpt/base/namespace.hpp"
1111
#include "mpt/base/span.hpp"
1212

13+
#if MPT_CXX_BEFORE(20)
14+
#include <algorithm>
15+
#endif // C++20
1316
#include <iterator>
1417
#include <memory>
1518
#include <string>
@@ -114,6 +117,33 @@ inline Tcont1 & append(Tcont1 & cont1, Tit2 beg, Tit2 end) {
114117

115118

116119

120+
#if MPT_CXX_AT_LEAST(20)
121+
122+
using std::erase;
123+
using std::erase_if;
124+
125+
#else // !C++20
126+
127+
template <typename T, typename U>
128+
inline typename std::vector<T>::size_type erase(std::vector<T> & c, const U & value) {
129+
auto it = std::remove(c.begin(), c.end(), value);
130+
auto r = c.end() - it;
131+
c.erase(it, c.end());
132+
return r;
133+
}
134+
135+
template <typename T, typename Pred>
136+
inline typename std::vector<T>::size_type erase_if(std::vector<T> & c, Pred pred) {
137+
auto it = std::remove_if(c.begin(), c.end(), pred);
138+
auto r = c.end() - it;
139+
c.erase(it, c.end());
140+
return r;
141+
}
142+
143+
#endif // C++20
144+
145+
146+
117147
template <typename Tdst, typename Tsrc>
118148
struct buffer_cast_impl {
119149
inline Tdst operator()(const Tsrc & src) const {

0 commit comments

Comments
 (0)