#include <NodeVector.h>
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 |
typedef std::vector<T>::iterator BaseKeySortedVector< T, T_key, T_rtt >::iterator |
iterator for this vector
typedef std::vector<T>::const_iterator BaseKeySortedVector< T, T_key, T_rtt >::const_iterator |
read-only iterator for this vector
BaseKeySortedVector< T, T_key, T_rtt >::BaseKeySortedVector | ( | uint16_t | maxSize = 0 , |
|
const Comparator< OverlayKey > * | comparator = NULL , |
|||
bool | useRtt = false | |||
) | [inline] |
constructor
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) { };
virtual BaseKeySortedVector< T, T_key, T_rtt >::~BaseKeySortedVector | ( | ) | [inline, virtual] |
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
element | the element to add |
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 };
bool BaseKeySortedVector< T, T_key, T_rtt >::isFull | ( | ) | const [inline] |
indicates if NodeVector holds maxSize nodes
Referenced by IterativeLookup::addSibling(), Kademlia::findNode(), Kademlia::refillSiblingTable(), and Kademlia::routingAdd().
00171 { 00172 return(std::vector<T>::size() == maxSize); 00173 };
bool BaseKeySortedVector< T, T_key, T_rtt >::isEmpty | ( | ) | const [inline] |
indicates if NodeVector holds at least one node
Referenced by Kademlia::isSiblingFor().
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
element | the element to add |
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 };
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.
key | the OverlayKey to find |
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 }
const T& BaseKeySortedVector< T, T_key, T_rtt >::find | ( | const OverlayKey & | key | ) | const [inline] |
searches for an OverlayKey in NodeVector
key | the OverlayKey to find |
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 };
iterator BaseKeySortedVector< T, T_key, T_rtt >::findIterator | ( | const OverlayKey & | key | ) | [inline] |
Searches for an OberlayKey in a NodeVector and returns an appropriate iterator.
key | The key to search |
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 }
void BaseKeySortedVector< T, T_key, T_rtt >::downsizeTo | ( | const uint | maxElements | ) | [inline] |
Downsize the vector to a maximum of maxElements.
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 }
void BaseKeySortedVector< T, T_key, T_rtt >::setComparator | ( | const Comparator< OverlayKey > * | comparator | ) | [inline] |
const Comparator<OverlayKey>* BaseKeySortedVector< T, T_key, T_rtt >::comparator [private] |
the OverlayKey Comparator for this vector
Referenced by BaseKeySortedVector< LookupEntry >::add(), and BaseKeySortedVector< LookupEntry >::isAddable().
uint16_t BaseKeySortedVector< T, T_key, T_rtt >::maxSize [private] |
maximum nodes this vector holds
Referenced by BaseKeySortedVector< LookupEntry >::add(), BaseKeySortedVector< LookupEntry >::isAddable(), and BaseKeySortedVector< LookupEntry >::isFull().
bool BaseKeySortedVector< T, T_key, T_rtt >::useRtt [private] |
sort by rtt after sorting using the comparator
Referenced by BaseKeySortedVector< LookupEntry >::add().
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().