-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
|
||
namespace choose { | ||
|
||
template <typename it, typename Comp> | ||
void stable_partial_sort(it begin, it middle, it end, Comp comp) { | ||
// adapted from https://stackoverflow.com/a/27248519/15534181 | ||
std::vector<it> sorted; | ||
sorted.resize(end - begin); | ||
auto to = sorted.begin(); | ||
for (auto p = begin; p != end; ++p) { | ||
*to++ = p; | ||
} | ||
auto n = middle - begin; | ||
|
||
std::partial_sort(sorted.begin(), sorted.begin() + n, sorted.end(), [&](const it& lhs, const it& rhs) { | ||
// First see if the underlying elements differ. | ||
if (comp(*lhs, *rhs)) | ||
return true; | ||
if (comp(*rhs, *lhs)) | ||
return false; | ||
// Underlying elements are the same, so compare iterators; these represent | ||
// position in original vector. | ||
return lhs < rhs; | ||
}); | ||
|
||
std::vector<typename it::value_type> replacement; | ||
replacement.resize(n); | ||
for (decltype(n) i = 0; i < n; ++i) { | ||
replacement[i] = *sorted[i]; | ||
} | ||
|
||
auto from = replacement.cbegin(); | ||
for (auto pos = begin; pos != middle; ++end) { | ||
*pos++ = *from++; | ||
} | ||
} | ||
|
||
} // namespace choose |
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