Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:31:59

0001 #include "Utilities/General/interface/precomputed_value_sort.h"
0002 
0003 #include <vector>
0004 #include <iterator>
0005 #include <iostream>
0006 #include <cmath>
0007 
0008 using namespace std;
0009 
0010 // A fake class
0011 class Point;
0012 
0013 //Define one here to avoid coupling with other packages
0014 class Phi {
0015 public:
0016   explicit Phi(double iV) : value_(iV) {}
0017   operator double() const { return value_; }
0018 
0019 private:
0020   double value_;
0021 };
0022 
0023 class Point {
0024 public:
0025   Point(float x = 0, float y = 0);
0026   Point(const Point& p);
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 Point::Point(const Point& p) : X(p.X), Y(p.Y) { cout << "New Point (copy)" << *this << endl; }
0041 
0042 // A trivial operation on Point
0043 float extractR1(const Point& p) { return p.r(); }
0044 
0045 // Same, but on Point*
0046 float extractR2(const Point* p) { return p->r(); }
0047 
0048 // Extract phi on Point*
0049 Phi extractPhi2(const Point* p) { return p->phi(); }
0050 
0051 // A trivial (useless!) binary predicate
0052 bool lessR(const double& a, const double& b) { return a < b; }
0053 
0054 // A less trivial example: sorts angles
0055 // within any range SMALLER THAN PI "counter-clockwise"
0056 // even if the angles cross the pi boundary.
0057 // The result is undefined if the input values cover a range larger than pi!!!
0058 // note: Phi handles periodicity...
0059 bool lessDPhi(const Phi& a, const Phi& b) { return b - a > 0.; }
0060 
0061 int main() {
0062   // Create a vector with some random Points
0063   vector<Point> v1;
0064   v1.reserve(6);
0065   v1.push_back(Point(-1.343, 2.445));
0066   v1.push_back(Point(-1.566, 1.678));
0067   v1.push_back(Point(-1.678, 1.569));
0068   v1.push_back(Point(-3.138, 5.321));
0069   v1.push_back(Point(-5.12, 0.321));
0070   v1.push_back(Point(-5.12, -0.321));
0071 
0072   // A vector of pointer to Points
0073   vector<Point*> v2;
0074   for (vector<Point>::iterator i = v1.begin(); i != v1.end(); ++i) {
0075     v2.push_back(&(*i));
0076   }
0077 
0078   // Copy it
0079   vector<Point*> v3 = v2;
0080 
0081   cout << "Original vector:" << endl;
0082   copy(v1.begin(), v1.end(), ostream_iterator<Point>(cout, "\n"));
0083   cout << endl;
0084 
0085   // Sort v1
0086   precomputed_value_sort(v1.begin(), v1.end(), extractR1);
0087   cout << "Sorted with ExtractR1 : " << endl;
0088   copy(v1.begin(), v1.end(), ostream_iterator<Point>(cout, "\n"));
0089   cout << endl;
0090 
0091   // Sort v2
0092   cout << "Sorted with ExtractR2: " << endl;
0093   precomputed_value_sort(v2.begin(), v2.end(), extractR2);
0094   copy(v2.begin(), v2.end(), ostream_iterator<Point*>(cout, "\n"));
0095   cout << endl;
0096 
0097   // Sort v3 using a BinaryPredicate
0098   cout << "Sort with LessR: " << endl;
0099   precomputed_value_sort(v3.begin(), v3.end(), extractR2, lessR);
0100   copy(v3.begin(), v3.end(), ostream_iterator<Point*>(cout, "\n"));
0101   cout << endl;
0102 
0103   // Sort v3 using phi
0104   cout << "Sort with ExtractPhi2: " << endl;
0105   precomputed_value_sort(v3.begin(), v3.end(), extractPhi2);
0106   copy(v3.begin(), v3.end(), ostream_iterator<Point*>(cout, "\n"));
0107   cout << endl;
0108 
0109   // Sort v3 using a BinaryPredicate
0110   cout << "Sort with LessDPhi: " << endl;
0111   precomputed_value_sort(v3.begin(), v3.end(), extractPhi2, lessDPhi);
0112   copy(v3.begin(), v3.end(), ostream_iterator<Point*>(cout, "\n"));
0113   cout << endl;
0114 
0115   return 0;
0116 }