neighbors.h
Go to the documentation of this file.
1 
23 #ifndef NEIGHBORS_H_
24 #define NEIGHBORS_H_
25 
26 #include <iostream>
27 #include <vector>
28 #include <map>
29 #include <algorithm>
30 
31 #include "micros_swarm/singleton.h"
33 
34 namespace micros_swarm{
35 
36  template<class Type>
37  class Neighbors{
38  public:
40  {
41  data_.clear();
43  }
44 
46  {
48  data_ = n.data_;
49  }
50 
52  {
53  if(this == &n) {
54  return *this;
55  }
56  data_.clear();
57  data_ = n.data_;
58  return *this;
59  }
60 
62  {
63  rth_.reset();
64  }
65 
66  std::map<int, Type>& data()
67  {
68  return data_;
69  }
70 
71  void print()
72  {
73  typename std::map<int, Type>::iterator it;
74 
75  for(it = data_.begin(); it != data_.end(); it++) {
76  std::cout<<"---"<<it->first<<","<<it->second<<"---"<<std::endl;
77  }
78  }
79 
80  void foreach(void(*f)(Type))
81  {
82  typename std::map<int, Type>::iterator n_it;
83 
84  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
85  (*f)(n_it->second);
86  }
87  }
88 
89  void foreach(const boost::function<void(Type)>& f) //for class member functions
90  {
91  typename std::map<int, Type>::iterator n_it;
92 
93  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
94  f(n_it->second);
95  }
96  }
97 
98  template<class T2>
99  Neighbors<T2> map( T2(*f)(Type))
100  {
101  Neighbors<T2> n2;
102  typename std::map<int, Type>::iterator n_it1;
103 
104  for(n_it1 = data_.begin(); n_it1 != data_.end(); n_it1++) {
105  T2 temp = (*f)(n_it1->second);
106  n2.data_.insert(std::pair<int, T2>(n_it1->first,temp));
107  }
108  return n2;
109  }
110 
111  template<class T2>
112  Neighbors<T2> map(const boost::function<T2(Type)>& f) //for class member functions
113  {
114  Neighbors<T2> n2;
115  typename std::map<int, Type>::iterator n_it1;
116 
117  for(n_it1 = data_.begin(); n_it1 != data_.end(); n_it1++) {
118  T2 temp = f(n_it1->second);
119  n2.data_.insert(std::pair<int, T2>(n_it1->first,temp));
120  }
121  return n2;
122  }
123 
124  template<class T2>
125  T2 reduce(T2(*f)(Type, T2 &), T2& t2)
126  {
127  typename std::map<int, Type>::iterator n_it1;
128 
129  for(n_it1 = data_.begin(); n_it1 != data_.end(); n_it1++) {
130  t2 = (*f)(n_it1->second, t2);
131  }
132 
133  return t2;
134  }
135 
136  template<class T2>
137  T2 reduce(const boost::function<T2(Type, T2&)>& f, T2& t2) //for class member functions
138  {
139  typename std::map<int, Type>::iterator n_it1;
140 
141  for(n_it1 = data_.begin(); n_it1 != data_.end(); n_it1++) {
142  t2 = f(n_it1->second, t2);
143  }
144 
145  return t2;
146  }
147 
148  Neighbors<Type> filter(bool(*f)(int, Type))
149  {
150  Neighbors<Type> result;
151 
152  typename std::map<int, Type>::iterator n_it;
153 
154  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
155  if((*f)(n_it->first, n_it->second)) {
156  result.data_.insert(std::pair<int, Type>(n_it->first,n_it->second));
157  }
158  }
159  return result;
160  }
161 
162  Neighbors<Type> filter(const boost::function<bool(int, Type)>& f) //for class member functions
163  {
164  Neighbors<Type> result;
165 
166  typename std::map<int, Type>::iterator n_it;
167 
168  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
169  if(f(n_it->first, n_it->second)) {
170  result.data_.insert(std::pair<int, Type>(n_it->first,n_it->second));
171  }
172  }
173  return result;
174  }
175 
176  Neighbors<Type> kin(int swarm_id)
177  {
178  Neighbors<Type> result;
179 
180  typename std::map<int, Type>::iterator n_it;
181 
182  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
183  if(rth_->inNeighborSwarm(n_it->first, swarm_id)) {
184  result.data_.insert(std::pair<int, Type>(n_it->first,n_it->second));
185  }
186  }
187 
188  return result;
189  }
190 
191  Neighbors<Type> nonkin(int swarm_id)
192  {
193  Neighbors<Type> result;
194 
195  typename std::map<int, Type>::iterator n_it;
196 
197  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
198  if(!rth_->inNeighborSwarm(n_it->first, swarm_id)) {
199  result.data_.insert(std::pair<int, Type>(n_it->first,n_it->second));
200  }
201  }
202 
203  return result;
204  }
205  private:
207  std::map<int, Type> data_;
208  };
209 
210  /*
211  * specialization for NeighborBase type
212  */
213  template<>
215  public:
217  {
218  data_.clear();
220  }
221 
222  Neighbors(bool get_data_now)
223  {
224  if(get_data_now) {
225  data_.clear();
227  rth_->getNeighbors(data_);
228  }
229  else {
230  data_.clear();
232  }
233  }
234 
236  {
238  data_ = n.data_;
239  }
240 
242  {
243  if(this == &n) {
244  return *this;
245  }
246  data_.clear();
247  data_ = n.data_;
248  return *this;
249  }
250 
252  {
253  rth_.reset();
254  }
255 
256  std::map<int, NeighborBase>& data()
257  {
258  return data_;
259  }
260 
261  void print()
262  {
263  typename std::map<int, NeighborBase>::iterator it;
264 
265  for(it = data_.begin(); it != data_.end(); it++) {
266  std::cout<<"---"<<it->first<<": "<<it->second.distance<<","<< \
267  it->second.azimuth<<","<<it->second.elevation<<","<< \
268  it->second.x<<","<<it->second.y<<","<<it->second.z<< \
269  it->second.vx<<","<<it->second.vy<<","<<it->second.vz<< \
270  "---"<<std::endl;
271  }
272  }
273 
274  void foreach(void(*f)(NeighborBase))
275  {
276  typename std::map<int, NeighborBase>::iterator n_it;
277 
278  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
279  (*f)(n_it->second);
280  }
281  }
282 
283  void foreach(const boost::function<void(NeighborBase)>& f) //for class member functions
284  {
285  typename std::map<int, NeighborBase>::iterator n_it;
286 
287  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
288  f(n_it->second);
289  }
290  }
291 
292  template<class T>
294  {
295  Neighbors<T> n;
296  typename std::map<int, NeighborBase>::iterator n_it;
297 
298  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
299  T temp = (*f)(n_it->second);
300  n.data_.insert(std::pair<int, T>(n_it->first,temp));
301  }
302 
303  return n;
304  }
305 
306  template<class T>
307  Neighbors<T> map(const boost::function<T(NeighborBase)>& f) //for class member functions
308  {
309  Neighbors<T> n;
310  typename std::map<int, NeighborBase>::iterator n_it;
311 
312  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
313  T temp = f(n_it->second);
314  n.data_.insert(std::pair<int, T>(n_it->first,temp));
315  }
316 
317  return n;
318  }
319 
320  template<class T>
321  T reduce(T(*f)(NeighborBase, T &), T& t)
322  {
323  typename std::map<int, NeighborBase>::iterator n_it;
324 
325  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
326  t = (*f)(n_it->second, t);
327  }
328 
329  return t;
330  }
331 
332  template<class T>
333  T reduce(const boost::function<T(NeighborBase, T &)>& f, T& t) //for class member functions
334  {
335  typename std::map<int, NeighborBase>::iterator n_it;
336 
337  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
338  t = f(n_it->second, t);
339  }
340 
341  return t;
342  }
343 
345  {
347 
348  typename std::map<int, NeighborBase>::iterator n_it;
349 
350  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
351  if((*f)(n_it->first, n_it->second)) {
352  result.data_.insert(std::pair<int, NeighborBase>(n_it->first,n_it->second));
353  }
354  }
355 
356  return result;
357  }
358 
359  Neighbors<NeighborBase> filter(const boost::function<bool(int, NeighborBase)>& f) //for class member functions
360  {
362 
363  typename std::map<int, NeighborBase>::iterator n_it;
364 
365  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
366  if(f(n_it->first, n_it->second)) {
367  result.data_.insert(std::pair<int, NeighborBase>(n_it->first,n_it->second));
368  }
369  }
370 
371  return result;
372  }
373 
375  {
377 
378  typename std::map<int, NeighborBase>::iterator n_it;
379 
380  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
381  if(rth_->inNeighborSwarm(n_it->first, swarm_id)) {
382  result.data_.insert(std::pair<int, NeighborBase>(n_it->first,n_it->second));
383  }
384  }
385 
386  return result;
387  }
388 
390  {
392 
393  typename std::map<int, NeighborBase>::iterator n_it;
394 
395  for(n_it = data_.begin(); n_it != data_.end(); n_it++) {
396  if(!rth_->inNeighborSwarm(n_it->first, swarm_id)) {
397  result.data_.insert(std::pair<int, NeighborBase>(n_it->first,n_it->second));
398  }
399  }
400 
401  return result;
402  }
403  private:
405  std::map<int, NeighborBase> data_;
406  };
407 };
408 #endif
Neighbors< NeighborBase > filter(bool(*f)(int, NeighborBase))
Definition: neighbors.h:344
Neighbors(const Neighbors< NeighborBase > &n)
Definition: neighbors.h:235
Neighbors< T > map(T(*f)(NeighborBase))
Definition: neighbors.h:293
boost::shared_ptr< RuntimeHandle > rth_
Definition: neighbors.h:206
Neighbors< Type > nonkin(int swarm_id)
Definition: neighbors.h:191
f
std::map< int, NeighborBase > & data()
Definition: neighbors.h:256
Neighbors< Type > filter(const boost::function< bool(int, Type)> &f)
Definition: neighbors.h:162
std::map< int, Type > data_
Definition: neighbors.h:207
Neighbors< NeighborBase > nonkin(int swarm_id)
Definition: neighbors.h:389
Neighbors & operator=(const Neighbors< Type > &n)
Definition: neighbors.h:51
Neighbors< T > map(const boost::function< T(NeighborBase)> &f)
Definition: neighbors.h:307
T reduce(T(*f)(NeighborBase, T &), T &t)
Definition: neighbors.h:321
Neighbors< NeighborBase > filter(const boost::function< bool(int, NeighborBase)> &f)
Definition: neighbors.h:359
std::map< int, Type > & data()
Definition: neighbors.h:66
boost::shared_ptr< RuntimeHandle > rth_
Definition: neighbors.h:404
T2 reduce(const boost::function< T2(Type, T2 &)> &f, T2 &t2)
Definition: neighbors.h:137
static boost::shared_ptr< T > getSingleton()
Definition: singleton.h:70
Neighbors(const Neighbors< Type > &n)
Definition: neighbors.h:45
Neighbors< Type > filter(bool(*f)(int, Type))
Definition: neighbors.h:148
Neighbors & operator=(const Neighbors< NeighborBase > &n)
Definition: neighbors.h:241
Neighbors< Type > kin(int swarm_id)
Definition: neighbors.h:176
Neighbors< T2 > map(const boost::function< T2(Type)> &f)
Definition: neighbors.h:112
std::map< int, NeighborBase > data_
Definition: neighbors.h:405
T reduce(const boost::function< T(NeighborBase, T &)> &f, T &t)
Definition: neighbors.h:333
Neighbors< NeighborBase > kin(int swarm_id)
Definition: neighbors.h:374
T2 reduce(T2(*f)(Type, T2 &), T2 &t2)
Definition: neighbors.h:125
Neighbors< T2 > map(T2(*f)(Type))
Definition: neighbors.h:99


micros_swarm
Author(s):
autogenerated on Mon Jun 10 2019 14:02:06