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 <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 //template <class T, class T_key, class T_rtt>
00121 //class BaseKeySortedVector : public std::vector<T> {
00122 //
00123 //private://fields: comparator
00124 //
00125 //    const Comparator<OverlayKey>* comparator; /**< the OverlayKey Comparator for this vector */
00126 //    uint16_t maxSize; /**< maximum nodes this vector holds */
00127 //    bool useRtt; /**< sort by rtt after sorting using the comparator */
00128 //
00129 //public://construction
00130 //
00131 //    /**
00132 //     * constructor
00133 //     *
00134 //     * @param maxSize maximum nodes this vector holds
00135 //     * @param comparator OverlayKey Comparator for this vector
00136 //     * @param useRtt sort by rtt after sorting using the comparator
00137 //     */
00138 //    BaseKeySortedVector(uint16_t maxSize = 0,
00139 //                        const Comparator<OverlayKey>* comparator = NULL,
00140 //                        bool useRtt = false) :
00141 //    std::vector<T>(),
00142 //    comparator(comparator),
00143 //    maxSize(maxSize),
00144 //    useRtt(useRtt) { };
00145 //
00146 //    /**
00147 //     * destructor
00148 //     */
00149 //    virtual ~BaseKeySortedVector() {};
00150 //
00151 //    typedef typename std::vector<T>::iterator iterator; /**< iterator for this vector */
00152 //    typedef typename std::vector<T>::const_iterator const_iterator; /**< read-only iterator for this vector */
00153 //
00154 //    static const T UNSPECIFIED_ELEMENT; /**< unspecified element of type T */
00155 //
00156 //
00157 //public://methods: sorted add support
00158 //
00159 //    /**
00160 //     * indicates if an object of type T can be added to the NodeVector
00161 //     *
00162 //     * @param element the element to add
00163 //     * @return true if element can be added to the NodeVector, false otherwise
00164 //     */
00165 //    bool isAddable( const T& element ) const
00166 //    {
00167 //        if (maxSize == 0) {
00168 //            return false;
00169 //        }
00170 //
00171 //        return(std::vector<T>::size() != maxSize ||
00172 //                (comparator && ( comparator->compare( T_key::key(element),
00173 //                                                      T_key::key(std::vector<T>::back()) ) <= 0 )));
00174 //    };
00175 //
00176 //    /**
00177 //     * indicates if NodeVector holds maxSize nodes
00178 //     *
00179 //     * @return true if the actual size of NodeVector has reached its maxSize, false otherwise
00180 //     */
00181 //    bool isFull() const
00182 //    {
00183 //        return(std::vector<T>::size() == maxSize);
00184 //    };
00185 //
00186 //    /**
00187 //     * indicates if NodeVector holds at least one node
00188 //     *
00189 //     * @return true if NodeVector does not hold any node, false otherwise
00190 //     */
00191 //    bool isEmpty() const
00192 //    {
00193 //        return(std::vector<T>::size() == 0);
00194 //    };
00195 //
00196 //    /**
00197 //     * adds an element of type T in increasing order to the NodeVector and
00198 //     * returns the position of the added element or -1 if the element was not added
00199 //     *
00200 //     * @param element the element to add
00201 //     * @return position of the added element, -1 if the element was not added
00202 //     */
00203 //    int add( const T& element )
00204 //    {
00205 //        int pos = -1;
00206 //
00207 //        // check if handle is addable
00208 //        if (isAddable(element)) { // yes ->
00209 //
00210 //            // add handle to the appropriate position
00211 //            if ((std::vector<T>::size() != 0) && comparator) {
00212 //                iterator i;
00213 //                for (i = std::vector<T>::begin(), pos=0;
00214 //                     i != std::vector<T>::end(); i++, pos++) {
00215 //
00216 //                    // don't add node with same key twice
00217 //                    if (T_key::key(element) == T_key::key(*i)) {
00218 //                        return -1;
00219 //                    }
00220 //
00221 //                    int compResult = comparator->compare(T_key::key(element),
00222 //                                                         T_key::key(*i));
00223 //                    if ((compResult < 0) ||
00224 //                            (useRtt && (compResult == 0) &&
00225 //                                    T_rtt::rtt(element) < T_rtt::rtt(*i))) {
00226 //
00227 //                        std::vector<T>::insert(i, element);
00228 //                        break;
00229 //
00230 //                        // if (useRtt &&
00231 //                        //    (compResult == 0) &&
00232 //                        //     T_rtt::rtt(element) < T_rtt::rtt(*i))
00233 //                        //     std::cout << "!!!" << std::endl;
00234 //                    }
00235 //                }
00236 //                if (i == std::vector<T>::end()) {
00237 //                    pos = std::vector<T>::size();
00238 //                    push_back(element);
00239 //                }
00240 //            } else {
00241 //                for (iterator i = std::vector<T>::begin(); i != std::vector<T>::end();
00242 //                     i++) {
00243 //                    // don't add node with same key twice
00244 //                    if (T_key::key(element) == T_key::key(*i)) {
00245 //                        return -1;
00246 //                    }
00247 //                }
00248 //                pos = std::vector<T>::size();
00249 //                push_back(element);
00250 //            }
00251 //
00252 //            // adjust size
00253 //            if ((maxSize != 0) && (std::vector<T>::size() > maxSize)) {
00254 //                std::vector<T>::resize(maxSize);
00255 //            }
00256 //        }
00257 //
00258 //        return pos;
00259 //    };
00260 //
00261 //    /**
00262 //     * Searches for an OverlayKey in NodeVector and returns true, if
00263 //     * it is found.
00264 //     *
00265 //     * @param key the OverlayKey to find
00266 //     * @return true, if the vector contains the key
00267 //     */
00268 //    const bool contains(const OverlayKey& key) const {
00269 //        for (const_iterator i = std::vector<T>::begin();
00270 //                i != std::vector<T>::end(); i++) {
00271 //            if (T_key::key(*i) == key) return true;
00272 //        }
00273 //
00274 //        return false;
00275 //    }
00276 //
00277 //    /**
00278 //     * searches for an OverlayKey in NodeVector
00279 //     *
00280 //     * @param key the OverlayKey to find
00281 //     * @return the UNSPECIFIED_ELEMENT if there is no element with the defined key,
00282 //     * the found element of type T otherwise
00283 //     */
00284 //    const T& find(const OverlayKey& key) const
00285 //    {
00286 //        for (const_iterator i = std::vector<T>::begin();
00287 //                i != std::vector<T>::end(); i++) {
00288 //            if (T_key::key(*i) == key) return *i;
00289 //        }
00290 //        return UNSPECIFIED_ELEMENT;
00291 //    };
00292 //
00293 //    /**
00294 //     * Searches for an OberlayKey in a NodeVector and returns an
00295 //     * appropriate iterator.
00296 //     *
00297 //     * @param key The key to search
00298 //     * @return iterator The iterator
00299 //     */
00300 //    iterator findIterator(const OverlayKey& key)
00301 //    {
00302 //        iterator i;
00303 //        for (i = std::vector<T>::begin(); i != std::vector<T>::end(); i++)
00304 //            if (T_key::key(*i) == key) break;
00305 //        return i;
00306 //    }
00307 //
00308 //    /**
00309 //     * Downsize the vector to a maximum of maxElements.
00310 //     *
00311 //     * @param maxElements The maximum number of elements after downsizing
00312 //     */
00313 //    void downsizeTo(const uint32_t maxElements)
00314 //    {
00315 //        if (std::vector<T>::size() > maxElements) {
00316 //            std::vector<T>::erase(std::vector<T>::begin()+maxElements, std::vector<T>::end());
00317 //        }
00318 //    }
00319 //
00320 //    void setComparator(const Comparator<OverlayKey>* comparator)
00321 //    {
00322 //        this->comparator = comparator;
00323 //    }
00324 //};
00325 
00326 template <class T, class T_key, class T_prox>
00327 class BaseKeySortedVector : public std::vector<T> {
00328 
00329 private://fields: comparator
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://construction
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://methods: sorted add support
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         // check if handle is addable
00433         if (isAddable(element)) { // yes ->
00434 
00435             // add handle to the appropriate position
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                     // don't add node with same key twice
00443                     if (T_key::key(element) == T_key::key(*i)) {
00444                         return -1;
00445                     }
00446 
00447                     if (pos < sizeProx) {
00448                         assert(proxComparator);
00449                         // only compare proximity
00450                         int compResult =
00451                             proxComparator->compare(T_prox::prox(element),
00452                                                     T_prox::prox(*i));
00453                         //if (T_prox::prox(element).compareTo(T_prox::prox(*i))) {
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                             //re-insert replaced entry into other 2 ranges
00459                             add(temp);
00460                             break;
00461                         }
00462                     } else if (pos < sizeProx + sizeComb) {
00463                         assert(proxKeyComparator);
00464                         // compare proximity and key distance
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                             //re-insert replaced entry into last range
00472                             add(temp);
00473                             break;
00474                         }
00475                     } else {
00476                         assert(comparator);
00477                         // only consider key distance
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                     // don't add node with same key twice
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             // adjust size
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
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3