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
template <class T> | |
struct sorted_element | |
{ | |
std::remove_reference_t<T>* _data; | |
std::vector<std::size_t> const* _is; | |
sorted_element(T& elem, std::vector<std::size_t> const& is) | |
: _data(&elem), _is(&is) | |
{ | |
} |
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
sorted_view sv(first_name, last_name, sport, medals_gold, | |
medals_silver, medals_bronze, medals_total); | |
auto [bName1, bName2, bSport, bGoldM, bSilverM, bBronzeM, bTotalM] = sv; |
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
sorted_view sv1(first_name, last_name, sport, medals_gold, medals_silver); | |
sorted_view sv2(medals_bronze, medals_total); | |
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
sv.multi_sort<9, 6, 7, 8>([](auto&& a, auto&& b) { return a > b; }); |
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
template <std::size_t I, std::size_t... Is, class F> | |
void multi_sort(F&& bop) const | |
{ | |
using namespace std; | |
_is.resize(get<I>(_ts).size()); | |
iota(begin(_is), end(_is), 0); | |
std::sort(begin(_is), end(_is), [&](size_t lhs, size_t rhs) { | |
return detail::use_comparator<I, Is...>::apply( |
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
struct S { | |
int i; | |
S(int i) : i(i) {} | |
int size() { return 1; } | |
S(S const&) = delete; | |
S(S&&) = default; | |
}; | |
int main() | |
{ |
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
// 1. Create the sorted view | |
auto sv = make_sorted_view(first_name, last_name, country, sport, | |
years, gender, medals_gold, medals_silver, medals_bronze, medals_total); | |
sv.sort<0>(std::less<>{}); // 2. Sort by first name | |
for (size_t i = 0; i < sv.size<0>(); i++) printf("{ %s-%s-%s }\n", | |
sv.at<0>(i).c_str(), // 3. access the ith sorted element of sequence 0 | |
sv.at<1>(i).c_str(), | |
sv.at<2>(i).c_str()); |
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
template <std::size_t I> | |
decltype(auto) at(std::size_t pos) | |
{ | |
return std::get<I>(_ts)[_is[pos]]; | |
} |
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
template <std::size_t I, class F> | |
void sort(F&& bop) const | |
{ | |
using namespace std; | |
_is.resize(get<I>(_ts).size()); | |
iota(begin(_is), end(_is), 0); | |
std::sort(begin(_is), end(_is), [&](size_t lhs, size_t rhs) { | |
return forward<F>(bop)(get<I>(_ts)[lhs], get<I>(_ts)[rhs]); |
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
template <class... Args> | |
auto make_sorted_view(Args&&... args) | |
{ | |
return sorted_view<Args...>(std::forward<Args>(args)...); | |
} |