BaseKeySortedVector< T, T_key, T_rtt > Class Template Reference

#include <NodeVector.h>

List of all members.


Detailed Description

template<class T, class T_key, class T_rtt>
class BaseKeySortedVector< T, T_key, T_rtt >

A STL-vector that supports inserts sorted by an OverlayKey found somewhere in the type.

Author:
Sebastian Mies

Public Types

typedef std::vector< T >::iterator iterator
 iterator for this vector
typedef std::vector< T >
::const_iterator 
const_iterator
 read-only iterator for this vector

Public Member Functions

 BaseKeySortedVector (uint16_t maxSize=0, const Comparator< OverlayKey > *comparator=NULL, bool useRtt=false)
 constructor
virtual ~BaseKeySortedVector ()
 destructor
bool isAddable (const T &element) const
 indicates if an object of type T can be added to the NodeVector
bool isFull () const
 indicates if NodeVector holds maxSize nodes
bool isEmpty () const
 indicates if NodeVector holds at least one node
int add (const T &element)
 adds an element of type T in increasing order to the NodeVector and returns the position of the added element or -1 if the element was not added
const bool contains (const OverlayKey &key) const
 Searches for an OverlayKey in NodeVector and returns true, if it is found.
const T & find (const OverlayKey &key) const
 searches for an OverlayKey in NodeVector
iterator findIterator (const OverlayKey &key)
 Searches for an OberlayKey in a NodeVector and returns an appropriate iterator.
void downsizeTo (const uint maxElements)
 Downsize the vector to a maximum of maxElements.
void setComparator (const Comparator< OverlayKey > *comparator)

Static Public Attributes

static const T UNSPECIFIED_ELEMENT
 unspecified element of type T

Private Attributes

const Comparator< OverlayKey > * comparator
 the OverlayKey Comparator for this vector
uint16_t maxSize
 maximum nodes this vector holds
bool useRtt
 sort by rtt after sorting using the comparator

Member Typedef Documentation

template<class T, class T_key, class T_rtt>
typedef std::vector<T>::iterator BaseKeySortedVector< T, T_key, T_rtt >::iterator

iterator for this vector

template<class T, class T_key, class T_rtt>
typedef std::vector<T>::const_iterator BaseKeySortedVector< T, T_key, T_rtt >::const_iterator

read-only iterator for this vector


Constructor & Destructor Documentation

template<class T, class T_key, class T_rtt>
BaseKeySortedVector< T, T_key, T_rtt >::BaseKeySortedVector ( uint16_t  maxSize = 0,
const Comparator< OverlayKey > *  comparator = NULL,
bool  useRtt = false 
) [inline]

constructor

Parameters:
maxSize maximum nodes this vector holds
comparator OverlayKey Comparator for this vector
useRtt sort by rtt after sorting using the comparator
00129                                              :
00130     std::vector<T>(),
00131     comparator(comparator),
00132     maxSize(maxSize),
00133     useRtt(useRtt) { };

template<class T, class T_key, class T_rtt>
virtual BaseKeySortedVector< T, T_key, T_rtt >::~BaseKeySortedVector (  )  [inline, virtual]

destructor

00138 {};


Member Function Documentation

template<class T, class T_key, class T_rtt>
bool BaseKeySortedVector< T, T_key, T_rtt >::isAddable ( const T &  element  )  const [inline]

indicates if an object of type T can be added to the NodeVector

Parameters:
element the element to add
Returns:
true if element can be added to the NodeVector, false otherwise

Referenced by BaseKeySortedVector< LookupEntry >::add(), and Kademlia::routingAdd().

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     };

template<class T, class T_key, class T_rtt>
bool BaseKeySortedVector< T, T_key, T_rtt >::isFull (  )  const [inline]

indicates if NodeVector holds maxSize nodes

Returns:
true if the actual size of NodeVector has reached its maxSize, false otherwise

Referenced by IterativeLookup::addSibling(), Kademlia::findNode(), Kademlia::refillSiblingTable(), and Kademlia::routingAdd().

00171     {
00172         return(std::vector<T>::size() == maxSize);
00173     };

template<class T, class T_key, class T_rtt>
bool BaseKeySortedVector< T, T_key, T_rtt >::isEmpty (  )  const [inline]

indicates if NodeVector holds at least one node

Returns:
true if NodeVector does not hold any node, false otherwise

Referenced by Kademlia::isSiblingFor().

00181     {
00182         return(std::vector<T>::size() == 0);
00183     };

template<class T, class T_key, class T_rtt>
int BaseKeySortedVector< T, T_key, T_rtt >::add ( const T &  element  )  [inline]

adds an element of type T in increasing order to the NodeVector and returns the position of the added element or -1 if the element was not added

Parameters:
element the element to add
Returns:
position of the added element, -1 if the element was not added

Referenced by IterativePathLookup::add(), IterativeLookup::addSibling(), PastryRoutingTable::findCloserNodes(), PastryNeighborhoodSet::findCloserNodes(), PastryLeafSet::findCloserNodes(), Kademlia::findNode(), BasePastry::findNode(), IterativePathLookup::handleFailedNodeResponse(), IterativePathLookup::handleTimeout(), Kademlia::refillSiblingTable(), and Kademlia::routingAdd().

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     };

template<class T, class T_key, class T_rtt>
const bool BaseKeySortedVector< T, T_key, T_rtt >::contains ( const OverlayKey key  )  const [inline]

Searches for an OverlayKey in NodeVector and returns true, if it is found.

Parameters:
key the OverlayKey to find
Returns:
true, if the vector contains the key

Referenced by BasePastry::isSiblingFor().

00257                                                      {
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     }

template<class T, class T_key, class T_rtt>
const T& BaseKeySortedVector< T, T_key, T_rtt >::find ( const OverlayKey key  )  const [inline]

searches for an OverlayKey in NodeVector

Parameters:
key the OverlayKey to find
Returns:
the UNSPECIFIED_ELEMENT if there is no element with the defined key, the found element of type T otherwise
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     };

template<class T, class T_key, class T_rtt>
iterator BaseKeySortedVector< T, T_key, T_rtt >::findIterator ( const OverlayKey key  )  [inline]

Searches for an OberlayKey in a NodeVector and returns an appropriate iterator.

Parameters:
key The key to search
Returns:
iterator The iterator

Referenced by IterativePathLookup::handleTimeout(), Kademlia::routingAdd(), and Kademlia::routingTimeout().

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     }

template<class T, class T_key, class T_rtt>
void BaseKeySortedVector< T, T_key, T_rtt >::downsizeTo ( const uint  maxElements  )  [inline]

Downsize the vector to a maximum of maxElements.

Parameters:
maxElements The maximum number of elements after downsizing

Referenced by Chord::findNode().

00303     {
00304         if (std::vector<T>::size() > maxElements) {
00305             std::vector<T>::erase(std::vector<T>::begin()+maxElements, std::vector<T>::end());
00306         }
00307     }

template<class T, class T_key, class T_rtt>
void BaseKeySortedVector< T, T_key, T_rtt >::setComparator ( const Comparator< OverlayKey > *  comparator  )  [inline]

00310     {
00311         this->comparator = comparator;
00312     }


Member Data Documentation

template<class T, class T_key, class T_rtt>
const Comparator<OverlayKey>* BaseKeySortedVector< T, T_key, T_rtt >::comparator [private]

template<class T, class T_key, class T_rtt>
uint16_t BaseKeySortedVector< T, T_key, T_rtt >::maxSize [private]

template<class T, class T_key, class T_rtt>
bool BaseKeySortedVector< T, T_key, T_rtt >::useRtt [private]

sort by rtt after sorting using the comparator

Referenced by BaseKeySortedVector< LookupEntry >::add().

template<class T, class T_key, class T_rtt>
const T BaseKeySortedVector< T, T_key, T_rtt >::UNSPECIFIED_ELEMENT [inline, static]

unspecified element of type T

an unspecified element of the NodeVector

Referenced by BaseKeySortedVector< LookupEntry >::find().


The documentation for this class was generated from the following file:

Generated on Fri Sep 19 13:05:05 2008 for ITM OverSim by  doxygen 1.5.5