10 #ifndef LARCOREALG_COREUTILS_SORTBYPOINTER_H 11 #define LARCOREALG_COREUTILS_SORTBYPOINTER_H 21 #include <type_traits> 29 template <
typename Coll,
typename PtrColl>
41 template <
typename Coll>
57 template <
typename Coll,
typename PtrColl>
92 template <
typename Coll,
typename Sorter>
108 template <
typename Coll,
typename Sorter>
121 template <
typename Coll,
typename =
void>
129 using pointer_type = decltype(&*
begin(coll));
130 using ptr_coll_t = std::vector<pointer_type>;
132 auto const n = coll.size();
140 begin(coll),
end(coll), std::back_inserter(ptrs), [](
auto& obj) {
return &obj; });
148 template <
typename Coll>
150 std::enable_if_t<util::is_unique_ptr_v<typename Coll::value_type>>> {
156 using unique_ptr_t =
typename coll_t::value_type;
157 using value_type =
typename unique_ptr_t::element_type;
158 using pointer_type = std::add_pointer_t<value_type>;
159 using ptr_coll_t = std::vector<pointer_type>;
161 static_assert(util::is_unique_ptr_v<unique_ptr_t>);
164 auto const n =
size(coll);
172 coll.begin(), coll.end(), std::back_inserter(ptrs), [](
auto& obj) {
return obj.get(); });
185 template <
typename Coll>
192 template <
typename Coll,
typename Sorter>
217 coll = std::move(sorted);
222 template <
typename Coll,
typename Sorter>
226 using Collection_t = Coll;
227 using UPtr_t =
typename Collection_t::value_type;
229 static_assert(util::is_unique_ptr_v<UPtr_t>);
248 for (
auto const& dataPtr : ptrs) {
249 std::size_t
const originalIndex = ptrIndex.at(dataPtr);
250 sorted.emplace_back(std::move(coll[originalIndex]));
256 coll = std::move(sorted);
264 template <
typename Coll,
typename PtrColl>
267 for (
auto&& ptr : src)
268 dest.push_back(std::move(*ptr));
271 template <
typename Coll,
typename PtrColl>
273 static void move(Coll& dest, PtrColl& src)
280 template <
typename Data,
typename PtrColl>
282 static void move(std::vector<Data>& dest, PtrColl& src)
285 dest.reserve(src.size());
295 #endif // LARCOREALG_COREUTILS_SORTBYPOINTER_H
static void move(std::vector< Data > &dest, PtrColl &src)
static void move(Coll &dest, PtrColl &src)
Namespace for general, non-LArSoft-specific utilities.
Provides util::makeValueIndex() helper function.
auto makePointerVector(Coll &coll)
Creates a STL vector with pointers to data from another collection.
void SortUniquePointers(Coll &coll, Sorter &&sorter)
Sorts a vector of unique pointers using a C pointer sorter.
static auto make(Coll &coll)
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.
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.