CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
precomputed_value_sort.h
Go to the documentation of this file.
1 #ifndef precomputed_value_sort_H
2 #define precomputed_value_sort_H
3 
15 #include <utility>
16 #include <vector>
17 #include <algorithm>
18 
19 namespace {
20  template<class T, class Scalar>
21  struct LessPair {
22  typedef std::pair<T,Scalar> SortPair;
23  bool operator()( const SortPair& a, const SortPair& b) {
24  return a.second < b.second;
25  }
26  };
27 
28  template <class T, class Scalar, class BinaryPredicate>
29  struct ComparePair {
30  ComparePair( const BinaryPredicate& cmp) : cmp_(cmp) {}
31  typedef std::pair<T,Scalar> SortPair;
32  bool operator()( const SortPair& a, const SortPair& b) {
33  return cmp_(a.second, b.second);
34  }
35  BinaryPredicate cmp_;
36  };
37 }
38 
39 
40 template<class RandomAccessIterator, class Extractor>
41 void precomputed_value_sort(RandomAccessIterator begin,
42  RandomAccessIterator end,
43  const Extractor& extr) {
44 
45  typedef typename Extractor::result_type Scalar;
46  typedef std::pair<RandomAccessIterator,Scalar> SortPair;
47 
48  std::vector<SortPair> tmpvec;
49  tmpvec.reserve(end-begin);
50 
51  // tmpvec holds iterators - does not copy the real objects
52  for (RandomAccessIterator i=begin; i!=end; i++) {
53  tmpvec.push_back(SortPair(i,extr(*i)));
54  }
55 
56  std::sort(tmpvec.begin(), tmpvec.end(),
57  LessPair<RandomAccessIterator,Scalar>());
58 
59  // overwrite the input range with the sorted values
60  // copy of input container not necessary, but tricky to avoid
62  for (unsigned int i=0; i<tmpvec.size(); i++) {
63  *(begin+i) = tmpcopy[tmpvec[i].first - begin];
64  }
65 }
66 
67 
69 
70 template<class RandomAccessIterator, class Extractor, class BinaryPredicate>
71 void precomputed_value_sort( RandomAccessIterator begin,
72  RandomAccessIterator end,
73  const Extractor& extr,
74  const BinaryPredicate& pred) {
75 
76  typedef typename Extractor::result_type Scalar;
77  typedef std::pair<RandomAccessIterator,Scalar> SortPair;
78 
79  std::vector<SortPair> tmpvec;
80  tmpvec.reserve(end-begin);
81 
82  // tmpvec holds iterators - does not copy the real objects
83  for (RandomAccessIterator i=begin; i!=end; i++) {
84  tmpvec.push_back(SortPair(i,extr(*i)));
85  }
86 
87  std::sort(tmpvec.begin(), tmpvec.end(),
88  ComparePair< RandomAccessIterator,Scalar,BinaryPredicate>(pred));
89 
90  // overwrite the input range with the sorted values
91  // copy of input container not necessary, but tricky to avoid
93  for (unsigned int i=0; i<tmpvec.size(); i++) {
94  *(begin+i) = tmpcopy[tmpvec[i].first - begin];
95  }
96 
97 }
98 
99 #endif
int i
Definition: DBlmapReader.cc:9
double Scalar
Definition: Definitions.h:27
#define end
Definition: vmac.h:38
bool first
Definition: L1TdeRCT.cc:94
Container::value_type value_type
double b
Definition: hdecay.h:120
#define begin
Definition: vmac.h:31
void precomputed_value_sort(RandomAccessIterator begin, RandomAccessIterator end, const Extractor &extr)
double a
Definition: hdecay.h:121