Fork me on GitHub

source: svn/trunk/Utilities/Fastjet/include/fastjet/internal/ClosestPair2D.hh@ 972

Last change on this file since 972 was 11, checked in by severine ovyn, 16 years ago

Fastjet added; CDFCones directory has been changed

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