Go to the documentation of this file.00001 #ifndef GeneratorInterface_LHEInterface_Matching_h
00002 #define GeneratorInterface_LHEInterface_Matching_h
00003
00004 #include <functional>
00005 #include <algorithm>
00006 #include <vector>
00007 #include <set>
00008
00009 #include "SimpleMatrix.h"
00010
00011 namespace lhef {
00012
00013 template<typename Delta>
00014 class Matching {
00015 public:
00016 typedef typename SimpleMatrix<Delta>::size_type index_type;
00017
00018 template<typename V1, typename V2, class Separation>
00019 Matching(const V1 &v1, const V2 &v2, Separation separation) :
00020 matrix(v1.size(), v2.size()),
00021 matched1(v1.size(), false),
00022 matched2(v2.size(), false)
00023 {
00024 index_type i = 0;
00025 for(typename V1::const_iterator iter1 = v1.begin();
00026 iter1 != v1.end(); ++iter1, i++) {
00027 index_type j = 0;
00028 for(typename V2::const_iterator iter2 = v2.begin();
00029 iter2 != v2.end(); ++iter2, j++)
00030 matrix(i, j) = separation(*iter1, *iter2);
00031
00032 }
00033 }
00034
00035 struct Match {
00036 typedef typename Matching::index_type index_type;
00037
00038 inline Match(index_type i1, index_type i2) :
00039 index1(i1), index2(i2) {}
00040
00041 index_type index1, index2;
00042 };
00043
00044 inline Delta delta(index_type index1, index_type index2) const
00045 { return matrix(index1, index2); }
00046
00047 inline Delta delta(Match match) const
00048 { return matrix(match.index1, match.index2); }
00049
00050 private:
00051 template<class SortComparator>
00052 struct Comparator : public std::binary_function<Delta, Delta, bool> {
00053 typedef typename Matching::index_type index_type;
00054
00055 inline Comparator(const SimpleMatrix<Delta> &matrix,
00056 SortComparator &sort) :
00057 matrix(matrix), sort(sort) {}
00058
00059 inline bool operator () (index_type i1, index_type i2) const
00060 { return sort(matrix[i1], matrix[i2]); }
00061
00062 const SimpleMatrix<Delta> &matrix;
00063 SortComparator &sort;
00064 };
00065
00066 struct AlwaysTrue : public std::unary_function<Delta, bool> {
00067 inline bool operator () (Delta dummy) { return true; }
00068 };
00069
00070 public:
00071 template<class SortComparator, class CutCriterion>
00072 std::vector<Match> match(
00073 SortComparator sortComparator = SortComparator(),
00074 CutCriterion cutCriterion = CutCriterion())
00075 {
00076 std::vector<index_type> matches(matrix.size());
00077 for(index_type i = 0; i != matrix.size(); i++)
00078 matches[i] = i;
00079
00080 std::sort(matches.begin(), matches.end(),
00081 Comparator<SortComparator>(matrix, sortComparator));
00082
00083 std::vector<Match> result;
00084 result.reserve(std::min(matrix.rows(), matrix.cols()));
00085 for(typename std::vector<index_type>::const_iterator iter =
00086 matches.begin(); iter != matches.end(); ++iter) {
00087
00088 index_type row = matrix.row(*iter);
00089 index_type col = matrix.col(*iter);
00090 if (matched1[row] || matched2[col])
00091 continue;
00092
00093 if (!cutCriterion(matrix[*iter]))
00094 continue;
00095
00096 matched1[row] = true;
00097 matched2[col] = true;
00098 result.push_back(Match(row, col));
00099 }
00100
00101 return result;
00102 }
00103
00104 template<class SortComparator>
00105 inline std::vector<Match> match()
00106 { return match<SortComparator, AlwaysTrue>(); }
00107
00108 inline std::vector<Match> match()
00109 { return match<std::less<Delta>, AlwaysTrue>(); }
00110
00111 inline bool isMatched1st(index_type index) { return matched1[index]; }
00112 inline bool isMatched2nd(index_type index) { return matched2[index]; }
00113
00114 private:
00115 SimpleMatrix<Delta> matrix;
00116 std::vector<bool> matched1, matched2;
00117 };
00118
00119 }
00120
00121 #endif // GeneratorEvent_LHEInterface_Matching_h