00001
00023 #ifndef NEIGHBORS_H_
00024 #define NEIGHBORS_H_
00025
00026 #include <iostream>
00027 #include <vector>
00028 #include <map>
00029 #include <algorithm>
00030
00031 #include "micros_swarm/singleton.h"
00032 #include "micros_swarm/runtime_handle.h"
00033
00034 namespace micros_swarm{
00035
00036 template<class Type>
00037 class Neighbors{
00038 public:
00039 Neighbors()
00040 {
00041 data_.clear();
00042 rth_ = Singleton<RuntimeHandle>::getSingleton();
00043 }
00044
00045 Neighbors(const Neighbors<Type>& n)
00046 {
00047 rth_ = Singleton<RuntimeHandle>::getSingleton();
00048 data_ = n.data_;
00049 }
00050
00051 Neighbors& operator=(const Neighbors<Type>& n)
00052 {
00053 if(this == &n) {
00054 return *this;
00055 }
00056 data_.clear();
00057 data_ = n.data_;
00058 return *this;
00059 }
00060
00061 ~Neighbors()
00062 {
00063 rth_.reset();
00064 }
00065
00066 std::map<int, Type>& data()
00067 {
00068 return data_;
00069 }
00070
00071 void print()
00072 {
00073 typename std::map<int, Type>::iterator it;
00074
00075 for(it = data_.begin(); it != data_.end(); it++) {
00076 std::cout<<"---"<<it->first<<","<<it->second<<"---"<<std::endl;
00077 }
00078 }
00079
00080 void foreach(void(*f)(Type))
00081 {
00082 typename std::map<int, Type>::iterator n_it;
00083
00084 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00085 (*f)(n_it->second);
00086 }
00087 }
00088
00089 void foreach(const boost::function<void(Type)>& f)
00090 {
00091 typename std::map<int, Type>::iterator n_it;
00092
00093 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00094 f(n_it->second);
00095 }
00096 }
00097
00098 template<class T2>
00099 Neighbors<T2> map( T2(*f)(Type))
00100 {
00101 Neighbors<T2> n2;
00102 typename std::map<int, Type>::iterator n_it1;
00103
00104 for(n_it1 = data_.begin(); n_it1 != data_.end(); n_it1++) {
00105 T2 temp = (*f)(n_it1->second);
00106 n2.data_.insert(std::pair<int, T2>(n_it1->first,temp));
00107 }
00108 return n2;
00109 }
00110
00111 template<class T2>
00112 Neighbors<T2> map(const boost::function<T2(Type)>& f)
00113 {
00114 Neighbors<T2> n2;
00115 typename std::map<int, Type>::iterator n_it1;
00116
00117 for(n_it1 = data_.begin(); n_it1 != data_.end(); n_it1++) {
00118 T2 temp = f(n_it1->second);
00119 n2.data_.insert(std::pair<int, T2>(n_it1->first,temp));
00120 }
00121 return n2;
00122 }
00123
00124 template<class T2>
00125 T2 reduce(T2(*f)(Type, T2 &), T2& t2)
00126 {
00127 typename std::map<int, Type>::iterator n_it1;
00128
00129 for(n_it1 = data_.begin(); n_it1 != data_.end(); n_it1++) {
00130 t2 = (*f)(n_it1->second, t2);
00131 }
00132
00133 return t2;
00134 }
00135
00136 template<class T2>
00137 T2 reduce(const boost::function<T2(Type, T2&)>& f, T2& t2)
00138 {
00139 typename std::map<int, Type>::iterator n_it1;
00140
00141 for(n_it1 = data_.begin(); n_it1 != data_.end(); n_it1++) {
00142 t2 = f(n_it1->second, t2);
00143 }
00144
00145 return t2;
00146 }
00147
00148 Neighbors<Type> filter(bool(*f)(int, Type))
00149 {
00150 Neighbors<Type> result;
00151
00152 typename std::map<int, Type>::iterator n_it;
00153
00154 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00155 if((*f)(n_it->first, n_it->second)) {
00156 result.data_.insert(std::pair<int, Type>(n_it->first,n_it->second));
00157 }
00158 }
00159 return result;
00160 }
00161
00162 Neighbors<Type> filter(const boost::function<bool(int, Type)>& f)
00163 {
00164 Neighbors<Type> result;
00165
00166 typename std::map<int, Type>::iterator n_it;
00167
00168 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00169 if(f(n_it->first, n_it->second)) {
00170 result.data_.insert(std::pair<int, Type>(n_it->first,n_it->second));
00171 }
00172 }
00173 return result;
00174 }
00175
00176 Neighbors<Type> kin(int swarm_id)
00177 {
00178 Neighbors<Type> result;
00179
00180 typename std::map<int, Type>::iterator n_it;
00181
00182 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00183 if(rth_->inNeighborSwarm(n_it->first, swarm_id)) {
00184 result.data_.insert(std::pair<int, Type>(n_it->first,n_it->second));
00185 }
00186 }
00187
00188 return result;
00189 }
00190
00191 Neighbors<Type> nonkin(int swarm_id)
00192 {
00193 Neighbors<Type> result;
00194
00195 typename std::map<int, Type>::iterator n_it;
00196
00197 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00198 if(!rth_->inNeighborSwarm(n_it->first, swarm_id)) {
00199 result.data_.insert(std::pair<int, Type>(n_it->first,n_it->second));
00200 }
00201 }
00202
00203 return result;
00204 }
00205 private:
00206 boost::shared_ptr<RuntimeHandle> rth_;
00207 std::map<int, Type> data_;
00208 };
00209
00210
00211
00212
00213 template<>
00214 class Neighbors<NeighborBase>{
00215 public:
00216 Neighbors()
00217 {
00218 data_.clear();
00219 rth_ = Singleton<RuntimeHandle>::getSingleton();
00220 }
00221
00222 Neighbors(bool get_data_now)
00223 {
00224 if(get_data_now) {
00225 data_.clear();
00226 rth_ = Singleton<RuntimeHandle>::getSingleton();
00227 rth_->getNeighbors(data_);
00228 }
00229 else {
00230 data_.clear();
00231 rth_ = Singleton<RuntimeHandle>::getSingleton();
00232 }
00233 }
00234
00235 Neighbors(const Neighbors<NeighborBase>& n)
00236 {
00237 rth_ = Singleton<RuntimeHandle>::getSingleton();
00238 data_ = n.data_;
00239 }
00240
00241 Neighbors& operator=(const Neighbors<NeighborBase>& n)
00242 {
00243 if(this == &n) {
00244 return *this;
00245 }
00246 data_.clear();
00247 data_ = n.data_;
00248 return *this;
00249 }
00250
00251 ~Neighbors()
00252 {
00253 rth_.reset();
00254 }
00255
00256 std::map<int, NeighborBase>& data()
00257 {
00258 return data_;
00259 }
00260
00261 void print()
00262 {
00263 typename std::map<int, NeighborBase>::iterator it;
00264
00265 for(it = data_.begin(); it != data_.end(); it++) {
00266 std::cout<<"---"<<it->first<<": "<<it->second.distance<<","<< \
00267 it->second.azimuth<<","<<it->second.elevation<<","<< \
00268 it->second.x<<","<<it->second.y<<","<<it->second.z<< \
00269 it->second.vx<<","<<it->second.vy<<","<<it->second.vz<< \
00270 "---"<<std::endl;
00271 }
00272 }
00273
00274 void foreach(void(*f)(NeighborBase))
00275 {
00276 typename std::map<int, NeighborBase>::iterator n_it;
00277
00278 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00279 (*f)(n_it->second);
00280 }
00281 }
00282
00283 void foreach(const boost::function<void(NeighborBase)>& f)
00284 {
00285 typename std::map<int, NeighborBase>::iterator n_it;
00286
00287 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00288 f(n_it->second);
00289 }
00290 }
00291
00292 template<class T>
00293 Neighbors<T> map(T(*f)(NeighborBase))
00294 {
00295 Neighbors<T> n;
00296 typename std::map<int, NeighborBase>::iterator n_it;
00297
00298 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00299 T temp = (*f)(n_it->second);
00300 n.data_.insert(std::pair<int, T>(n_it->first,temp));
00301 }
00302
00303 return n;
00304 }
00305
00306 template<class T>
00307 Neighbors<T> map(const boost::function<T(NeighborBase)>& f)
00308 {
00309 Neighbors<T> n;
00310 typename std::map<int, NeighborBase>::iterator n_it;
00311
00312 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00313 T temp = f(n_it->second);
00314 n.data_.insert(std::pair<int, T>(n_it->first,temp));
00315 }
00316
00317 return n;
00318 }
00319
00320 template<class T>
00321 T reduce(T(*f)(NeighborBase, T &), T& t)
00322 {
00323 typename std::map<int, NeighborBase>::iterator n_it;
00324
00325 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00326 t = (*f)(n_it->second, t);
00327 }
00328
00329 return t;
00330 }
00331
00332 template<class T>
00333 T reduce(const boost::function<T(NeighborBase, T &)>& f, T& t)
00334 {
00335 typename std::map<int, NeighborBase>::iterator n_it;
00336
00337 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00338 t = f(n_it->second, t);
00339 }
00340
00341 return t;
00342 }
00343
00344 Neighbors<NeighborBase> filter(bool(*f)(int, NeighborBase))
00345 {
00346 Neighbors<NeighborBase> result;
00347
00348 typename std::map<int, NeighborBase>::iterator n_it;
00349
00350 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00351 if((*f)(n_it->first, n_it->second)) {
00352 result.data_.insert(std::pair<int, NeighborBase>(n_it->first,n_it->second));
00353 }
00354 }
00355
00356 return result;
00357 }
00358
00359 Neighbors<NeighborBase> filter(const boost::function<bool(int, NeighborBase)>& f)
00360 {
00361 Neighbors<NeighborBase> result;
00362
00363 typename std::map<int, NeighborBase>::iterator n_it;
00364
00365 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00366 if(f(n_it->first, n_it->second)) {
00367 result.data_.insert(std::pair<int, NeighborBase>(n_it->first,n_it->second));
00368 }
00369 }
00370
00371 return result;
00372 }
00373
00374 Neighbors<NeighborBase> kin(int swarm_id)
00375 {
00376 Neighbors<NeighborBase> result;
00377
00378 typename std::map<int, NeighborBase>::iterator n_it;
00379
00380 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00381 if(rth_->inNeighborSwarm(n_it->first, swarm_id)) {
00382 result.data_.insert(std::pair<int, NeighborBase>(n_it->first,n_it->second));
00383 }
00384 }
00385
00386 return result;
00387 }
00388
00389 Neighbors<NeighborBase> nonkin(int swarm_id)
00390 {
00391 Neighbors<NeighborBase> result;
00392
00393 typename std::map<int, NeighborBase>::iterator n_it;
00394
00395 for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
00396 if(!rth_->inNeighborSwarm(n_it->first, swarm_id)) {
00397 result.data_.insert(std::pair<int, NeighborBase>(n_it->first,n_it->second));
00398 }
00399 }
00400
00401 return result;
00402 }
00403 private:
00404 boost::shared_ptr<RuntimeHandle> rth_;
00405 std::map<int, NeighborBase> data_;
00406 };
00407 };
00408 #endif