Commit b7283ea0 authored by Joe Loser's avatar Joe Loser Committed by Facebook Github Bot

Implement uniform container erasure for FBVector and small_vector (#1089)

Summary:
- Implement `erase` and `erase_if` for both `FBVector` and
  `small_vector`.
- This matches the APIs as in the C++20 Library Fundamentals v2.
Pull Request resolved: https://github.com/facebook/folly/pull/1089

Reviewed By: Orvid

Differential Revision: D15112753

Pulled By: yfeldblum

fbshipit-source-id: dc93c600fa8e7e8a7ed42d6a6e07439d4fa51204
parent dbce35e9
......@@ -1751,4 +1751,14 @@ template <
fbvector(InputIt, InputIt, Allocator = Allocator())
->fbvector<typename std::iterator_traits<InputIt>::value_type, Allocator>;
#endif
template <class T, class A, class U>
void erase(fbvector<T, A>& v, U value) {
v.erase(std::remove(v.begin(), v.end(), value), v.end());
}
template <class T, class A, class Predicate>
void erase_if(fbvector<T, A>& v, Predicate predicate) {
v.erase(std::remove_if(v.begin(), v.end(), predicate), v.end());
}
} // namespace folly
......@@ -1267,6 +1267,22 @@ void swap(
a.swap(b);
}
template <class T, std::size_t MaxInline, class A, class B, class C, class U>
void erase(small_vector<T, MaxInline, A, B, C>& v, U value) {
v.erase(std::remove(v.begin(), v.end(), value), v.end());
}
template <
class T,
std::size_t MaxInline,
class A,
class B,
class C,
class Predicate>
void erase_if(small_vector<T, MaxInline, A, B, C>& v, Predicate predicate) {
v.erase(std::remove_if(v.begin(), v.end(), predicate), v.end());
}
//////////////////////////////////////////////////////////////////////
namespace detail {
......
......@@ -22,6 +22,7 @@
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <folly/FBString.h>
#include <folly/Random.h>
......@@ -270,3 +271,23 @@ TEST(FBVector, deduction_guides) {
EXPECT_TRUE((std::is_same_v<fbvector<fbvector<int>::iterator>, decltype(y)>));
}
#endif
TEST(FBVector, erase) {
fbvector<int> v(3);
std::iota(v.begin(), v.end(), 1);
v.push_back(2);
erase(v, 2);
ASSERT_EQ(2u, v.size());
EXPECT_EQ(1u, v[0]);
EXPECT_EQ(3u, v[1]);
}
TEST(FBVector, erase_if) {
fbvector<int> v(6);
std::iota(v.begin(), v.end(), 1);
erase_if(v, [](const auto& x) { return x % 2 == 0; });
ASSERT_EQ(3u, v.size());
EXPECT_EQ(1u, v[0]);
EXPECT_EQ(3u, v[1]);
EXPECT_EQ(5u, v[2]);
}
......@@ -21,6 +21,7 @@
#include <iterator>
#include <limits>
#include <memory>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
......@@ -1162,3 +1163,23 @@ TEST(small_vector, SelfCopyAssignmentForVectorOfPair) {
EXPECT_EQ(test.size(), 1);
EXPECT_EQ(test[0].first, 13);
}
TEST(small_vector, erase) {
small_vector<int> v(3);
std::iota(v.begin(), v.end(), 1);
v.push_back(2);
erase(v, 2);
ASSERT_EQ(2u, v.size());
EXPECT_EQ(1u, v[0]);
EXPECT_EQ(3u, v[1]);
}
TEST(small_vector, erase_if) {
small_vector<int> v(6);
std::iota(v.begin(), v.end(), 1);
erase_if(v, [](const auto& x) { return x % 2 == 0; });
ASSERT_EQ(3u, v.size());
EXPECT_EQ(1u, v[0]);
EXPECT_EQ(3u, v[1]);
EXPECT_EQ(5u, v[2]);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment