Public Types | Public Member Functions | Static Public Attributes | Private Attributes

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.

                                               :
    std::vector<T>(),
    comparator(comparator),
    proxComparator(proxComparator),
    proxKeyComparator(proxKeyComparator),
    maxSize(maxSize),
    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.

{};


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().

    {
        int pos = -1;

        // check if handle is addable
        if (isAddable(element)) { // yes ->

            // add handle to the appropriate position
            if ((std::vector<T>::size() != 0) &&
                (comparator || proxComparator || proxKeyComparator)) {
                iterator i;
                for (i = std::vector<T>::begin(), pos=0;
                     i != std::vector<T>::end(); i++, pos++) {

                    // don't add node with same key twice
                    if (T_key::key(element) == T_key::key(*i)) {
                        return -1;
                    }

                    if (pos < sizeProx) {
                        assert(proxComparator);
                        // only compare proximity
                        int compResult =
                            proxComparator->compare(T_prox::prox(element),
                                                    T_prox::prox(*i));
                        //if (T_prox::prox(element).compareTo(T_prox::prox(*i))) {
                        if (compResult < 0) {
                            iterator temp_it = std::vector<T>::insert(i, element);
                            T temp = *(temp_it++);
                            std::vector<T>::erase(temp_it);
                            //re-insert replaced entry into other 2 ranges
                            add(temp);
                            break;
                        }
                    } else if (pos < sizeProx + sizeComb) {
                        assert(proxKeyComparator);
                        // compare proximity and key distance
                        int compResult = proxKeyComparator->compare(ProxKey(T_prox::prox(element), T_key::key(element)),
                                                                    ProxKey(T_prox::prox(*i), T_key::key(*i)));
                        if (compResult < 0) {
                            iterator temp_it = std::vector<T>::insert(i, element);
                            T temp = *(temp_it++);
                            std::vector<T>::erase(temp_it);
                            //re-insert replaced entry into last range
                            add(temp);
                            break;
                        }
                    } else {
                        assert(comparator);
                        // only consider key distance
                        int compResult = comparator->compare(T_key::key(element),
                                                             T_key::key(*i));
                        if (compResult < 0) {
                            std::vector<T>::insert(i, element);
                            break;
                        }
                    }
                }
                if (i == std::vector<T>::end()) {
                    pos = std::vector<T>::size();
                    push_back(element);
                }
            } else {
                for (iterator i = std::vector<T>::begin(); i != std::vector<T>::end();
                     i++) {
                    // don't add node with same key twice
                    if (T_key::key(element) == T_key::key(*i)) {
                        return -1;
                    }
                }
                pos = std::vector<T>::size();
                push_back(element);
            }

            // adjust size
            if ((maxSize != 0) && (std::vector<T>::size() > maxSize)) {
                std::vector<T>::resize(maxSize);
            }
        }
        return pos;
    };

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().

                                                     {
        for (const_iterator i = std::vector<T>::begin();
                i != std::vector<T>::end(); i++) {
            if (T_key::key(*i) == key) return true;
        }

        return false;
    }

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().

    {
        if (std::vector<T>::size() > maxElements) {
            std::vector<T>::erase(std::vector<T>::begin()+maxElements, std::vector<T>::end());
        }
    }

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.

    {
        for (const_iterator i = std::vector<T>::begin();
                i != std::vector<T>::end(); i++) {
            if (T_key::key(*i) == key) return *i;
        }
        return UNSPECIFIED_ELEMENT;
    };

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().

    {
        iterator i;
        for (i = std::vector<T>::begin(); i != std::vector<T>::end(); i++)
            if (T_key::key(*i) == key) break;
        return i;
    }

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().

    {
        if (maxSize == 0) {
            return true;
        }

        return (std::vector<T>::size() != maxSize ||
               (comparator &&
                (comparator->compare(T_key::key(element),
                                     T_key::key(std::vector<T>::back())) <= 0 )) ||
               (proxComparator &&
                (proxComparator->compare(T_prox::prox(element),
                                         T_prox::prox(std::vector<T>::back())) <= 0 )) ||
               (proxKeyComparator &&
                (proxKeyComparator->compare(ProxKey(T_prox::prox(element),
                                                    T_key::key(element)),
                                            ProxKey(T_prox::prox(std::vector<T>::back()),
                                                    T_key::key(std::vector<T>::back()))) <= 0 )));
    };

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().

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

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().

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

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().

    {
        this->comparator = comparator;
    }


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 [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: