NodeVector.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2007 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00025 #ifndef __NODE_VECTOR_H
00026 #define __NODE_VECTOR_H
00027 
00028 #include <Comparator.h>
00029 #include <NodeHandle.h>
00030 #include <vector>
00031 
00032 template <class T> class KeyExtractor;
00033 template <class T> class RttExtractor;
00034 template <class T,
00035           class T_key = KeyExtractor<T>,
00036           class T_rtt = RttExtractor<T> > class BaseKeySortedVector;
00037 typedef BaseKeySortedVector< NodeHandle > NodeVector;
00038 typedef BaseKeySortedVector< std::pair<NodeHandle, simtime_t> > NodeRttVector;
00039 
00047 template <class T>
00048 struct KeyExtractor {
00049     static const OverlayKey& key(const T&)
00050     {
00051         return OverlayKey::UNSPECIFIED_KEY;
00052     };
00053 };
00054 
00061 template <>
00062 struct KeyExtractor<NodeHandle> {
00063     static const OverlayKey& key(const NodeHandle& node)
00064     {
00065         return node.key;
00066     };
00067 };
00068 
00078 template <>
00079 struct KeyExtractor<std::pair<NodeHandle, simtime_t> > {
00080     static const OverlayKey& key(const std::pair<NodeHandle, simtime_t>& nodes)
00081     {
00082         return nodes.first.key;
00083     };
00084 };
00085 
00086 template <class T>
00087 struct RttExtractor {
00088     static simtime_t rtt(const T&)
00089     {
00090         return MAXTIME;
00091     };
00092 };
00093 
00094 template <>
00095 struct RttExtractor<std::pair<NodeHandle, simtime_t> >{
00096     static simtime_t rtt(const std::pair<NodeHandle, simtime_t>& node)
00097     {
00098         return node.second;
00099     };
00100 };
00101 
00102 
00109 template <class T, class T_key, class T_rtt>
00110 class BaseKeySortedVector : public std::vector<T> {
00111 
00112 private://fields: comparator
00113 
00114     const Comparator<OverlayKey>* comparator; 
00115     uint16_t maxSize; 
00116     bool useRtt; 
00118 public://construction
00119 
00127     BaseKeySortedVector(uint16_t maxSize = 0,
00128                         const Comparator<OverlayKey>* comparator = NULL,
00129                         bool useRtt = false) :
00130     std::vector<T>(),
00131     comparator(comparator),
00132     maxSize(maxSize),
00133     useRtt(useRtt) { };
00134 
00138     virtual ~BaseKeySortedVector() {};
00139 
00140     typedef typename std::vector<T>::iterator iterator; 
00141     typedef typename std::vector<T>::const_iterator const_iterator; 
00143     static const T UNSPECIFIED_ELEMENT; 
00146 public://methods: sorted add support
00147 
00154     bool isAddable( const T& element ) const
00155     {
00156         if (maxSize == 0) {
00157             return false;
00158         }
00159 
00160         return(std::vector<T>::size() != maxSize ||
00161                 (comparator && ( comparator->compare( T_key::key(element),
00162                                                       T_key::key(std::vector<T>::back()) ) <= 0 )));
00163     };
00164 
00170     bool isFull() const
00171     {
00172         return(std::vector<T>::size() == maxSize);
00173     };
00174 
00180     bool isEmpty() const
00181     {
00182         return(std::vector<T>::size() == 0);
00183     };
00184 
00192     int add( const T& element )
00193     {
00194         int pos = -1;
00195 
00196         // check if handle is addable
00197         if (isAddable(element)) { // yes ->
00198 
00199             // add handle to the appropriate position
00200             if ((std::vector<T>::size() != 0) && comparator) {
00201                 iterator i;
00202                 for (i = std::vector<T>::begin(), pos=0;
00203                      i != std::vector<T>::end(); i++, pos++) {
00204 
00205                     // don't add node with same key twice
00206                     if (T_key::key(element) == T_key::key(*i)) {
00207                         return -1;
00208                     }
00209 
00210                     int compResult = comparator->compare(T_key::key(element),
00211                                                          T_key::key(*i));
00212                     if ((compResult < 0) ||
00213                             (useRtt && (compResult == 0) &&
00214                                     T_rtt::rtt(element) < T_rtt::rtt(*i))) {
00215 
00216                         std::vector<T>::insert(i, element);
00217                         break;
00218 
00219                         // if (useRtt &&
00220                         //    (compResult == 0) &&
00221                         //     T_rtt::rtt(element) < T_rtt::rtt(*i))
00222                         //     std::cout << "!!!" << std::endl;
00223                     }
00224                 }
00225                 if (i == std::vector<T>::end()) {
00226                     pos = std::vector<T>::size();
00227                     push_back(element);
00228                 }
00229             } else {
00230                 for (iterator i = std::vector<T>::begin(); i != std::vector<T>::end();
00231                      i++) {
00232                     // don't add node with same key twice
00233                     if (T_key::key(element) == T_key::key(*i)) {
00234                         return -1;
00235                     }
00236                 }
00237                 pos = std::vector<T>::size();
00238                 push_back(element);
00239             }
00240 
00241             // adjust size
00242             if ((maxSize != 0) && (std::vector<T>::size() > maxSize)) {
00243                 std::vector<T>::resize(maxSize);
00244             }
00245         }
00246 
00247         return pos;
00248     };
00249 
00257     const bool contains(const OverlayKey& key) const {
00258         for (const_iterator i = std::vector<T>::begin();
00259                 i != std::vector<T>::end(); i++) {
00260             if (T_key::key(*i) == key) return true;
00261         }
00262 
00263         return false;
00264     }
00265 
00273     const T& find(const OverlayKey& key) const
00274     {
00275         for (const_iterator i = std::vector<T>::begin();
00276                 i != std::vector<T>::end(); i++) {
00277             if (T_key::key(*i) == key) return *i;
00278         }
00279         return UNSPECIFIED_ELEMENT;
00280     };
00281 
00289     iterator findIterator(const OverlayKey& key)
00290     {
00291         iterator i;
00292         for (i = std::vector<T>::begin(); i != std::vector<T>::end(); i++)
00293             if (T_key::key(*i) == key) break;
00294         return i;
00295     }
00296 
00302     void downsizeTo(const uint32_t maxElements)
00303     {
00304         if (std::vector<T>::size() > maxElements) {
00305             std::vector<T>::erase(std::vector<T>::begin()+maxElements, std::vector<T>::end());
00306         }
00307     }
00308 
00309     void setComparator(const Comparator<OverlayKey>* comparator)
00310     {
00311         this->comparator = comparator;
00312     }
00313 };
00314 
00315 template <class T, class T_key, class T_rtt>
00316 const T BaseKeySortedVector<T, T_key, T_rtt>::UNSPECIFIED_ELEMENT; 
00318 #endif

Generated on Wed Mar 25 16:32:49 2009 for OverSim by  doxygen 1.5.5