Project CMSSW displayed by LXR

File indexing completed on 2024-04-06 12:31:46

```0001 #include "Utilities/General/interface/precomputed_value_sort.h"
0002
0003 #include <vector>
0004 #include <iterator>
0005 #include <iostream>
0006 #include <cmath>
0007 #include <cassert>
0008
0009 using namespace std;
0010
0011 // A fake class
0012 class Point;
0013
0014 //Define one here to avoid coupling with other packages
0015 class Phi {
0016 public:
0017   explicit Phi(double iV) : value_(iV) {}
0018   operator double() const { return value_; }
0019
0020 private:
0021   double value_;
0022 };
0023
0024 class Point {
0025 public:
0026   Point(float x = 0, float y = 0);
0027   float r() const { return sqrt(X * X + Y * Y); }
0028   Phi phi() const { return Phi(atan2(Y, X)); }
0029   float X, Y;
0030 };
0031
0032 ostream& operator<<(ostream& o, const Point& p) {
0033   return o << "[p=(" << p.X << "," << p.Y << "); r=" << p.r() << " phi=" << p.phi() << "]";
0034 }
0035
0036 ostream& operator<<(ostream& o, const Point* p) { return o << *p; }
0037
0038 Point::Point(float x, float y) : X(x), Y(y) { cout << "New Point" << *this << endl; }
0039
0040 // A trivial operation on Point
0041 float extractR1(const Point& p) { return p.r(); }
0042
0043 // Same, but on Point*
0044 float extractR2(const Point* p) { return p->r(); }
0045
0046 // Extract phi on Point*
0047 Phi extractPhi2(const Point* p) { return p->phi(); }
0048
0049 // A trivial (useless!) binary predicate
0050 bool lessR(const double& a, const double& b) { return a < b; }
0051
0052 // A less trivial example: sorts angles
0053 // within any range SMALLER THAN PI "counter-clockwise"
0054 // even if the angles cross the pi boundary.
0055 // The result is undefined if the input values cover a range larger than pi!!!
0056 // note: Phi handles periodicity...
0057 bool lessDPhi(const Phi& a, const Phi& b) { return b - a > 0.; }
0058
0059 int main() {
0060   // Create a vector with some random Points
0061   vector<Point> v1;
0062   v1.reserve(6);
0063   v1.push_back(Point(-1.343, 2.445));
0064   v1.push_back(Point(-1.566, 1.678));
0065   v1.push_back(Point(-1.678, 1.569));
0066   v1.push_back(Point(-3.138, 5.321));
0067   v1.push_back(Point(-5.12, 0.321));
0068   v1.push_back(Point(-5.12, -0.321));
0069
0070   vector<float> r;
0071   r.reserve(v1.size());
0072   vector<float> phi;
0073   phi.reserve(v1.size());
0074   for (vector<Point>::iterator i = v1.begin(); i != v1.end(); ++i) {
0075     r.push_back(i->r());
0076     phi.push_back(i->phi());
0077   }
0078   std::sort(r.begin(), r.end());
0079   std::sort(phi.begin(), phi.end());
0080
0081   cout << endl;
0082   cout << "Sorted R's:" << endl;
0083   for (size_t i = 0; i < r.size(); i++) {
0084     cout << r[i] << endl;
0085   }
0086
0087   cout << endl;
0088   cout << "Sorted Phi's:" << endl;
0089   for (size_t i = 0; i < phi.size(); i++) {
0090     cout << phi[i] << endl;
0091   }
0092   cout << endl;
0093
0094   // A vector of pointer to Points
0095   vector<Point*> v2;
0096   for (vector<Point>::iterator i = v1.begin(); i != v1.end(); ++i) {
0097     v2.push_back(&(*i));
0098   }
0099
0100   // Copy it
0101   vector<Point*> v3 = v2;
0102
0103   cout << "Original vector:" << endl;
0104   copy(v1.begin(), v1.end(), ostream_iterator<Point>(cout, "\n"));
0105   cout << endl;
0106
0107   // Sort v1
0108   precomputed_value_sort(v1.begin(), v1.end(), extractR1);
0109   cout << "Sorted with ExtractR1 : " << endl;
0110   copy(v1.begin(), v1.end(), ostream_iterator<Point>(cout, "\n"));
0111   cout << endl;
0112   for (size_t i = 0; i < r.size(); i++) {
0113     assert(v1[i].r() == r[i]);
0114   }
0115
0116   // Sort v2
0117   cout << "Sorted with ExtractR2: " << endl;
0118   precomputed_value_sort(v2.begin(), v2.end(), extractR2);
0119   copy(v2.begin(), v2.end(), ostream_iterator<Point*>(cout, "\n"));
0120   cout << endl;
0121   for (size_t i = 0; i < r.size(); i++) {
0122     assert(v2[i]->r() == r[i]);
0123   }
0124
0125   // Sort v3 using a BinaryPredicate
0126   cout << "Sort with LessR: " << endl;
0127   precomputed_value_sort(v3.begin(), v3.end(), extractR2, lessR);
0128   copy(v3.begin(), v3.end(), ostream_iterator<Point*>(cout, "\n"));
0129   cout << endl;
0130   for (size_t i = 0; i < r.size(); i++) {
0131     assert(v3[i]->r() == r[i]);
0132   }
0133
0134   // Sort v2 using phi
0135   cout << "Sort with ExtractPhi2: " << endl;
0136   precomputed_value_sort(v2.begin(), v2.end(), extractPhi2);
0137   copy(v2.begin(), v2.end(), ostream_iterator<Point*>(cout, "\n"));
0138   cout << endl;
0139   for (size_t i = 0; i < phi.size(); i++) {
0140     assert(v2[i]->phi() == phi[i]);
0141   }
0142
0143   // Sort v3 using a BinaryPredicate
0144   cout << "Sort with LessDPhi: " << endl;
0145   precomputed_value_sort(v3.begin(), v3.end(), extractPhi2, lessDPhi);
0146   copy(v3.begin(), v3.end(), ostream_iterator<Point*>(cout, "\n"));
0147   cout << endl;
0148   for (size_t i = 0; i < phi.size(); i++) {
0149     assert(v3[i]->phi() == phi[i]);
0150   }
0151
0152   return 0;
0153 }```