CMS 3D CMS Logo

precomputed_value_sort.h
Go to the documentation of this file.
1 #ifndef precomputed_value_sort_H
2 #define precomputed_value_sort_H
3 
12 #include <vector>
13 #include <algorithm>
14 #include <functional>
15 
16 template<class RandomAccessIterator, class Extractor, class Compare>
17 void precomputed_value_sort( RandomAccessIterator begin, RandomAccessIterator end,
18  const Extractor& extr, const Compare& comp )
19 {
21  using Scalar = decltype(extr(*begin));
22 
23  std::vector<std::pair<RandomAccessIterator,Scalar>> tmpvec;
24  tmpvec.reserve(end-begin);
25 
26  // tmpvec holds iterators - does not copy the real objects
27  for (RandomAccessIterator i=begin; i!=end; i++) tmpvec.emplace_back(i,extr(*i));
28 
29  std::sort(tmpvec.begin(), tmpvec.end(),[&comp](auto const& a, auto const& b){
30  return comp(a.second, b.second);
31  });
32 
33  // overwrite the input range with the sorted values
34  // copy of input container not necessary, but tricky to avoid
35  std::vector<Value> tmpcopy(begin,end);
36  for (unsigned int i=0; i < tmpvec.size(); i++) {
37  *(begin+i) = std::move(tmpcopy[tmpvec[i].first - begin]);
38  }
39 }
40 
41 template<class RandomAccessIterator, class Extractor>
42 void precomputed_value_sort( RandomAccessIterator begin, RandomAccessIterator end,
43  const Extractor& extr )
44 {
45  using Scalar = decltype(extr(*begin));
46  precomputed_value_sort(begin, end, extr, std::less<Scalar>());
47 }
48 
49 #endif
double Scalar
Definition: Definitions.h:27
reco::JetExtendedAssociation::JetExtendedData Value
#define end
Definition: vmac.h:39
void precomputed_value_sort(RandomAccessIterator begin, RandomAccessIterator end, const Extractor &extr, const Compare &comp)
double b
Definition: hdecay.h:120
#define begin
Definition: vmac.h:32
double a
Definition: hdecay.h:121
def move(src, dest)
Definition: eostools.py:511