10 #ifndef ICARUSALG_UTILITIES_SORTBY_H
11 #define ICARUSALG_UTILITIES_SORTBY_H
20 #include <type_traits>
49 typename BIter,
typename EIter,
50 typename Key,
typename Sorter = std::less<void>
76 template <
typename Coll,
typename Key,
typename Sorter = std::less<
void>>
77 auto sortCollBy(Coll& coll, Key key, Sorter sorter = {});
88 namespace util::details {
90 template <
typename Iter>
92 std::random_access_iterator_tag,
93 typename std::iterator_traits<Iter>::iterator_category
100 typename BIter,
typename EIter,
101 typename Key,
typename Sorter
104 (BIter begin, EIter end, Key key, Sorter sorter )
114 using value_type =
typename BIter::value_type;
119 static constexpr
bool isPointer = std::is_pointer_v<value_type>;
121 using pointer_type = std::conditional_t
122 <isPointer, value_type,
typename std::iterator_traits<BIter>::pointer>;
124 using Key_t = std::decay_t<decltype(key(*begin))>;
125 using SortingPair_t = std::pair<Key_t, pointer_type>;
130 auto getPointer = [](
auto& elem)
131 {
if constexpr(isPointer)
return elem;
else return &elem; };
132 auto makePair = [&key, getPointer](
auto&& item)
133 {
return SortingPair_t(key(item), getPointer(item)); };
135 std::vector<SortingPair_t> sortingColl;
138 if constexpr(details::is_random_access_iterator_v<BIter>)
146 = [&sorter](SortingPair_t
const&
a, SortingPair_t
const& b)
147 {
return sorter(a.first, b.first); }
149 std::sort(sortingColl.begin(), sortingColl.end(), pair_sorter);
155 std::vector<pointer_type> sortedColl;
156 sortedColl.reserve(sortingColl.size());
158 sortingColl.begin(), sortingColl.end(), back_inserter(sortedColl),
159 [](SortingPair_t
const& pair){
return pair.second; }
168 template <
typename Coll,
typename Key,
typename Sorter >
178 #endif // ICARUSALG_UTILITIES_SORTBY_H
auto sortBy(BIter begin, EIter end, Key key, Sorter sorter={})
Returns a vectors to pointers to coll elements, sorted by key.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
auto end(FixedBins< T, C > const &) noexcept
auto sortCollBy(Coll &coll, Key key, Sorter sorter={})
Returns a vectors to pointers to coll elements, sorted by key.
auto begin(FixedBins< T, C > const &) noexcept
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
constexpr bool is_random_access_iterator_v