SISCone  2.0.5
hash.cpp
1 
2 // File: hash.cpp //
3 // Description: source file for classes sph_hash_element and sph_hash_cones //
4 // This file is part of the SISCone project. //
5 // WARNING: this is not the main SISCone trunk but //
6 // an adaptation to spherical coordinates //
7 // For more details, see http://projects.hepforge.org/siscone //
8 // //
9 // Copyright (c) 2006-2008 Gavin Salam and Gregory Soyez //
10 // //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
15 // //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details. //
20 // //
21 // You should have received a copy of the GNU General Public License //
22 // along with this program; if not, write to the Free Software //
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA //
24 // //
25 // $Revision:: 294 $//
26 // $Date:: 2009-05-01 17:15:04 +0200 (Fri, 01 May 2009) $//
28 
29 #include <math.h>
30 #include <stdio.h>
31 #include "hash.h"
32 #include <iostream>
33 
34 namespace siscone_spherical{
35 
36 using namespace std;
37 
38 /**************************************************************
39  * implementation of sph_hash_cones *
40  * list of cones candidates. *
41  * We store in this class all the sph_hash_element and give *
42  * functions to manipulate them. *
43  **************************************************************/
44 
45 // constructor with initialisation
46 // - _Np number of particles
47 // - _radius cone radius
48 //-----------------------------------
49 sph_hash_cones::sph_hash_cones(int _Np, double _radius){
50  int i;
51 
52  n_cones = 0;
53 #ifdef DEBUG_STABLE_CONES
54  n_occupied_cells = 0;
55 #endif
56 
57  // determine hash size
58  // for a ymax=5 and R=0.7, we observed an occupancy around 1/8 N^2 ~ N2 R2/4
59  //mask = 1 << (int) (2*log(double(_Np))/log(2.0));
60  //if (mask<=1) mask=2;
61  int nbits = (int) (log(_Np*_radius*_radius*_Np/4.0)/log(2.0));
62  if (nbits<1) nbits=1;
63  mask = 1 << nbits;
64 
65  // create hash
66  hash_array = new sph_hash_element*[mask];
67  mask--;
68 
69  // set the array to 0
70  //? needed ?
71  for (i=0;i<mask+1;i++)
72  hash_array[i] = NULL;
73 
74  tan2R = tan(_radius);
75  tan2R *= tan2R;
76 }
77 
78 // destructor
79 //------------
81  int i;
82  sph_hash_element *elm;
83 
84  for (i=0;i<mask+1;i++){
85  while (hash_array[i]!=NULL){
86  elm = hash_array[i];
87  hash_array[i] = hash_array[i]->next;
88  delete elm;
89  }
90  }
91 
92  delete[] hash_array;
93 }
94 
95 
96 /*
97  * insert a new candidate into the hash.
98  * - v 4-momentum of the cone to add
99  * - parent parent particle defining the cone
100  * - child child particle defining the cone
101  * - p_io whether the parent has to belong to the cone or not
102  * - c_io whether the child has to belong to the cone or not
103  * return 0 on success, 1 on error
104  ***********************************************************************/
105 int sph_hash_cones::insert(CSphmomentum *v, CSphmomentum *parent, CSphmomentum *child, bool p_io, bool c_io){
106  sph_hash_element *elm;
107  int index = (v->ref.ref[0]) & mask;
108 
109  // check the array cell corresponding to our reference
110  elm = hash_array[index];
111 
112 #ifdef DEBUG_STABLE_CONES
113  if (elm==NULL)
114  n_occupied_cells++;
115 #endif
116 
117  do{
118  // if it is not present, add it
119  if (elm==NULL){
120  // create element
121  elm = new sph_hash_element;
122 
123  // set its varibles
124  // Note: at this level, eta and phi have already been computed
125  // through CSphmomentum::build_thetaphi.
126  elm->centre = *v;
127 
128  // if at least one of the two is_closer tests gives a result != from the expected,
129  // the || will be true hence !(...) false as wanted
130  elm->is_stable = !((is_closer(v, parent, tan2R)^p_io)||(is_closer(v, child, tan2R)^c_io));
131  //cout << "-- new status of " << v->ref[0] << ":" << elm->is_stable << endl;
132 
133  // update hash
134  elm->next = hash_array[index];
135  hash_array[index] = elm;
136 
137  n_cones++;
138  return 0;
139  }
140 
141  // if the cone is already there, simply update stability status
142  if (v->ref == elm->centre.ref){
143  // there is only an update to perform to see if the cone is still stable
144  if (elm->is_stable){
145  elm->is_stable = !((is_closer(v, parent, tan2R)^p_io)||(is_closer(v, child, tan2R)^c_io));
146  //cout << " parent/child: "
147  // << parent->ref[0] << ":" << is_closer(v, parent) << ":" << p_io << " "
148  // << child->ref[0] << ":" << is_closer(v, child) << ":" << c_io << endl;
149  //cout << "-- rep status of " << v->ref[0] << ":" << elm->is_stable << endl;
150  //cout << v->eta << " " << v->phi << endl;
151  //cout << (child->eta) << " " << child->phi << endl;
152  }
153  return 0;
154  }
155 
156  elm = elm->next;
157  } while (1);
158 
159  return 1;
160 }
161 
162 /*
163  * insert a new candidate into the hash.
164  * - v 4-momentum of te cone to add
165  * Note, in this case, we assume stability. We also assume
166  * that eta and phi are computed for v
167  * return 0 on success, 1 on error
168  ***********************************************************************/
170  sph_hash_element *elm;
171  int index = (v->ref.ref[0]) & mask;
172  //cout << "-- stable candidate: " << v->ref[0] << ":" << endl;
173 
174  // check the array cell corresponding to our reference
175  elm = hash_array[index];
176  do{
177  // if it is not present, add it
178  if (elm==NULL){
179  // create element
180  elm = new sph_hash_element;
181 
182  // set its varibles
183  // Note: at this level, eta and phi have already been computed
184  // through CSphmomentum::build_thetaphi.
185  elm->centre = *v;
186  elm->is_stable = true;
187 
188  // update hash
189  elm->next = hash_array[index];
190  hash_array[index] = elm;
191 
192  n_cones++;
193  return 0;
194  }
195 
196  // if the cone is already there, we have nothing to do
197  if (v->ref == elm->centre.ref){
198  return 0;
199  }
200 
201  elm = elm->next;
202  } while (1);
203 
204  return 1;
205 }
206 
207 }
The SISCone project has been developed by Gavin Salam and Gregory Soyez
Documentation generated on Wed Mar 19 2014 21:30:25 for SISCone by  Doxygen 1.8.1.2