Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:15

0001 //FJSTARTHEADER
0002 // $Id$
0003 //
0004 // Copyright (c) 2005-2021, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
0005 //
0006 //----------------------------------------------------------------------
0007 // This file is part of FastJet.
0008 //
0009 //  FastJet is free software; you can redistribute it and/or modify
0010 //  it under the terms of the GNU General Public License as published by
0011 //  the Free Software Foundation; either version 2 of the License, or
0012 //  (at your option) any later version.
0013 //
0014 //  The algorithms that underlie FastJet have required considerable
0015 //  development. They are described in the original FastJet paper,
0016 //  hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
0017 //  FastJet as part of work towards a scientific publication, please
0018 //  quote the version you use and include a citation to the manual and
0019 //  optionally also to hep-ph/0512210.
0020 //
0021 //  FastJet is distributed in the hope that it will be useful,
0022 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
0023 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0024 //  GNU General Public License for more details.
0025 //
0026 //  You should have received a copy of the GNU General Public License
0027 //  along with FastJet. If not, see <http://www.gnu.org/licenses/>.
0028 //----------------------------------------------------------------------
0029 //FJENDHEADER
0030 
0031 #ifndef __FASTJET_CLOSESTPAIR2D__HH__
0032 #define __FASTJET_CLOSESTPAIR2D__HH__
0033 
0034 #include<vector>
0035 #include<stack>
0036 #include<iostream>
0037 #include "fastjet/internal/ClosestPair2DBase.hh"
0038 #include "fastjet/internal/SearchTree.hh"
0039 #include "fastjet/internal/MinHeap.hh"
0040 #include "fastjet/SharedPtr.hh"
0041 
0042 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
0043 
0044 //----------------------------------------------------------------------
0045 /// \if internal_doc
0046 /// @ingroup internal
0047 /// \class ClosestPair2D
0048 /// concrete implementation for finding closest pairs in 2D -- will
0049 /// use Chan's (hopefully efficient) shuffle based structures
0050 /// \endif
0051 class ClosestPair2D : public ClosestPair2DBase {
0052 public:
0053   /// constructor from a vector of 2D positions -- number of objects
0054   /// after insertion and deletion must never exceed positions.size();
0055   /// objects are given IDs that correspond to their index in the vector 
0056   /// of positions
0057   ClosestPair2D(const std::vector<Coord2D> & positions, 
0058         const Coord2D & left_corner, const Coord2D & right_corner) {
0059     _initialize(positions, left_corner, right_corner, positions.size());
0060   };
0061 
0062   /// constructor which allows structure to grow beyond positions.size(), up
0063   /// to max_size
0064   ClosestPair2D(const std::vector<Coord2D> & positions, 
0065         const Coord2D & left_corner, const Coord2D & right_corner,
0066         const unsigned int max_size) {
0067     _initialize(positions, left_corner, right_corner, max_size);
0068   };
0069 
0070   /// provides the IDs of the closest pair as well as the distance between
0071   /// them
0072   void closest_pair(unsigned int & ID1, unsigned int & ID2, 
0073             double & distance2) const;
0074 
0075   /// removes the entry labelled by ID from the object;
0076   void remove(unsigned int ID);
0077  
0078   /// inserts the position into the closest pair structure and returns the
0079   /// ID that has been allocated for the object.
0080   unsigned int insert(const Coord2D &);
0081 
0082   /// removes ID1 and ID2 and inserts position, returning the ID 
0083   /// corresponding to position...
0084   virtual unsigned int replace(unsigned int ID1, unsigned int ID2, 
0085                    const Coord2D & position);
0086 
0087   /// replaces IDs_to_remove with points at the new_positions
0088   /// indicating the IDs allocated to the new points in new_IDs
0089   virtual void replace_many(const std::vector<unsigned int> & IDs_to_remove,
0090                 const std::vector<Coord2D> & new_positions,
0091                 std::vector<unsigned int> & new_IDs);
0092 
0093   // mostly for checking how things are working...
0094   inline void print_tree_depths(std::ostream & outdev) const {
0095     outdev    << _trees[0]->max_depth() << " "
0096           << _trees[1]->max_depth() << " "
0097           << _trees[2]->max_depth() << "\n";
0098   };
0099 
0100   unsigned int size();
0101 
0102 private:
0103   
0104   void _initialize(const std::vector<Coord2D> & positions, 
0105           const Coord2D & left_corner, const Coord2D & right_corner,
0106           const unsigned int max_size);
0107 
0108   static const unsigned int _nshift = 3;
0109 
0110   class Point; // will be defined below
0111 
0112   /// since sets of three objects will crop up repeatedly, useful
0113   /// to have a triplet class?
0114   template<class T> class triplet {
0115   public:
0116     inline const T & operator[](unsigned int i) const {return _contents[i];};
0117     inline       T & operator[](unsigned int i)       {return _contents[i];};
0118   private:
0119     T _contents[_nshift];
0120   };
0121 
0122 
0123   /// class that will take care of ordering of shuffles for us
0124   class Shuffle {
0125   public:
0126     unsigned int x, y;
0127     Point * point;
0128     bool operator<(const Shuffle &) const;
0129     void operator+=(unsigned int shift) {x += shift; y+= shift;};
0130   };
0131 
0132   typedef SearchTree<Shuffle>     Tree;
0133   typedef Tree::circulator        circulator;
0134   typedef Tree::const_circulator  const_circulator;
0135 
0136 
0137   triplet<SharedPtr<Tree> >  _trees;
0138   SharedPtr<MinHeap>     _heap;
0139   std::vector<Point>     _points;
0140   std::stack<Point *>    _available_points;
0141 
0142   /// points that are "under review" in some way
0143   std::vector<Point *>   _points_under_review;
0144 
0145   // different statuses for review
0146   static const unsigned int _remove_heap_entry = 1;
0147   static const unsigned int _review_heap_entry = 2;
0148   static const unsigned int _review_neighbour  = 4;
0149 
0150   /// add a label to a point as to the nature of review needed
0151   /// (includes adding it to list of points needing review) [doesn't
0152   /// affect other labels already set for the point]
0153   void _add_label(Point * point, unsigned int review_flag);
0154 
0155   /// sets the label for the point to be exclusively this 
0156   /// review flag (and adds it to list of points needing review
0157   /// if not already there)
0158   void _set_label(Point * point, unsigned int review_flag);
0159 
0160   /// for all entries of the _points_under_review[] vector, carry out
0161   /// the actions indicated by its review flag; the points are
0162   /// then removed from _points_under_review[] and their flags
0163   /// set to zero
0164   void _deal_with_points_to_review();
0165 
0166   /// carry out the search-tree related operations of point removal
0167   void _remove_from_search_tree(Point * point_to_remove);
0168 
0169   /// carry out the search-tree related operations of point insertion
0170   void _insert_into_search_tree(Point * new_point);
0171 
0172   /// takes a point and creates a shuffle with the given shift
0173   void _point2shuffle(Point & , Shuffle & , unsigned int shift);
0174 
0175   /// pieces needed for converting coordinates to integer
0176   Coord2D _left_corner;
0177   double _range;
0178 
0179   int _ID(const Point *) const;
0180 
0181   triplet<unsigned int> _shifts;     // absolute shifts
0182   triplet<unsigned int> _rel_shifts; // shifts relative to previous shift
0183 
0184   unsigned int _cp_search_range;
0185 };
0186 
0187 
0188 //----------------------------------------------------------------------
0189 /// \if internal_doc
0190 /// @ingroup internal
0191 /// \class ClosestPair2D::Point
0192 /// class for representing all info needed about a point
0193 /// \endif
0194 class ClosestPair2D::Point {
0195 public:
0196   /// the point's coordinates
0197   Coord2D coord;
0198   /// a pointer to its closest neighbour in our structure
0199   Point * neighbour;
0200   /// the corresponding squared distance
0201   double  neighbour_dist2;
0202   /// circulators for each of the shifts of the shuffles
0203   triplet<circulator> circ;
0204 
0205   /// indicates that something special is currently happening to this point
0206   unsigned int review_flag;
0207 
0208   /// returns the distance between two of these objects
0209   double distance2(const Point & other) const {
0210     return coord.distance2(other.coord);
0211   };
0212 
0213   /// creates a shuffle for us with a given shift
0214   //void set_shuffle(Shuffle & shuffle);
0215 };
0216 
0217 
0218 //----------------------------------------------------------------------
0219 /// returns true if floor(ln_base2(x)) < floor(ln_base2(y)), using
0220 /// Chan's neat trick...
0221 inline bool floor_ln2_less(unsigned x, unsigned y) {
0222   if (x>y) return false;
0223   return (x < (x^y)); // beware of operator precedence...
0224 }
0225 
0226 
0227 //----------------------------------------------------------------------
0228 /// returns the ID for the specified point...
0229 inline int ClosestPair2D::_ID(const Point * point) const {
0230   return point - &(_points[0]);
0231 }
0232 
0233 
0234 //
0235 inline unsigned int ClosestPair2D::size() {
0236   return _points.size() - _available_points.size();
0237 }
0238 
0239 
0240 
0241 FASTJET_END_NAMESPACE
0242 
0243 #endif // __FASTJET_CLOSESTPAIR2D__HH__