DynamicNearestNeighbours.hh

00001 //STARTHEADER
00002 // $Id: DynamicNearestNeighbours.hh 1761 2010-09-16 10:43:18Z soyez $
00003 //
00004 // Copyright (c) 2005-2006, Matteo Cacciari and Gavin Salam
00005 //
00006 //----------------------------------------------------------------------
00007 // This file is part of FastJet.
00008 //
00009 //  FastJet is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU General Public License as published by
00011 //  the Free Software Foundation; either version 2 of the License, or
00012 //  (at your option) any later version.
00013 //
00014 //  The algorithms that underlie FastJet have required considerable
00015 //  development and are described in hep-ph/0512210. If you use
00016 //  FastJet as part of work towards a scientific publication, please
00017 //  include a citation to the FastJet paper.
00018 //
00019 //  FastJet is distributed in the hope that it will be useful,
00020 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 //  GNU General Public License for more details.
00023 //
00024 //  You should have received a copy of the GNU General Public License
00025 //  along with FastJet; if not, write to the Free Software
00026 //  Foundation, Inc.:
00027 //      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //----------------------------------------------------------------------
00029 //ENDHEADER
00030 
00031 
00032 #ifndef __FASTJET_DYNAMICNEARESTNEIGHBOURS_HH__
00033 #define __FASTJET_DYNAMICNEARESTNEIGHBOURS_HH__
00034 
00035 #include<vector>
00036 #include<string>
00037 #include<iostream>
00038 #include<sstream>
00039 #include<cassert>
00040 #include "fastjet/internal/numconsts.hh"
00041 
00042 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00043 
00045 //typedef std::pair<double,double> EtaPhi;
00046 
00053 class EtaPhi {
00054 public:
00055   double first, second;
00056   EtaPhi() {}
00057   EtaPhi(double a, double b) {first = a; second = b;}
00059   void sanitize() {    
00060     if (second <  0)     second += twopi; 
00061     if (second >= twopi) second -= twopi;
00062   }
00063 
00064 };
00065 
00072 class DnnError {
00073 public:
00074   // constructors
00075   DnnError() {;};
00076   DnnError(const std::string & message) {
00077     _message = message; std::cerr << message << std::endl;};
00078 
00079   std::string message() const {return _message;};
00080 
00081 private:
00082   std::string _message;
00083 };
00084 
00085 
00103 class DynamicNearestNeighbours {
00104 
00105 public:
00107   //virtual DynamicNearestNeighbours() {};
00108    
00111   //virtual DynamicNearestNeighbours(const std::vector<EtaPhi> &, 
00112   //                               const bool & verbose = false ) = 0;
00113 
00116   virtual int NearestNeighbourIndex(const int & ii) const = 0;
00117 
00120   virtual double NearestNeighbourDistance(const int & ii) const = 0;
00121 
00125   virtual bool Valid(const int & index) const = 0;
00126 
00137   virtual void RemoveAndAddPoints(const std::vector<int> & indices_to_remove,
00138                           const std::vector<EtaPhi> & points_to_add,
00139                           std::vector<int> & indices_added,
00140                           std::vector<int> & indices_of_updated_neighbours) = 0;
00141 
00142 
00145   inline void RemovePoint (const int & index,
00146                            std::vector<int> & indices_of_updated_neighbours) {
00147     std::vector<int> indices_added;
00148     std::vector<EtaPhi> points_to_add;
00149     std::vector<int> indices_to_remove(1);
00150     indices_to_remove[0] = index;
00151     RemoveAndAddPoints(indices_to_remove, points_to_add, indices_added,
00152                        indices_of_updated_neighbours
00153                        );};
00154 
00155 
00161   inline void RemoveCombinedAddCombination(
00162                         const int & index1, const int & index2,
00163                         const EtaPhi & newpoint,
00164                         int & index3,
00165                         std::vector<int> & indices_of_updated_neighbours) {
00166     std::vector<int> indices_added(1);
00167     std::vector<EtaPhi> points_to_add(1);
00168     std::vector<int> indices_to_remove(2);
00169     indices_to_remove[0] = index1;
00170     indices_to_remove[1] = index2;
00171     points_to_add[0] = newpoint;
00172     RemoveAndAddPoints(indices_to_remove, points_to_add, indices_added,
00173                        indices_of_updated_neighbours
00174                        );
00175     index3 = indices_added[0];
00176   };
00177 
00179   virtual ~DynamicNearestNeighbours () {}
00180 };
00181   
00182 
00183 FASTJET_END_NAMESPACE
00184 
00185 #endif // __FASTJET_DYNAMICNEARESTNEIGHBOURS_HH__