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