-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from paulfd/swap-and-pop
Implement helpers for the swap and pop idioms
- Loading branch information
Showing
7 changed files
with
242 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
|
||
// This code is part of the sfizz library and is licensed under a BSD 2-clause | ||
// license. You should have receive a LICENSE.md file along with the code. | ||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz | ||
|
||
#pragma once | ||
|
||
namespace sfz | ||
{ | ||
|
||
/** | ||
* @brief Implement a swap and pop idiom for a vector, applying an action on elements | ||
* fitting a condition and removing them by swapping them out with the last element of | ||
* the vector. This costs less than `erase` at the cost of the ordering of the vector. | ||
* | ||
* @param vector | ||
* @param condition which elements to remove | ||
* @param action what to do with them before removing | ||
* @return unsigned | ||
*/ | ||
template<class T, class F, class A> | ||
inline unsigned swapAndPopAll(std::vector<T>& vector, F&& condition, A&& action) | ||
{ | ||
auto it = vector.begin(); | ||
auto sentinel = vector.rbegin(); | ||
while (it < sentinel.base()) { | ||
if (condition(*it)) { | ||
action(*it); | ||
std::iter_swap(it, sentinel); | ||
++sentinel; | ||
} else { | ||
++it; | ||
} | ||
} | ||
auto removed = std::distance(sentinel.base(), vector.end()); | ||
vector.resize(std::distance(vector.begin(), sentinel.base())); | ||
return removed; | ||
} | ||
|
||
/** | ||
* @brief Implement a swap and pop idiom for a vector, removing all the elements | ||
* fitting some condition by swapping them out with the last element of the vector. | ||
* This costs less than `erase` at the cost of the ordering of the vector. | ||
* | ||
* @param vector | ||
* @param condition which elements to remove | ||
* @return unsigned | ||
*/ | ||
template<class T, class F> | ||
inline unsigned swapAndPopAll(std::vector<T>& vector, F&& condition) | ||
{ | ||
return swapAndPopAll(vector, std::forward<F>(condition), [](T& v){ (void)v; }); | ||
} | ||
|
||
/** | ||
* @brief Implement a swap and pop idiom for a vector, applying an action on the first | ||
* element fitting a condition and removing it by swapping it out with the last element of | ||
* the vector. This costs less than `erase` at the cost of the ordering of the vector. | ||
* | ||
* @param vector | ||
* @param condition which element to remove | ||
* @param action what to do with it before removing | ||
* @return unsigned | ||
*/ | ||
template<class T, class F, class A> | ||
inline bool swapAndPopFirst(std::vector<T>& vector, F&& condition, A&& action) | ||
{ | ||
auto it = vector.begin(); | ||
auto sentinel = vector.rbegin(); | ||
while (it < sentinel.base()) { | ||
if (condition(*it)) { | ||
action(*it); | ||
std::iter_swap(it, sentinel); | ||
vector.pop_back(); | ||
return true; | ||
} | ||
++it; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @brief Implement a swap and pop idiom for a vector, removing the first element | ||
* fitting some condition by swapping it out with the last element of the vector. | ||
* This costs less than `erase` at the cost of the ordering of the vector. | ||
* | ||
* @param vector | ||
* @param condition which element to remove | ||
* @return unsigned | ||
*/ | ||
template<class T, class F> | ||
inline unsigned swapAndPopFirst(std::vector<T>& vector, F&& condition) | ||
{ | ||
return swapAndPopFirst(vector, std::forward<F>(condition), [](T& v){ (void)v; }); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
|
||
// This code is part of the sfizz library and is licensed under a BSD 2-clause | ||
// license. You should have receive a LICENSE.md file along with the code. | ||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz | ||
|
||
#include "catch2/catch.hpp" | ||
#include "sfizz/SwapAndPop.h" | ||
using namespace Catch::literals; | ||
|
||
TEST_CASE("[SwapAndPop] Popping one element") | ||
{ | ||
SECTION("PopFirst -- Test 1") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopFirst(vector, [](int v) -> bool { return v == 2; })); | ||
std::vector<int> expected { 1, 6, 3, 4, 5 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopFirst -- Test 2") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopFirst(vector, [](int v) -> bool { return v == 5; })); | ||
std::vector<int> expected { 1, 2, 3, 4, 6 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopFirst -- Test 3") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopFirst(vector, [](int v) -> bool { return v == 1; })); | ||
std::vector<int> expected { 6, 2, 3, 4, 5 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopFirst -- Test 4") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopFirst(vector, [](int v) -> bool { return v == 6; })); | ||
std::vector<int> expected { 1, 2, 3, 4, 5 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopAll -- Test 1") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopAll(vector, [](int v) -> bool { return v == 2; }) == 1); | ||
std::vector<int> expected { 1, 6, 3, 4, 5 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopAll -- Test 2") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopAll(vector, [](int v) -> bool { return v == 5; }) == 1); | ||
std::vector<int> expected { 1, 2, 3, 4, 6 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopAll -- Test 3") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopAll(vector, [](int v) -> bool { return v == 1; }) == 1); | ||
std::vector<int> expected { 6, 2, 3, 4, 5 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopAll -- Test 4") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopAll(vector, [](int v) -> bool { return v == 6; }) == 1); | ||
std::vector<int> expected { 1, 2, 3, 4, 5 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
} | ||
|
||
TEST_CASE("[SwapAndPop] Popping multiple elements") | ||
{ | ||
SECTION("PopAll -- Test 1") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopAll(vector, [](int v) -> bool { return v == 1 || v == 2; }) == 2); | ||
std::vector<int> expected { 6, 5, 3, 4 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopAll -- Test 2") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopAll(vector, [](int v) -> bool { return v % 2 == 0; }) == 3); | ||
std::vector<int> expected { 1, 5, 3 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopAll -- Test 3") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopAll(vector, [](int v) -> bool { return v == 1 || v == 5; }) == 2); | ||
std::vector<int> expected { 6, 2, 3, 4 }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopAll -- Test 4") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
REQUIRE(sfz::swapAndPopAll(vector, [](int v) -> bool { (void)v; return true; }) == 6); | ||
std::vector<int> expected { }; | ||
REQUIRE( vector == expected ); | ||
} | ||
SECTION("PopAll -- Test 1") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
const auto condition = [](int v) -> bool { return v == 1 || v == 2; }; | ||
int poppedSum = 0; | ||
const auto action = [&poppedSum](int v) { poppedSum += v; }; | ||
REQUIRE(sfz::swapAndPopAll(vector, condition, action) == 2); | ||
std::vector<int> expected { 6, 5, 3, 4 }; | ||
REQUIRE( vector == expected ); | ||
REQUIRE( poppedSum == 3 ); | ||
} | ||
SECTION("PopAll -- Test 2") | ||
{ | ||
std::vector<int> vector { 1, 2, 3, 4, 5, 6 }; | ||
const auto condition = [](int v) -> bool { return v % 2 == 0; }; | ||
int poppedSum = 0; | ||
const auto action = [&poppedSum](int v) { poppedSum += v; }; | ||
REQUIRE(sfz::swapAndPopAll(vector, condition, action) == 3); | ||
std::vector<int> expected { 1, 5, 3 }; | ||
REQUIRE( vector == expected ); | ||
REQUIRE( poppedSum == 12 ); | ||
} | ||
} |