Files
scandocs/uni/masterarbeit/source/moversight/mt/LTNodeQueue.cc
2014-06-30 13:58:10 +02:00

179 lines
4.8 KiB
C++

/*
* File: LTNodeQueue.cc
* Author: jgaebler
*
* Created on April 14, 2010, 9:43 AM
*/
#include "LTNodeQueue.h"
#include "mt/msg/MulticastMessage.h"
namespace ubeeme {
namespace moversight {
/**
* @brief Constructor
*/
LTNodeQueue::LTNodeQueue() {
queue.clear();
}//End LTNodeQueue
/**
* @brief Destructor
*/
LTNodeQueue::~LTNodeQueue() {
}//End ~LTNodeQueue
/**
* @brief Adds a node to the queue.
* @param node the node to add
*/
void
LTNodeQueue::add(LTNode & node) {
queue.push_back(node);
}//End add
/**
* @brief Clears the queue.
*/
void
LTNodeQueue::clear() {
queue.clear();
}//End clear
/**
* @brief Returns the actual size of the queue.
* @return the size of the queue
*/
size_t
LTNodeQueue::size() const {
return queue.size();
}//End size
/**
* @brief Sorts the queue according to the virtual time field of the stored node in ascending order.
*/
void
LTNodeQueue::sort() {
std::sort(queue.begin(), queue.end());
}//End sort
/**
* @brief Returns the first element of the queue.
* @return the first element of the queue
*/
LTNode &
LTNodeQueue::front() {
return *queue.begin();
}
/**
* @brief Permits read/write access to the element at the given index.
* @param i The index to access.
* @return The desired element
* @throw IndexOutOfBounceException Exceeds the given index the queue size.
*/
LTNode &
LTNodeQueue::get(const unsigned int i) {
if(i < queue.size()){
return queue.at(i);
}
throw IndexOutOfBounceException("the given index exceeds the queue size");
}
/**
* @brief Removes the first element of the queue.
*/
void
LTNodeQueue::dequeue() {
if (queue.begin() != queue.end()) {
MulticastMessage* elem = queue.begin()->getMessage();
queue.erase(queue.begin());
delete elem;
}//End if
}
/**
* @brief Determines, if a message stored within the queue, identified by the message referenence.
* @param ref the message reference of the search message
* @return true, if the message identified by the message reference stored within the queue, false otherwhise
*/
bool
LTNodeQueue::contains(MessageReference & ref) const {
for (size_t i = 0; i < queue.size(); i++) {
if (queue[i].getMessageReference() == ref) {
return true;
}//End if
}//end for
return false;
}
/**
* @brief Determines, if a message stored within the queue, identified by the message referenence. The searched node is returned to the caller, if it stored within the queue, NULL otherwise.
* @param ref the message reference, identifing the search node
* @return the searched node, if it is stored within the queue, NULL otherwise
*/
LTNode *
LTNodeQueue::find(MessageReference & ref) {
NodeQueue::iterator it;
PeerID nodeId = ref.getPeerID();
unsigned int sequ = ref.getSequenceNumber();
it = std::find_if(queue.begin(),
queue.end(),
LTNodeEqualComparatorFunctor(LTNode(sequ, nodeId, ref)));
if (it != queue.end()) {
return &(*it);
}//End if
return NULL;
}
/**
* @brief Removes a message from the queue, identified by the message reference. If the message not found, nothing happens.
* @param ref The message reference of the message to remove.
*/
void
LTNodeQueue::remove(MessageReference & ref) {
NodeQueue::iterator it;
PeerID nodeId = ref.getPeerID();
unsigned int sequ = ref.getSequenceNumber();
it = std::find_if(queue.begin(),
queue.end(),
LTNodeEqualComparatorFunctor(LTNode(sequ, nodeId, ref)));
if (it != queue.end()) {
queue.erase(it);
}//End if
}
/**
* @brief Returns, if the queue empty or not.
* @return False, if the size > 0, true otherwise.
*/
bool
LTNodeQueue::isEmpty(){
return (queue.size() > 0? false: true);
}
};
};