FastJet 3.4.2
ClosestPair2D.hh
1//FJSTARTHEADER
2// $Id$
3//
4// Copyright (c) 2005-2023, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
5//
6//----------------------------------------------------------------------
7// This file is part of FastJet.
8//
9// FastJet is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 2 of the License, or
12// (at your option) any later version.
13//
14// The algorithms that underlie FastJet have required considerable
15// development. They are described in the original FastJet paper,
16// hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
17// FastJet as part of work towards a scientific publication, please
18// quote the version you use and include a citation to the manual and
19// optionally also to hep-ph/0512210.
20//
21// FastJet is distributed in the hope that it will be useful,
22// but WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24// GNU General Public License for more details.
25//
26// You should have received a copy of the GNU General Public License
27// along with FastJet. If not, see <http://www.gnu.org/licenses/>.
28//----------------------------------------------------------------------
29//FJENDHEADER
30
31#ifndef __FASTJET_CLOSESTPAIR2D__HH__
32#define __FASTJET_CLOSESTPAIR2D__HH__
33
34#include<vector>
35#include<stack>
36#include<iostream>
37#include "fastjet/internal/ClosestPair2DBase.hh"
38#include "fastjet/internal/SearchTree.hh"
39#include "fastjet/internal/MinHeap.hh"
40#include "fastjet/SharedPtr.hh"
41
42FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
43
44//----------------------------------------------------------------------
45/// \if internal_doc
46/// @ingroup internal
47/// \class ClosestPair2D
48/// concrete implementation for finding closest pairs in 2D -- will
49/// use Chan's (hopefully efficient) shuffle based structures
50/// \endif
52public:
53 /// constructor from a vector of 2D positions -- number of objects
54 /// after insertion and deletion must never exceed positions.size();
55 /// objects are given IDs that correspond to their index in the vector
56 /// of positions
57 ClosestPair2D(const std::vector<Coord2D> & positions,
58 const Coord2D & left_corner, const Coord2D & right_corner) {
59 _initialize(positions, left_corner, right_corner, positions.size());
60 };
61
62 /// constructor which allows structure to grow beyond positions.size(), up
63 /// to max_size
64 ClosestPair2D(const std::vector<Coord2D> & positions,
65 const Coord2D & left_corner, const Coord2D & right_corner,
66 const unsigned int max_size) {
67 _initialize(positions, left_corner, right_corner, max_size);
68 };
69
70 /// provides the IDs of the closest pair as well as the distance between
71 /// them
72 void closest_pair(unsigned int & ID1, unsigned int & ID2,
73 double & distance2) const;
74
75 /// removes the entry labelled by ID from the object;
76 void remove(unsigned int ID);
77
78 /// inserts the position into the closest pair structure and returns the
79 /// ID that has been allocated for the object.
80 unsigned int insert(const Coord2D &);
81
82 /// removes ID1 and ID2 and inserts position, returning the ID
83 /// corresponding to position...
84 virtual unsigned int replace(unsigned int ID1, unsigned int ID2,
85 const Coord2D & position);
86
87 /// replaces IDs_to_remove with points at the new_positions
88 /// indicating the IDs allocated to the new points in new_IDs
89 virtual void replace_many(const std::vector<unsigned int> & IDs_to_remove,
90 const std::vector<Coord2D> & new_positions,
91 std::vector<unsigned int> & new_IDs);
92
93 // mostly for checking how things are working...
94 inline void print_tree_depths(std::ostream & outdev) const {
95 outdev << _trees[0]->max_depth() << " "
96 << _trees[1]->max_depth() << " "
97 << _trees[2]->max_depth() << "\n";
98 };
99
100 unsigned int size();
101
102private:
103
104 void _initialize(const std::vector<Coord2D> & positions,
105 const Coord2D & left_corner, const Coord2D & right_corner,
106 const unsigned int max_size);
107
108 static const unsigned int _nshift = 3;
109
110
111 /// since sets of three objects will crop up repeatedly, useful
112 /// to have a triplet class?
113 template<class T> class triplet {
114 public:
115 inline const T & operator[](unsigned int i) const {return _contents[i];};
116 inline T & operator[](unsigned int i) {return _contents[i];};
117 private:
118 T _contents[_nshift];
119 };
120
121 // forward declaration
122 class Point;
123
124 /// class that will take care of ordering of shuffles for us
125 class Shuffle {
126 public:
127 unsigned int x, y;
128 Point * point;
129 bool operator<(const Shuffle &) const;
130 void operator+=(unsigned int shift) {x += shift; y+= shift;};
131 };
132
133 typedef SearchTree<Shuffle> Tree;
134 typedef Tree::circulator circulator;
135 typedef Tree::const_circulator const_circulator;
136
137 //----------------------------------------------------------------------
138 /// \if internal_doc
139 /// @ingroup internal
140 /// \class ClosestPair2D::Point
141 /// class for representing all info needed about a point
142 /// \endif
143 class Point {
144 public:
145 /// the point's coordinates
146 Coord2D coord;
147 /// a pointer to its closest neighbour in our structure
148 Point * neighbour;
149 /// the corresponding squared distance
150 double neighbour_dist2;
151 /// circulators for each of the shifts of the shuffles
152 triplet<circulator> circ;
153
154 /// indicates that something special is currently happening to this point
155 unsigned int review_flag;
156
157 /// returns the distance between two of these objects
158 double distance2(const Point & other) const {
159 return coord.distance2(other.coord);
160 };
161
162 /// creates a shuffle for us with a given shift
163 //void set_shuffle(Shuffle & shuffle);
164 };
165
166 triplet<SharedPtr<Tree> > _trees;
167 SharedPtr<MinHeap> _heap;
168 std::vector<Point> _points;
169 std::stack<Point *> _available_points;
170
171 /// points that are "under review" in some way
172 std::vector<Point *> _points_under_review;
173
174 // different statuses for review
175 static const unsigned int _remove_heap_entry = 1;
176 static const unsigned int _review_heap_entry = 2;
177 static const unsigned int _review_neighbour = 4;
178
179 /// add a label to a point as to the nature of review needed
180 /// (includes adding it to list of points needing review) [doesn't
181 /// affect other labels already set for the point]
182 void _add_label(Point * point, unsigned int review_flag);
183
184 /// sets the label for the point to be exclusively this
185 /// review flag (and adds it to list of points needing review
186 /// if not already there)
187 void _set_label(Point * point, unsigned int review_flag);
188
189 /// for all entries of the _points_under_review[] vector, carry out
190 /// the actions indicated by its review flag; the points are
191 /// then removed from _points_under_review[] and their flags
192 /// set to zero
193 void _deal_with_points_to_review();
194
195 /// carry out the search-tree related operations of point removal
196 void _remove_from_search_tree(Point * point_to_remove);
197
198 /// carry out the search-tree related operations of point insertion
199 void _insert_into_search_tree(Point * new_point);
200
201 /// takes a point and creates a shuffle with the given shift
202 void _point2shuffle(Point & , Shuffle & , unsigned int shift);
203
204 /// pieces needed for converting coordinates to integer
205 Coord2D _left_corner;
206 double _range;
207
208 int _ID(const Point *) const;
209
210 triplet<unsigned int> _shifts; // absolute shifts
211 triplet<unsigned int> _rel_shifts; // shifts relative to previous shift
212
213 unsigned int _cp_search_range;
214};
215
216
217
218
219//----------------------------------------------------------------------
220/// returns true if floor(ln_base2(x)) < floor(ln_base2(y)), using
221/// Chan's neat trick...
222inline bool floor_ln2_less(unsigned x, unsigned y) {
223 if (x>y) return false;
224 return (x < (x^y)); // beware of operator precedence...
225}
226
227
228//----------------------------------------------------------------------
229/// returns the ID for the specified point...
230inline int ClosestPair2D::_ID(const Point * point) const {
231 return point - &(_points[0]);
232}
233
234
235//
236inline unsigned int ClosestPair2D::size() {
237 return _points.size() - _available_points.size();
238}
239
240
241
242FASTJET_END_NAMESPACE
243
244#endif // __FASTJET_CLOSESTPAIR2D__HH__
ClosestPair2D(const std::vector< Coord2D > &positions, const Coord2D &left_corner, const Coord2D &right_corner)
constructor from a vector of 2D positions – number of objects after insertion and deletion must never...
ClosestPair2D(const std::vector< Coord2D > &positions, const Coord2D &left_corner, const Coord2D &right_corner, const unsigned int max_size)
constructor which allows structure to grow beyond positions.size(), up to max_size
bool operator<(SharedPtr< T > const &t, SharedPtr< U > const &u)
comparison: ordering
Definition: SharedPtr.hh:613
bool floor_ln2_less(unsigned x, unsigned y)
returns true if floor(ln_base2(x)) < floor(ln_base2(y)), using Chan's neat trick.....