LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
lar_content::LArUtilityHelper Class Reference

LArUtilityHelper class. More...

#include "LArUtilityHelper.h"

Static Public Member Functions

template<typename T , typename Comparison >
static std::vector< std::size_t > GetSortIndices (const std::vector< T > &input, Comparison &compare)
 Determine the permutation that would apply to the elements of a vector if sorted in ascending order. More...
 
template<typename T >
static void SortByIndices (const std::vector< std::size_t > &order, std::vector< T > &vector)
 Sort a vector in place based on a supplied index ordering. More...
 

Detailed Description

LArUtilityHelper class.

Definition at line 21 of file LArUtilityHelper.h.

Member Function Documentation

template<typename T , typename Comparison >
std::vector< std::size_t > lar_content::LArUtilityHelper::GetSortIndices ( const std::vector< T > &  input,
Comparison &  compare 
)
static

Determine the permutation that would apply to the elements of a vector if sorted in ascending order.

Parameters
inputthe vector for which an order is to be determined
comparethe function to use for element comparisons
Returns
The order of indices determined by the sort operation

Definition at line 47 of file LArUtilityHelper.h.

48 {
49  std::vector<std::size_t> order(input.size());
50  std::iota(order.begin(), order.end(), 0);
51  std::sort(order.begin(), order.end(), [&](std::size_t i, std::size_t j) { return compare(input[i], input[j]); });
52 
53  return order;
54 }
template<typename T >
void lar_content::LArUtilityHelper::SortByIndices ( const std::vector< std::size_t > &  order,
std::vector< T > &  vector 
)
static

Sort a vector in place based on a supplied index ordering.

Parameters
orderthe index ordering that should be applied to the vector
vectorthe vector to be sorted in place

Definition at line 57 of file LArUtilityHelper.h.

58 {
59  std::vector<bool> done(vector.size());
60  for (std::size_t i = 0; i < vector.size(); ++i)
61  {
62  if (done[i])
63  continue;
64  done[i] = true;
65  std::size_t idx1{i};
66  std::size_t idx2{order[i]};
67  // keep swapping indices until we move the matching index into the current index
68  while (i != idx2)
69  {
70  std::swap(vector[idx1], vector[idx2]);
71  done[idx2] = true;
72  idx1 = idx2;
73  idx2 = order[idx2];
74  }
75  }
76 }
void swap(lar::deep_const_fwd_iterator_nested< CITER, INNERCONTEXTRACT > &a, lar::deep_const_fwd_iterator_nested< CITER, INNERCONTEXTRACT > &b)

The documentation for this class was generated from the following file: