BaseKeySortedVector< T, T_key, T_prox > Class Template Reference

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

#include <NodeVector.h>

List of all members.

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, const AbstractProxComparator *proxComparator=NULL, const AbstractProxKeyComparator *proxKeyComparator=NULL, uint16_t sizeProx=0, uint16_t sizeComb=0)
 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 uint32_t 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
const AbstractProxComparatorproxComparator
const AbstractProxKeyComparatorproxKeyComparator
uint16_t maxSize
 maximum nodes this vector holds
uint16_t sizeProx
uint16_t sizeComb

Detailed Description

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

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

Author:
Sebastian Mies constructor
Parameters:
maxSize maximum nodes this vector holds
comparator OverlayKey Comparator for this vector
useRtt sort by rtt after sorting using the comparator destructor indicates if an object of type T can be added to the NodeVector
element the element to add
Returns:
true if element can be added to the NodeVector, false otherwise indicates if NodeVector holds maxSize nodes
true if the actual size of NodeVector has reached its maxSize, false otherwise indicates if NodeVector holds at least one node
true if NodeVector does not hold any node, false otherwise 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 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 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 Searches for an OberlayKey in a NodeVector and returns an appropriate iterator.
Parameters:
key The key to search
Returns:
iterator The iterator Downsize the vector to a maximum of maxElements.
Parameters:
maxElements The maximum number of elements after downsizing

Definition at line 327 of file NodeVector.h.


Member Typedef Documentation

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

read-only iterator for this vector

Definition at line 368 of file NodeVector.h.

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

iterator for this vector

Definition at line 365 of file NodeVector.h.


Constructor & Destructor Documentation

template<class T, class T_key, class T_prox>
BaseKeySortedVector< T, T_key, T_prox >::BaseKeySortedVector ( uint16_t  maxSize = 0,
const Comparator< OverlayKey > *  comparator = NULL,
const AbstractProxComparator proxComparator = NULL,
const AbstractProxKeyComparator proxKeyComparator = NULL,
uint16_t  sizeProx = 0,
uint16_t  sizeComb = 0 
) [inline]

constructor

Parameters:
maxSize maximum nodes this vector holds
comparator OverlayKey comparator for this vector
proxComparator proximity comparator for this vector
proxKeyComparator proximity/key comparator for this vector
sizeProx number of nodes sorted by proximity
sizeComb number of nodes sorted by proximity/key

Definition at line 348 of file NodeVector.h.

00354                                                :
00355     std::vector<T>(),
00356     comparator(comparator),
00357     proxComparator(proxComparator),
00358     proxKeyComparator(proxKeyComparator),
00359     maxSize(maxSize),
00360     sizeProx(sizeProx),
    sizeComb(sizeComb) { };

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

destructor

Definition at line 365 of file NodeVector.h.

00365 {};


Member Function Documentation

template<class T, class T_key, class T_prox>
int BaseKeySortedVector< T, T_key, T_prox >::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

Definition at line 428 of file NodeVector.h.

Referenced by BaseKeySortedVector< LookupEntry >::add(), IterativePathLookup::add(), IterativeLookup::addSibling(), IterativeLookup::checkStop(), BrooseBucket::fillVector(), PastryRoutingTable::findCloserNodes(), PastryNeighborhoodSet::findCloserNodes(), PastryLeafSet::findCloserNodes(), MyOverlay::findNode(), Kademlia::findNode(), Broose::findNode(), BasePastry::findNode(), IterativePathLookup::handleFailedNodeResponse(), DHT::handlePutRequest(), IterativePathLookup::handleTimeout(), Kademlia::refillSiblingTable(), and Kademlia::routingAdd().

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

template<class T, class T_key, class T_prox>
const bool BaseKeySortedVector< T, T_key, T_prox >::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

Definition at line 517 of file NodeVector.h.

Referenced by BasePastry::isSiblingFor().

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

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

Downsize the vector to a maximum of maxElements.

Parameters:
maxElements The maximum number of elements after downsizing

Definition at line 562 of file NodeVector.h.

Referenced by oversim::Chord::findNode().

00563     {
00564         if (std::vector<T>::size() > maxElements) {
00565             std::vector<T>::erase(std::vector<T>::begin()+maxElements, std::vector<T>::end());
00566         }
00567     }

template<class T, class T_key, class T_prox>
const T& BaseKeySortedVector< T, T_key, T_prox >::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

Definition at line 533 of file NodeVector.h.

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

template<class T, class T_key, class T_prox>
iterator BaseKeySortedVector< T, T_key, T_prox >::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

Definition at line 549 of file NodeVector.h.

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

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     }

template<class T, class T_key, class T_prox>
bool BaseKeySortedVector< T, T_key, T_prox >::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

Definition at line 381 of file NodeVector.h.

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

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

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

indicates if NodeVector holds at least one node

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

Definition at line 416 of file NodeVector.h.

Referenced by Kademlia::findNode().

00417     {
00418         return(std::vector<T>::size() == 0);
00419     };

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

indicates if NodeVector holds maxSize nodes

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

Definition at line 406 of file NodeVector.h.

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

00407     {
00408         return(std::vector<T>::size() == maxSize);
00409     };

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

Definition at line 569 of file NodeVector.h.

Referenced by Broose::findNode(), and Kademlia::routingInit().

00570     {
00571         this->comparator = comparator;
00572     }


Member Data Documentation

template<class T, class T_key, class T_prox>
const Comparator<OverlayKey>* BaseKeySortedVector< T, T_key, T_prox >::comparator [private]
template<class T, class T_key, class T_prox>
uint16_t BaseKeySortedVector< T, T_key, T_prox >::maxSize [private]
template<class T, class T_key, class T_prox>
const AbstractProxComparator* BaseKeySortedVector< T, T_key, T_prox >::proxComparator [private]
template<class T, class T_key, class T_prox>
const AbstractProxKeyComparator* BaseKeySortedVector< T, T_key, T_prox >::proxKeyComparator [private]
template<class T, class T_key, class T_prox>
uint16_t BaseKeySortedVector< T, T_key, T_prox >::sizeComb [private]

Definition at line 335 of file NodeVector.h.

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

template<class T, class T_key, class T_prox>
uint16_t BaseKeySortedVector< T, T_key, T_prox >::sizeProx [private]

Definition at line 334 of file NodeVector.h.

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

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

unspecified element of type T

an unspecified element of the NodeVector

Definition at line 370 of file NodeVector.h.

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


The documentation for this class was generated from the following file:
Generated on Wed May 26 16:21:16 2010 for OverSim by  doxygen 1.6.3