Skip to content

Commit

Permalink
Rename remove/remove_if to erase/erase_if
Browse files Browse the repository at this point in the history
Task-Id: #7
  • Loading branch information
jesperkdab committed Oct 11, 2022
1 parent 7cdd660 commit 943fddd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct S
bool hasEqualKeyValuePair() const { return x == y; }
};
std::vector<Struct> vec{{2, 3}, {1, 1}, {2, 2}, {4, 1}};
kdalgorithms::remove_if(vec, &Struct::hasEqualKeyValuePair);
kdalgorithms::erase_if(vec, &Struct::hasEqualKeyValuePair);
```

Be aware though that the method must be const.
Expand All @@ -98,7 +98,7 @@ Modifying algorithms
- <a href="#reverse">reverse</a>
- <a href="#sort">sort</a>
- <a href="#remove_duplicates">remove_duplicates</a>
- <a href="#remove">remove / remove_if</a>
- <a href="#erase">erase / erase_if</a>

Queries

Expand Down Expand Up @@ -537,13 +537,13 @@ auto result = kdalgorithms::has_duplicates(vec, kdalgorithms::do_sort);
// result = true
```

<a name="remove">remove / remove_if</a>
<a name="erase">erase / erase_if</a>
---------------------------------------
**remove** removes all instances of a given value, while **remove_if** remove all instances matching a predicate.
**erase** removes all instances of a given value, while **erase_if** remove all instances matching a predicate.

```
std::vector<int> vec{1, 2, 1, 3};
kdalgorithms::remove(vec, 1);
kdalgorithms::erase(vec, 1);
// vec = {2,3}
```

Expand All @@ -556,7 +556,7 @@ struct Struct
auto with_key = [](int key) { return [key](const Struct &s) { return s.key == key; }; };
std::vector<Struct> vec{{2, 3}, {1, 4}, {2, 2}, {4, 1}};
kdalgorithms::remove_if(vec, with_key(2));
kdalgorithms::erase_if(vec, with_key(2));
// vec = {{1,4}, {4,1}}
```

Expand Down
6 changes: 3 additions & 3 deletions src/kdalgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ bool has_duplicates(Container &&container, SortOption sort)
return hasDuplicates(sorted(std::forward<Container>(container)));
}

// -------------------- remove / remove_if --------------------
// -------------------- erase / erase_if --------------------
template <typename Container, typename Value>
#if __cplusplus >= 202002L
requires ContainerOfType<Container, Value>
#endif
void remove(Container &container, Value &&value)
void erase(Container &container, Value &&value)
{
auto it = std::remove(container.begin(), container.end(), std::forward<Value>(value));
container.erase(it, container.end());
Expand All @@ -322,7 +322,7 @@ template <typename Container, typename UnaryPredicate>
#if __cplusplus >= 202002L
requires UnaryPredicateOnContainerValues<UnaryPredicate, Container>
#endif
void remove_if(Container &container, UnaryPredicate &&predicate)
void erase_if(Container &container, UnaryPredicate &&predicate)
{
auto it = std::remove_if(container.begin(), container.end(),
detail::to_function_object(std::forward<UnaryPredicate>(predicate)));
Expand Down
18 changes: 9 additions & 9 deletions tests/tst_kdalgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ private Q_SLOTS:
void remove_duplicates();
void has_duplicates();
void has_duplicates_data();
void remove();
void remove_if();
void erase();
void erase_if();
void combiningTests();
};

Expand Down Expand Up @@ -1182,32 +1182,32 @@ void TestAlgorithms::has_duplicates_data()
QTest::newRow("unsorted not unique") << std::vector<int>{3, 1, 3, 4} << true << true;
}

void TestAlgorithms::remove()
void TestAlgorithms::erase()
{
// several duplicates to remove
{
std::vector<int> vec{1, 2, 1, 3};
kdalgorithms::remove(vec, 1);
kdalgorithms::erase(vec, 1);
std::vector<int> expected{2, 3};
QCOMPARE(vec, expected);
}

// Nothing to remove
{
std::vector<int> vec{1, 2, 1, 3};
kdalgorithms::remove(vec, 42);
kdalgorithms::erase(vec, 42);
std::vector<int> expected{1, 2, 1, 3};
QCOMPARE(vec, expected);
}
}

void TestAlgorithms::remove_if()
void TestAlgorithms::erase_if()
{
auto withKey = [](int key) { return [key](const Struct &s) { return s.key == key; }; };
// several duplicates to remove
{
std::vector<Struct> vec{{2, 3}, {1, 4}, {2, 2}, {4, 1}};
kdalgorithms::remove_if(vec, withKey(2));
kdalgorithms::erase_if(vec, withKey(2));
std::vector<Struct> expected{{1, 4}, {4, 1}};
QCOMPARE(vec, expected);
}
Expand All @@ -1216,14 +1216,14 @@ void TestAlgorithms::remove_if()
{
std::vector<Struct> vec{{2, 3}, {1, 4}, {2, 2}, {4, 1}};
auto expected = vec;
kdalgorithms::remove_if(vec, withKey(42));
kdalgorithms::erase_if(vec, withKey(42));
QCOMPARE(vec, expected);
}

{
std::vector<Struct> vec{{2, 3}, {1, 1}, {2, 2}, {4, 1}};
std::vector<Struct> expected = {{2, 3}, {4, 1}};
kdalgorithms::remove_if(vec, &Struct::hasEqualKeyValuePair);
kdalgorithms::erase_if(vec, &Struct::hasEqualKeyValuePair);
QCOMPARE(vec, expected);
}
}
Expand Down

0 comments on commit 943fddd

Please sign in to comment.