26 #include "boost/shared_ptr.hpp"
39 Bin(
const vector<pair<pair<double,double>, T> >& objects,
double binSizeX,
double binSizeY);
40 Bin(
const vector<pair<pair<double,double>, boost::shared_ptr<T> > >& objects,
double binSizeX,
double binSizeY);
53 void update(
const T& t, pair<double,double> coordinates);
54 void erase(
const T& t, pair<double,double> coordinates);
55 void rebin(
const double& binSizeX,
const double& binSizeY);
57 size_t count(pair<int, int> coordinates)
const
59 return _data.count(coordinates);
62 void getBinContents(
const pair<int, int>& coordinates, vector<T>& result)
const;
63 void getBinContents(
const pair<double,double>& coordinates, vector<boost::shared_ptr<T> >& result)
const;
68 const vector<pair<pair<double,double>, boost::shared_ptr<T> > >&
getObjects()
const {
return _objects;}
69 const multimap<const pair<int, int>,boost::shared_ptr<T> >&
getData()
const {
return _data;}
78 vector<pair<pair<double,double>, boost::shared_ptr<T> > >
_objects;
80 multimap<const pair<int,int>, boost::shared_ptr<T> >
_data;
90 Bin<T>::Bin(
const vector<pair<pair<double,double>, T> >& objects,
double binSizeX,
double binSizeY)
91 : _binSizeX(binSizeX), _binSizeY(binSizeY)
93 typename vector<pair<pair<double,double>, T> >::const_iterator it = objects.begin();
94 for(; it!= objects.end(); ++it)
96 int binXCoord = int(floor(it->first.first/
_binSizeX));
97 int binYCoord = int(floor(it->first.second/
_binSizeY));
99 boost::shared_ptr<T> t(
new T(it->second));
100 boost::shared_ptr<T> sp_t(
new T(it->second));
102 _data.insert(pair<pair<int,int>, boost::shared_ptr<T> >(pair<int,int>(binXCoord,binYCoord), t));
104 _objects.push_back(make_pair(it->first, t));
110 template <
typename T>
111 Bin<T>::Bin(
const vector<pair<pair<double,double>, boost::shared_ptr<T> > >& objects,
double binSizeX,
double binSizeY) : _binSizeX(binSizeX), _binSizeY(binSizeY)
113 typename vector<pair<pair<double,double>, boost::shared_ptr<T> > >::const_iterator it = objects.begin();
114 for(; it!= objects.end(); ++it)
116 int binXCoord = int(floor(it->first.first/
_binSizeX));
117 int binYCoord = int(floor(it->first.second/
_binSizeY));
119 _data.insert(pair<pair<int,int>, boost::shared_ptr<T> >(pair<int,int>(binXCoord,binYCoord), it->second));
127 template <
typename T>
130 int binXCoord = int(floor(coordinates.first/_binSizeX));
131 int binYCoord = int(floor(coordinates.second/_binSizeY));
133 boost::shared_ptr<T> tp_sp(
new T(t));
134 const pair<const pair<int,int>, boost::shared_ptr<T> >& entry = make_pair(make_pair(binXCoord,binYCoord), tp_sp);
136 _objects.push_back(entry);
137 _allContents.push_back(tp_sp);
141 template <
typename T>
146 bool operator()(pair<
const pair<double,double>,boost::shared_ptr<T> > entry) {
return (*(entry.second) ==
_t); }
150 template <
typename T>
159 template <
typename T>
162 int binXCoord = int(floor(coordinates.first/_binSizeX));
163 int binYCoord = int(floor(coordinates.second/_binSizeY));
165 pair<int,int> intCoordinates = make_pair(binXCoord, binYCoord);
166 pair<typename multimap<const pair<int,int>, boost::shared_ptr<T> >::iterator,
typename multimap<const pair<int,int>,boost::shared_ptr<T> >::iterator> its = _data.equal_range(intCoordinates);
168 typename multimap<const pair<int,int>,boost::shared_ptr<T> >::iterator search_it = find_if(its.first, its.second,
SecondIs<T>(t));
169 if (search_it != its.second)
171 _data.erase(search_it);
174 typename vector<pair<pair<double,double>, boost::shared_ptr<T> > >::iterator objects_eraser = find_if(_objects.begin(), _objects.end(),
SecondIs<T>(t));
175 _objects.erase(objects_eraser);
178 typename vector<boost::shared_ptr<T> >::iterator allContents_erase = find_if(_allContents.begin(), _allContents.end(),
IsObject<T>(t));
179 _allContents.erase(allContents_erase);
183 else cerr <<
"[Bin<T>::erase] Object to erase was not found." << endl;
187 template <
typename T>
190 Bin<T> rebinned(_objects, binSizeX, binSizeY);
194 _binSizeX = binSizeX;
195 _binSizeY = binSizeY;
201 template <
typename T>
203 vector<T>& result)
const
205 pair<typename multimap<const pair<int,int>,boost::shared_ptr<T> >::const_iterator,
typename multimap<const pair<int,int>,boost::shared_ptr<T> >::const_iterator> its = _data.equal_range(coordinates);
207 typename multimap<const pair<int, int>, boost::shared_ptr<T> >::const_iterator it = its.first;
208 for(; it != its.second; ++it)
210 result.push_back(*(it->second));
218 template <
typename T>
220 vector<boost::shared_ptr<T> >& result)
const
222 int binXCoord = int(floor(coordinates.first/_binSizeX));
223 int binYCoord = int(floor(coordinates.second/_binSizeY));
224 pair<int,int> intCoordinates = make_pair(binXCoord, binYCoord);
226 pair<typename multimap<const pair<int,int>,boost::shared_ptr<T> >::const_iterator,
typename multimap<const pair<int,int>,boost::shared_ptr<T> >::const_iterator> its = _data.equal_range(intCoordinates);
228 typename multimap<const pair<int, int>, boost::shared_ptr<T> >::const_iterator it = its.first;
229 for(; it != its.second; ++it)
231 result.push_back((it->second));
239 template <
typename T>
248 pair<double,double> dasher = make_pair(coordinates.first - _binSizeX, coordinates.second + _binSizeY);
249 pair<double,double> dancer = make_pair(coordinates.first + _binSizeX, coordinates.second);
250 pair<double,double> prancer = make_pair(coordinates.first + _binSizeX, coordinates.second + _binSizeY);
251 pair<double,double> vixen = make_pair(coordinates.first, coordinates.second + _binSizeY);
252 pair<double,double> comet = make_pair(coordinates.first + _binSizeX, coordinates.second - _binSizeY);
253 pair<double,double> cupid = make_pair(coordinates.first, coordinates.second - _binSizeY);
254 pair<double,double> donner = make_pair(coordinates.first - _binSizeX, coordinates.second - _binSizeY);
255 pair<double,double> blitzen = make_pair(coordinates.first - _binSizeX, coordinates.second);
257 vector<pair<double,double> > sleigh;
258 sleigh.push_back(dasher);
259 sleigh.push_back(dancer);
260 sleigh.push_back(prancer);
261 sleigh.push_back(vixen);
262 sleigh.push_back(comet);
263 sleigh.push_back(cupid);
264 sleigh.push_back(donner);
265 sleigh.push_back(blitzen);
267 vector<boost::shared_ptr<T> > santa;
268 getBinContents(coordinates, santa);
269 copy(santa.begin(), santa.end(), back_inserter(result));
271 vector<pair<double,double> >::iterator it = sleigh.begin();
272 for(; it!= sleigh.end(); ++it)
274 vector<boost::shared_ptr<T> > rudolph;
275 getBinContents(*it, rudolph);
276 copy(rudolph.begin(), rudolph.end(), back_inserter(result));
283 template <
typename T>
288 _data == that.
_data &&
294 template <
typename T>
297 return !(*
this == that);