OverSim
NodeVector.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
25 #ifndef __NODE_VECTOR_H
26 #define __NODE_VECTOR_H
27 
28 #include <vector>
29 #include <cassert>
30 
31 #include <Comparator.h>
32 #include <NodeHandle.h>
33 #include <ProxNodeHandle.h>
34 
35 
36 template < class T > class KeyExtractor;
37 template < class T > class ProxExtractor;
38 template < class T > class AddressExtractor;
39 
40 template < class T,
41  class T_key = KeyExtractor<T>,
42  class T_prox = ProxExtractor<T>,
43  class T_address = AddressExtractor<T> > class BaseKeySortedVector;
44 
48 
56 template <class T>
57 struct KeyExtractor {
58  static const OverlayKey& key(const T&)
59  {
61  };
62 };
63 
70 template <>
72  static const OverlayKey& key(const NodeHandle& node)
73  {
74  return node.getKey();
75  };
76 };
77 
78 template <>
80  static const OverlayKey& key(const ProxNodeHandle& node)
81  {
82  return node.getKey();
83  };
84 };
85 
93 template <>
94 struct KeyExtractor<std::pair<NodeHandle, simtime_t> > {
95  static const OverlayKey& key(const std::pair<NodeHandle, simtime_t>& nodes)
96  {
97  return nodes.first.getKey();
98  };
99 };
100 
101 template <class T>
103  static Prox prox(const T&)
104  {
105  return Prox::PROX_UNKNOWN;
106  };
107 };
108 
109 template <>
111  static Prox prox(const ProxNodeHandle& node)
112  {
113  return node.getProx();
114  };
115 };
116 
117 template <>
119  static Prox prox(const ProxTransportAddress& address)
120  {
121  return address.getProx();
122  };
123 };
124 
125 
126 template <class T>
128  static TransportAddress address(const T&)
129  {
131  };
132 };
133 
134 template <>
136  static TransportAddress address(const NodeHandle& node)
137  {
138  return node;
139  };
140 };
141 
142 template <>
145  {
146  return address;
147  };
148 };
149 
156 //template <class T, class T_key, class T_rtt>
157 //class BaseKeySortedVector : public std::vector<T> {
158 //
159 //private://fields: comparator
160 //
161 // const Comparator<OverlayKey>* comparator; /**< the OverlayKey Comparator for this vector */
162 // uint16_t maxSize; /**< maximum nodes this vector holds */
163 // bool useRtt; /**< sort by rtt after sorting using the comparator */
164 //
165 //public://construction
166 //
167 // /**
168 // * constructor
169 // *
170 // * @param maxSize maximum nodes this vector holds
171 // * @param comparator OverlayKey Comparator for this vector
172 // * @param useRtt sort by rtt after sorting using the comparator
173 // */
174 // BaseKeySortedVector(uint16_t maxSize = 0,
175 // const Comparator<OverlayKey>* comparator = NULL,
176 // bool useRtt = false) :
177 // std::vector<T>(),
178 // comparator(comparator),
179 // maxSize(maxSize),
180 // useRtt(useRtt) { };
181 //
182 // /**
183 // * destructor
184 // */
185 // virtual ~BaseKeySortedVector() {};
186 //
187 // typedef typename std::vector<T>::iterator iterator; /**< iterator for this vector */
188 // typedef typename std::vector<T>::const_iterator const_iterator; /**< read-only iterator for this vector */
189 //
190 // static const T UNSPECIFIED_ELEMENT; /**< unspecified element of type T */
191 //
192 //
193 //public://methods: sorted add support
194 //
195 // /**
196 // * indicates if an object of type T can be added to the NodeVector
197 // *
198 // * @param element the element to add
199 // * @return true if element can be added to the NodeVector, false otherwise
200 // */
201 // bool isAddable( const T& element ) const
202 // {
203 // if (maxSize == 0) {
204 // return false;
205 // }
206 //
207 // return(std::vector<T>::size() != maxSize ||
208 // (comparator && ( comparator->compare( T_key::key(element),
209 // T_key::key(std::vector<T>::back()) ) <= 0 )));
210 // };
211 //
212 // /**
213 // * indicates if NodeVector holds maxSize nodes
214 // *
215 // * @return true if the actual size of NodeVector has reached its maxSize, false otherwise
216 // */
217 // bool isFull() const
218 // {
219 // return(std::vector<T>::size() == maxSize);
220 // };
221 //
222 // /**
223 // * indicates if NodeVector holds at least one node
224 // *
225 // * @return true if NodeVector does not hold any node, false otherwise
226 // */
227 // bool isEmpty() const
228 // {
229 // return(std::vector<T>::size() == 0);
230 // };
231 //
232 // /**
233 // * adds an element of type T in increasing order to the NodeVector and
234 // * returns the position of the added element or -1 if the element was not added
235 // *
236 // * @param element the element to add
237 // * @return position of the added element, -1 if the element was not added
238 // */
239 // int add( const T& element )
240 // {
241 // int pos = -1;
242 //
243 // // check if handle is addable
244 // if (isAddable(element)) { // yes ->
245 //
246 // // add handle to the appropriate position
247 // if ((std::vector<T>::size() != 0) && comparator) {
248 // iterator i;
249 // for (i = std::vector<T>::begin(), pos=0;
250 // i != std::vector<T>::end(); i++, pos++) {
251 //
252 // // don't add node with same key twice
253 // if (T_key::key(element) == T_key::key(*i)) {
254 // return -1;
255 // }
256 //
257 // int compResult = comparator->compare(T_key::key(element),
258 // T_key::key(*i));
259 // if ((compResult < 0) ||
260 // (useRtt && (compResult == 0) &&
261 // T_rtt::rtt(element) < T_rtt::rtt(*i))) {
262 //
263 // std::vector<T>::insert(i, element);
264 // break;
265 //
266 // // if (useRtt &&
267 // // (compResult == 0) &&
268 // // T_rtt::rtt(element) < T_rtt::rtt(*i))
269 // // std::cout << "!!!" << std::endl;
270 // }
271 // }
272 // if (i == std::vector<T>::end()) {
273 // pos = std::vector<T>::size();
274 // push_back(element);
275 // }
276 // } else {
277 // for (iterator i = std::vector<T>::begin(); i != std::vector<T>::end();
278 // i++) {
279 // // don't add node with same key twice
280 // if (T_key::key(element) == T_key::key(*i)) {
281 // return -1;
282 // }
283 // }
284 // pos = std::vector<T>::size();
285 // push_back(element);
286 // }
287 //
288 // // adjust size
289 // if ((maxSize != 0) && (std::vector<T>::size() > maxSize)) {
290 // std::vector<T>::resize(maxSize);
291 // }
292 // }
293 //
294 // return pos;
295 // };
296 //
297 // /**
298 // * Searches for an OverlayKey in NodeVector and returns true, if
299 // * it is found.
300 // *
301 // * @param key the OverlayKey to find
302 // * @return true, if the vector contains the key
303 // */
304 // const bool contains(const OverlayKey& key) const {
305 // for (const_iterator i = std::vector<T>::begin();
306 // i != std::vector<T>::end(); i++) {
307 // if (T_key::key(*i) == key) return true;
308 // }
309 //
310 // return false;
311 // }
312 //
313 // /**
314 // * searches for an OverlayKey in NodeVector
315 // *
316 // * @param key the OverlayKey to find
317 // * @return the UNSPECIFIED_ELEMENT if there is no element with the defined key,
318 // * the found element of type T otherwise
319 // */
320 // const T& find(const OverlayKey& key) const
321 // {
322 // for (const_iterator i = std::vector<T>::begin();
323 // i != std::vector<T>::end(); i++) {
324 // if (T_key::key(*i) == key) return *i;
325 // }
326 // return UNSPECIFIED_ELEMENT;
327 // };
328 //
329 // /**
330 // * Searches for an OberlayKey in a NodeVector and returns an
331 // * appropriate iterator.
332 // *
333 // * @param key The key to search
334 // * @return iterator The iterator
335 // */
336 // iterator findIterator(const OverlayKey& key)
337 // {
338 // iterator i;
339 // for (i = std::vector<T>::begin(); i != std::vector<T>::end(); i++)
340 // if (T_key::key(*i) == key) break;
341 // return i;
342 // }
343 //
344 // /**
345 // * Downsize the vector to a maximum of maxElements.
346 // *
347 // * @param maxElements The maximum number of elements after downsizing
348 // */
349 // void downsizeTo(const uint32_t maxElements)
350 // {
351 // if (std::vector<T>::size() > maxElements) {
352 // std::vector<T>::erase(std::vector<T>::begin()+maxElements, std::vector<T>::end());
353 // }
354 // }
355 //
356 // void setComparator(const Comparator<OverlayKey>* comparator)
357 // {
358 // this->comparator = comparator;
359 // }
360 //};
361 
362 template <class T, class T_key, class T_prox, class T_address>
363 class BaseKeySortedVector : public std::vector<T> {
364 
365 private://fields: comparator
369  uint16_t maxSize;
370  uint16_t sizeProx;
371  uint16_t sizeComb;
372 
373 public://construction
385  const Comparator<OverlayKey>* comparator = NULL,
388  uint16_t sizeProx = 0,
389  uint16_t sizeComb = 0) :
390  std::vector<T>(),
394  maxSize(maxSize),
396  sizeComb(sizeComb) { };
397 
401  virtual ~BaseKeySortedVector() {};
402 
403  typedef typename std::vector<T>::iterator iterator;
404  typedef typename std::vector<T>::const_iterator const_iterator;
406  static const T UNSPECIFIED_ELEMENT;
409 public://methods: sorted add support
410 
417  bool isAddable( const T& element ) const
418  {
419  if (maxSize == 0) {
420  return true;
421  }
422 
423  return (std::vector<T>::size() != maxSize ||
424  (comparator &&
425  (comparator->compare(T_key::key(element),
426  T_key::key(std::vector<T>::back())) <= 0 )) ||
427  (proxComparator &&
428  (proxComparator->compare(T_prox::prox(element),
429  T_prox::prox(std::vector<T>::back())) <= 0 )) ||
431  (proxKeyComparator->compare(ProxKey(T_prox::prox(element),
432  T_key::key(element)),
433  ProxKey(T_prox::prox(std::vector<T>::back()),
434  T_key::key(std::vector<T>::back()))) <= 0 )));
435  };
436 
442  bool isFull() const
443  {
444  return(std::vector<T>::size() == maxSize);
445  };
446 
452  bool isEmpty() const
453  {
454  return(std::vector<T>::size() == 0);
455  };
456 
464  int add( const T& element )
465  {
466  int pos = -1;
467 
468  // check if handle is addable
469  if (isAddable(element)) { // yes ->
470 
471  // add handle to the appropriate position
472  if ((std::vector<T>::size() != 0) &&
474  iterator i;
475  for (i = std::vector<T>::begin(), pos=0;
476  i != std::vector<T>::end(); i++, pos++) {
477 
478  // don't add node with same key twice
479  if (!T_key::key(element).isUnspecified()) {
480  if (T_key::key(element) == T_key::key(*i)) {
481  return -1;
482  }
483  } else {
484  if (T_address::address(element) == T_address::address(*i)) {
485  return -1;
486  }
487  }
488 
489  if (pos < sizeProx) {
490  assert(proxComparator);
491  // only compare proximity
492  int compResult =
493  proxComparator->compare(T_prox::prox(element),
494  T_prox::prox(*i));
495  //if (T_prox::prox(element).compareTo(T_prox::prox(*i))) {
496  if (compResult < 0) {
497  iterator temp_it = std::vector<T>::insert(i, element);
498  int tempPos = pos;
499 
500  //std::cout << std::vector<T>::size() << std::endl;
501  while ((tempPos < sizeProx) && (temp_it != std::vector<T>::end())) {
502  ++temp_it;
503  ++tempPos;
504  //std::cout << pos << " " << sizeProx << std::endl;
505  }
506 
507  //std::cout << pos << " " << sizeProx
508  // << " " << maxSize << std::endl;
509 
510  if ((tempPos >= sizeProx) && (tempPos < maxSize) &&
511  (temp_it != std::vector<T>::end())) {
512  T temp = *temp_it;
513  std::vector<T>::erase(temp_it);
514 
515  //re-insert replaced entry into other 2 ranges
516  add(temp);
517  }
518  break;
519  }
520  } else if (pos < sizeProx + sizeComb) {
521  assert(proxKeyComparator);
522  // compare proximity and key distance
523  int compResult = proxKeyComparator->compare(ProxKey(T_prox::prox(element), T_key::key(element)),
524  ProxKey(T_prox::prox(*i), T_key::key(*i)));
525  if (compResult < 0) {
526  iterator temp_it = std::vector<T>::insert(i, element);
527  int tempPos = pos;
528 
529  while ((tempPos < sizeProx + sizeComb) &&
530  (temp_it != std::vector<T>::end())) {
531  ++temp_it;
532  ++tempPos;
533  }
534 
535  if ((tempPos >= sizeProx + sizeComb) && (pos < maxSize) &&
536  (temp_it != std::vector<T>::end())) {
537  T temp = *temp_it;
538  std::vector<T>::erase(temp_it);
539 
540  //re-insert replaced entry into other 2 ranges
541  add(temp);
542  }
543  break;
544  }
545  } else {
546  assert(comparator);
547  // only consider key distance
548  int compResult = comparator->compare(T_key::key(element),
549  T_key::key(*i));
550  if (compResult < 0) {
551  std::vector<T>::insert(i, element);
552  break;
553  }
554  }
555  }
556  if (i == std::vector<T>::end()) {
557  pos = std::vector<T>::size();
558  this->push_back(element);
559  }
560  } else {
561  for (iterator i = std::vector<T>::begin(); i != std::vector<T>::end();
562  i++) {
563  std::cout << "should not happen" << std::endl;
564  // don't add node with same key twice
565  if (T_key::key(element) == T_key::key(*i)) {
566  return -1;
567  }
568  }
569  pos = std::vector<T>::size();
570  assert(pos == 0);
571  this->push_back(element);
572  }
573 
574  // adjust size
575  if ((maxSize != 0) && (std::vector<T>::size() > maxSize)) {
576  std::vector<T>::resize(maxSize);
577  }
578  }
579  return pos;
580  };
581 
589  const bool contains(const OverlayKey& key) const {
590  for (const_iterator i = std::vector<T>::begin();
591  i != std::vector<T>::end(); i++) {
592  if (T_key::key(*i) == key) return true;
593  }
594 
595  return false;
596  }
597 
605  const T& find(const OverlayKey& key) const
606  {
607  for (const_iterator i = std::vector<T>::begin();
608  i != std::vector<T>::end(); i++) {
609  if (T_key::key(*i) == key) return *i;
610  }
611  return UNSPECIFIED_ELEMENT;
612  };
613 
622  {
623  iterator i;
624  for (i = std::vector<T>::begin(); i != std::vector<T>::end(); i++)
625  if (T_key::key(*i) == key) break;
626  return i;
627  }
628 
634  void downsizeTo(const uint32_t maxElements)
635  {
636  if (std::vector<T>::size() > maxElements) {
637  std::vector<T>::erase(std::vector<T>::begin()+maxElements, std::vector<T>::end());
638  }
639  }
640 
642  {
643  this->comparator = comparator;
644  }
645 };
646 template <class T, class T_key, class T_rtt, class T_address>
649 #endif