Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:57:11

0001 //FJSTARTHEADER
0002 // $Id$
0003 //
0004 // Copyright (c) 2005-2025, 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 
0111   /// since sets of three objects will crop up repeatedly, useful
0112   /// to have a triplet class?
0113   template<class T> class triplet {
0114   public:
0115     inline const T & operator[](unsigned int i) const {return _contents[i];};
0116     inline       T & operator[](unsigned int i)       {return _contents[i];};
0117   private:
0118     T _contents[_nshift];
0119   };
0120 
0121   // forward declaration
0122   class Point;
0123 
0124   /// class that will take care of ordering of shuffles for us
0125   class Shuffle {
0126   public:
0127     unsigned int x, y;
0128     Point * point;
0129     bool operator<(const Shuffle &) const;
0130     void operator+=(unsigned int shift) {x += shift; y+= shift;};
0131   };
0132 
0133   typedef SearchTree<Shuffle>     Tree;
0134   typedef Tree::circulator        circulator;
0135   typedef Tree::const_circulator  const_circulator;
0136 
0137   //----------------------------------------------------------------------
0138   /// \if internal_doc
0139   /// @ingroup internal
0140   /// \class ClosestPair2D::Point
0141   /// class for representing all info needed about a point
0142   /// \endif
0143   class Point {
0144   public:
0145     /// the point's coordinates
0146     Coord2D coord;
0147     /// a pointer to its closest neighbour in our structure
0148     Point * neighbour;
0149     /// the corresponding squared distance
0150     double  neighbour_dist2;
0151     /// circulators for each of the shifts of the shuffles
0152     triplet<circulator> circ;
0153 
0154     /// indicates that something special is currently happening to this point
0155     unsigned int review_flag;
0156 
0157     /// returns the distance between two of these objects
0158     double distance2(const Point & other) const {
0159       return coord.distance2(other.coord);
0160     };
0161 
0162     /// creates a shuffle for us with a given shift
0163     //void set_shuffle(Shuffle & shuffle);
0164   };
0165 
0166   triplet<SharedPtr<Tree> >  _trees;
0167   SharedPtr<MinHeap>     _heap;
0168   std::vector<Point>     _points;
0169   std::stack<Point *>    _available_points;
0170 
0171   /// points that are "under review" in some way
0172   std::vector<Point *>   _points_under_review;
0173 
0174   // different statuses for review
0175   static const unsigned int _remove_heap_entry = 1;
0176   static const unsigned int _review_heap_entry = 2;
0177   static const unsigned int _review_neighbour  = 4;
0178 
0179   /// add a label to a point as to the nature of review needed
0180   /// (includes adding it to list of points needing review) [doesn't
0181   /// affect other labels already set for the point]
0182   void _add_label(Point * point, unsigned int review_flag);
0183 
0184   /// sets the label for the point to be exclusively this 
0185   /// review flag (and adds it to list of points needing review
0186   /// if not already there)
0187   void _set_label(Point * point, unsigned int review_flag);
0188 
0189   /// for all entries of the _points_under_review[] vector, carry out
0190   /// the actions indicated by its review flag; the points are
0191   /// then removed from _points_under_review[] and their flags
0192   /// set to zero
0193   void _deal_with_points_to_review();
0194 
0195   /// carry out the search-tree related operations of point removal
0196   void _remove_from_search_tree(Point * point_to_remove);
0197 
0198   /// carry out the search-tree related operations of point insertion
0199   void _insert_into_search_tree(Point * new_point);
0200 
0201   /// takes a point and creates a shuffle with the given shift
0202   void _point2shuffle(Point & , Shuffle & , unsigned int shift);
0203 
0204   /// pieces needed for converting coordinates to integer
0205   Coord2D _left_corner;
0206   double _range;
0207 
0208   int _ID(const Point *) const;
0209 
0210   triplet<unsigned int> _shifts;     // absolute shifts
0211   triplet<unsigned int> _rel_shifts; // shifts relative to previous shift
0212 
0213   unsigned int _cp_search_range;
0214 };
0215 
0216 
0217 
0218 
0219 //----------------------------------------------------------------------
0220 /// returns true if floor(ln_base2(x)) < floor(ln_base2(y)), using
0221 /// Chan's neat trick...
0222 inline bool floor_ln2_less(unsigned x, unsigned y) {
0223   if (x>y) return false;
0224   return (x < (x^y)); // beware of operator precedence...
0225 }
0226 
0227 
0228 //----------------------------------------------------------------------
0229 /// returns the ID for the specified point...
0230 inline int ClosestPair2D::_ID(const Point * point) const {
0231   return point - &(_points[0]);
0232 }
0233 
0234 
0235 //
0236 inline unsigned int ClosestPair2D::size() {
0237   return _points.size() - _available_points.size();
0238 }
0239 
0240 
0241 
0242 FASTJET_END_NAMESPACE
0243 
0244 #endif // __FASTJET_CLOSESTPAIR2D__HH__