NodeVector.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00025 #ifndef __NODE_VECTOR_H
00026 #define __NODE_VECTOR_H
00027
00028 #include <vector>
00029 #include <cassert>
00030
00031 #include <Comparator.h>
00032 #include <NodeHandle.h>
00033 #include <ProxNodeHandle.h>
00034
00035
00036 template <class T> class KeyExtractor;
00037 template <class T> class ProxExtractor;
00038
00039 template <class T,
00040 class T_key = KeyExtractor<T>,
00041 class T_prox = ProxExtractor<T> > class BaseKeySortedVector;
00042
00043 typedef BaseKeySortedVector< NodeHandle > NodeVector;
00044 typedef BaseKeySortedVector< ProxNodeHandle > ProxNodeVector;
00045
00053 template <class T>
00054 struct KeyExtractor {
00055 static const OverlayKey& key(const T&)
00056 {
00057 return OverlayKey::UNSPECIFIED_KEY;
00058 };
00059 };
00060
00067 template <>
00068 struct KeyExtractor<NodeHandle> {
00069 static const OverlayKey& key(const NodeHandle& node)
00070 {
00071 return node.getKey();
00072 };
00073 };
00074
00075 template <>
00076 struct KeyExtractor<ProxNodeHandle> {
00077 static const OverlayKey& key(const ProxNodeHandle& node)
00078 {
00079 return node.getKey();
00080 };
00081 };
00082
00090 template <>
00091 struct KeyExtractor<std::pair<NodeHandle, simtime_t> > {
00092 static const OverlayKey& key(const std::pair<NodeHandle, simtime_t>& nodes)
00093 {
00094 return nodes.first.getKey();
00095 };
00096 };
00097
00098 template <class T>
00099 struct ProxExtractor {
00100 static Prox prox(const T&)
00101 {
00102 return Prox::PROX_UNKNOWN;
00103 };
00104 };
00105
00106 template <>
00107 struct ProxExtractor< ProxNodeHandle >{
00108 static Prox prox(const ProxNodeHandle& node)
00109 {
00110 return node.getProx();
00111 };
00112 };
00113
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 template <class T, class T_key, class T_prox>
00327 class BaseKeySortedVector : public std::vector<T> {
00328
00329 private:
00330 const Comparator<OverlayKey>* comparator;
00331 const AbstractProxComparator* proxComparator;
00332 const AbstractProxKeyComparator* proxKeyComparator;
00333 uint16_t maxSize;
00334 uint16_t sizeProx;
00335 uint16_t sizeComb;
00336
00337 public:
00348 BaseKeySortedVector(uint16_t maxSize = 0,
00349 const Comparator<OverlayKey>* comparator = NULL,
00350 const AbstractProxComparator* proxComparator = NULL,
00351 const AbstractProxKeyComparator* proxKeyComparator = NULL,
00352 uint16_t sizeProx = 0,
00353 uint16_t sizeComb = 0) :
00354 std::vector<T>(),
00355 comparator(comparator),
00356 proxComparator(proxComparator),
00357 proxKeyComparator(proxKeyComparator),
00358 maxSize(maxSize),
00359 sizeProx(sizeProx),
00360 sizeComb(sizeComb) { };
00361
00365 virtual ~BaseKeySortedVector() {};
00366
00367 typedef typename std::vector<T>::iterator iterator;
00368 typedef typename std::vector<T>::const_iterator const_iterator;
00370 static const T UNSPECIFIED_ELEMENT;
00373 public:
00374
00381 bool isAddable( const T& element ) const
00382 {
00383 if (maxSize == 0) {
00384 return true;
00385 }
00386
00387 return (std::vector<T>::size() != maxSize ||
00388 (comparator &&
00389 (comparator->compare(T_key::key(element),
00390 T_key::key(std::vector<T>::back())) <= 0 )) ||
00391 (proxComparator &&
00392 (proxComparator->compare(T_prox::prox(element),
00393 T_prox::prox(std::vector<T>::back())) <= 0 )) ||
00394 (proxKeyComparator &&
00395 (proxKeyComparator->compare(ProxKey(T_prox::prox(element),
00396 T_key::key(element)),
00397 ProxKey(T_prox::prox(std::vector<T>::back()),
00398 T_key::key(std::vector<T>::back()))) <= 0 )));
00399 };
00400
00406 bool isFull() const
00407 {
00408 return(std::vector<T>::size() == maxSize);
00409 };
00410
00416 bool isEmpty() const
00417 {
00418 return(std::vector<T>::size() == 0);
00419 };
00420
00428 int add( const T& element )
00429 {
00430 int pos = -1;
00431
00432
00433 if (isAddable(element)) {
00434
00435
00436 if ((std::vector<T>::size() != 0) &&
00437 (comparator || proxComparator || proxKeyComparator)) {
00438 iterator i;
00439 for (i = std::vector<T>::begin(), pos=0;
00440 i != std::vector<T>::end(); i++, pos++) {
00441
00442
00443 if (T_key::key(element) == T_key::key(*i)) {
00444 return -1;
00445 }
00446
00447 if (pos < sizeProx) {
00448 assert(proxComparator);
00449
00450 int compResult =
00451 proxComparator->compare(T_prox::prox(element),
00452 T_prox::prox(*i));
00453
00454 if (compResult < 0) {
00455 iterator temp_it = std::vector<T>::insert(i, element);
00456 T temp = *(temp_it++);
00457 std::vector<T>::erase(temp_it);
00458
00459 add(temp);
00460 break;
00461 }
00462 } else if (pos < sizeProx + sizeComb) {
00463 assert(proxKeyComparator);
00464
00465 int compResult = proxKeyComparator->compare(ProxKey(T_prox::prox(element), T_key::key(element)),
00466 ProxKey(T_prox::prox(*i), T_key::key(*i)));
00467 if (compResult < 0) {
00468 iterator temp_it = std::vector<T>::insert(i, element);
00469 T temp = *(temp_it++);
00470 std::vector<T>::erase(temp_it);
00471
00472 add(temp);
00473 break;
00474 }
00475 } else {
00476 assert(comparator);
00477
00478 int compResult = comparator->compare(T_key::key(element),
00479 T_key::key(*i));
00480 if (compResult < 0) {
00481 std::vector<T>::insert(i, element);
00482 break;
00483 }
00484 }
00485 }
00486 if (i == std::vector<T>::end()) {
00487 pos = std::vector<T>::size();
00488 push_back(element);
00489 }
00490 } else {
00491 for (iterator i = std::vector<T>::begin(); i != std::vector<T>::end();
00492 i++) {
00493
00494 if (T_key::key(element) == T_key::key(*i)) {
00495 return -1;
00496 }
00497 }
00498 pos = std::vector<T>::size();
00499 push_back(element);
00500 }
00501
00502
00503 if ((maxSize != 0) && (std::vector<T>::size() > maxSize)) {
00504 std::vector<T>::resize(maxSize);
00505 }
00506 }
00507 return pos;
00508 };
00509
00517 const bool contains(const OverlayKey& key) const {
00518 for (const_iterator i = std::vector<T>::begin();
00519 i != std::vector<T>::end(); i++) {
00520 if (T_key::key(*i) == key) return true;
00521 }
00522
00523 return false;
00524 }
00525
00533 const T& find(const OverlayKey& key) const
00534 {
00535 for (const_iterator i = std::vector<T>::begin();
00536 i != std::vector<T>::end(); i++) {
00537 if (T_key::key(*i) == key) return *i;
00538 }
00539 return UNSPECIFIED_ELEMENT;
00540 };
00541
00549 iterator findIterator(const OverlayKey& key)
00550 {
00551 iterator i;
00552 for (i = std::vector<T>::begin(); i != std::vector<T>::end(); i++)
00553 if (T_key::key(*i) == key) break;
00554 return i;
00555 }
00556
00562 void downsizeTo(const uint32_t maxElements)
00563 {
00564 if (std::vector<T>::size() > maxElements) {
00565 std::vector<T>::erase(std::vector<T>::begin()+maxElements, std::vector<T>::end());
00566 }
00567 }
00568
00569 void setComparator(const Comparator<OverlayKey>* comparator)
00570 {
00571 this->comparator = comparator;
00572 }
00573 };
00574 template <class T, class T_key, class T_rtt>
00575 const T BaseKeySortedVector<T, T_key, T_rtt>::UNSPECIFIED_ELEMENT;
00577 #endif