File indexing completed on 2024-04-06 12:25:22
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef RecoJets_FFTJetAlgorithms_matchOneToOne_h
0012 #define RecoJets_FFTJetAlgorithms_matchOneToOne_h
0013
0014 #include <vector>
0015 #include <algorithm>
0016
0017 namespace fftjetcms {
0018 namespace Private {
0019 struct matchOneToOne_MatchInfo {
0020 double distance;
0021 unsigned i1;
0022 unsigned i2;
0023
0024 inline bool operator<(const matchOneToOne_MatchInfo& r) const { return distance < r.distance; }
0025
0026 inline bool operator>(const matchOneToOne_MatchInfo& r) const { return distance > r.distance; }
0027 };
0028 }
0029
0030
0031
0032
0033
0034
0035
0036
0037 template <class T1, class T2, class DistanceCalculator>
0038 unsigned matchOneToOne(const std::vector<T1>& v1,
0039 const std::vector<T2>& v2,
0040 const DistanceCalculator& calc,
0041 std::vector<int>* matchFrom1To2,
0042 const double maxMatchingDistance = 1.0e300) {
0043 unsigned nused = 0;
0044 matchFrom1To2->clear();
0045
0046 const unsigned n1 = v1.size();
0047 if (n1) {
0048 matchFrom1To2->reserve(n1);
0049 for (unsigned i1 = 0; i1 < n1; ++i1)
0050 matchFrom1To2->push_back(-1);
0051
0052 const unsigned n2 = v2.size();
0053 if (n2) {
0054 const unsigned nmatches = n1 * n2;
0055 std::vector<Private::matchOneToOne_MatchInfo> distanceTable(nmatches);
0056 std::vector<int> taken2(n2);
0057
0058 for (unsigned i2 = 0; i2 < n2; ++i2)
0059 taken2[i2] = 0;
0060
0061 Private::matchOneToOne_MatchInfo* m;
0062 for (unsigned i1 = 0; i1 < n1; ++i1)
0063 for (unsigned i2 = 0; i2 < n2; ++i2) {
0064 m = &distanceTable[i1 * n2 + i2];
0065 m->distance = calc(v1[i1], v2[i2]);
0066 m->i1 = i1;
0067 m->i2 = i2;
0068 }
0069
0070 std::sort(distanceTable.begin(), distanceTable.end());
0071 for (unsigned i = 0; i < nmatches && nused < n1 && nused < n2; ++i) {
0072 m = &distanceTable[i];
0073 if (m->distance > maxMatchingDistance)
0074 break;
0075 if ((*matchFrom1To2)[m->i1] < 0 && !taken2[m->i2]) {
0076 (*matchFrom1To2)[m->i1] = static_cast<int>(m->i2);
0077 taken2[m->i2] = 1;
0078 ++nused;
0079 }
0080 }
0081 }
0082 }
0083
0084 return nused;
0085 }
0086 }
0087
0088 #endif