10 #ifndef LARCOREALG_COREUTILS_SORTBYPOINTER_H
11 #define LARCOREALG_COREUTILS_SORTBYPOINTER_H
23 #include <type_traits>
30 template <
typename Coll,
typename PtrColl>
43 template <
typename Coll>
60 template <
typename Coll,
typename PtrColl>
94 template <
typename Coll,
typename Sorter>
111 template <
typename Coll,
typename Sorter>
123 namespace util::details {
126 template <
typename Coll,
typename =
void>
133 using pointer_type = decltype(&*
begin(coll));
134 using ptr_coll_t = std::vector<pointer_type>;
136 auto const n = coll.size();
144 [](
auto& obj){
return &obj; });
153 template <
typename Coll>
155 <Coll,
std::
enable_if_t<util::is_unique_ptr_v<typename Coll::value_type>>>
163 using unique_ptr_t =
typename coll_t::value_type;
164 using value_type =
typename unique_ptr_t::element_type;
165 using pointer_type = std::add_pointer_t<value_type>;
166 using ptr_coll_t = std::vector<pointer_type>;
168 static_assert(util::is_unique_ptr_v<unique_ptr_t>);
171 auto const n =
size(coll);
178 std::transform(coll.begin(), coll.end(), std::back_inserter(ptrs),
179 [](
auto& obj){
return obj.get(); });
197 template <
typename Coll>
203 template <
typename Coll,
typename Sorter>
227 coll = std::move(sorted);
233 template <
typename Coll,
typename Sorter>
236 using Collection_t = Coll;
237 using UPtr_t =
typename Collection_t::value_type;
239 static_assert(util::is_unique_ptr_v<UPtr_t>);
258 for (
auto const& dataPtr: ptrs) {
259 std::size_t
const originalIndex = ptrIndex.at(dataPtr);
260 sorted.emplace_back(std::move(coll[originalIndex]));
266 coll = std::move(sorted);
275 template <
typename Coll,
typename PtrColl>
277 {
for (
auto&& ptr: src) dest.push_back(std::move(*ptr)); }
280 template <
typename Coll,
typename PtrColl>
281 struct MoveFromPointersImpl {
282 static void move(Coll& dest, PtrColl& src)
290 template <
typename Data,
typename PtrColl>
292 static void move(std::vector<Data>& dest, PtrColl& src)
295 dest.reserve(src.size());
306 #endif // LARCOREALG_COREUTILS_SORTBYPOINTER_H
static void move(std::vector< Data > &dest, PtrColl &src)
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
static void move(Coll &dest, PtrColl &src)
Provides util::makeValueIndex() helper function.
auto makePointerVector(Coll &coll)
Creates a STL vector with pointers to data from another collection.
std::size_t size(FixedBins< T, C > const &) noexcept
void SortUniquePointers(Coll &coll, Sorter &&sorter)
Sorts a vector of unique pointers using a C pointer sorter.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
void SortByPointers(Coll &coll, Sorter sorter)
Applies sorting indirectly, minimizing data copy.
auto end(FixedBins< T, C > const &) noexcept
typename std::enable_if< B, T >::type enable_if_t
auto begin(FixedBins< T, C > const &) noexcept
static auto make(Coll &coll)
void moveFromPointersImplBase(Coll &dest, PtrColl &src)
static auto make(Coll &coll)
void MoveFromPointers(Coll &dest, PtrColl &src)
Moves the content from a collection of pointers to one of data.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
decltype(auto) makeValueIndex(Coll const &coll, Extractor getter)
Returns a map of value to index.