Files
2014-06-30 13:58:10 +02:00

257 lines
6.5 KiB
C++

/*
* File: PairQueue.h
* Author: jgaebler
*
* Created on October 29, 2010, 2:06 PM
*/
#pragma once
#ifndef PAIRQUEUE_H
#define PAIRQUEUE_H
#include "common/Defines.h"
#include "List.h"
namespace ubeeme {
namespace moversight {
/**
* @class PairQueue
* @brief Provides a generic queue to store elements.
* Internal, each element consist of a identifing field and a field, which is aimed to store, thus, the elements will stored as pairs within the queue.
* The first pair element identifies the pair, the second is the element to store.
* @author Jan Gäbler
* @ingroup Moversight
*/
template < typename S, typename T >
class PairQueue {
public:
PairQueue();
virtual ~PairQueue();
void clear();
void remove(S const & key);
T find(S const & key);
T get(int index);
S getKey(int index);
T front();
void pop();
virtual void add(T t) = 0;
size_t size() const;
bool contains(S const & key);
List<S> getKeyList() const;
protected:
void add(S key, T t);
private:
typedef std::pair < S, T > STPair;
typedef std::vector < STPair > STPairList;
STPairList queue;
class STPairListFunctor {
public:
STPairListFunctor(S const & key) {
orig = key;
}
bool operator()(STPair & elem) {
return orig == elem.first;
}
private:
S orig;
};
};
/**
* @brief Constructor. Creates an empty queue.
*/
template <typename S, typename T>
PairQueue<S, T>::PairQueue() {
queue.clear();
}
/**
* @brief Destructor.
*/
template <typename S, typename T>
PairQueue<S, T>::~PairQueue() {
}
/**
* @brief Clears the queue.
*/
template <typename S, typename T>
void
PairQueue<S, T>::clear() {
queue.clear();
}
/**
* @brief Removes a timer from the queue, identified by the key, otherwise, it does nothing.
* @param key Identify the element to remove
*/
template <typename S, typename T>
void
PairQueue<S, T>::remove(S const & key) {
typename STPairList::iterator it;
it = std::find_if(queue.begin(),
queue.end(),
STPairListFunctor(key));
if (it != queue.end()) {
queue.erase(it);
}//End if
}
/**
* @brief Returns a read/write reference to the data at the first element of the queue.
* @return The desired reference.
*/
template <typename S, typename T>
T
PairQueue<S, T>::front() {
return queue.front().second;
}
/**
* @brief Removes the first element of the queue.
* @note The use of this operation could be expensive. See, %vector%.erase for more information.
*/
template <typename S, typename T>
void
PairQueue<S, T>::pop() {
queue.erase(queue.begin());
}
/**
* @brief Returns the second part of given element from the queue.
* @param i The element index.
* @return The desired element.
*/
template <typename S, typename T>
T
PairQueue<S, T>::get(int i) {
return (queue.at(i).second);
}
/**
* @brief Returns the first part of the given element from the queue.
* @param i The element index.
* @return The desired element.
*/
template <typename S, typename T>
S
PairQueue<S, T>::getKey(int i) {
return (queue.at(i).first);
}
/**
* @brief Searches, if the timer associated to the element identified by
* the key stored in the queue and returns it. If the timer not stored
* within the queue, the method returns NULL.
* @param key identify the element, associated with the timer
* @return the timer, if it stored within the queue, NULL otherwise
*/
template <typename S, typename T>
T
PairQueue<S, T>::find(S const & key) {
typename STPairList::iterator it;
it = std::find_if(queue.begin(),
queue.end(),
STPairListFunctor(key));
if (it == queue.end()) {
return NULL;
}//End if
return (it->second);
}
/**
* @brief Adds an element to the queue.
* @param key The identifying element
* @param t The associated object
*/
template <typename S, typename T>
void
PairQueue<S, T>::add(S key, T t) {
STPair elem = std::make_pair(key, t);
queue.push_back(elem);
}
/**
* @brief Returns the size of queue.
* @return the size of the queue
*/
template <typename S, typename T>
std::size_t
PairQueue<S, T>::size() const {
return queue.size();
}
/**
* @brief Method checks whether the given key is contained in the queue or not.
* @param key The key we're looking for.
* @return True if the key could be found in the queue, false else.
*/
template <typename S, typename T>
bool
PairQueue<S, T>::contains(S const & key) {
typename STPairList::iterator it;
it = std::find_if(queue.begin(),
queue.end(),
STPairListFunctor(key));
if (it == queue.end()) {
return false;
}//End if
return true;
}
/**
* @brief Creates a list with the keys stored in the queue.
* @return A list of keys stored in this queue.
*/
template <typename S, typename T>
List<S>
PairQueue<S,T>::getKeyList() const {
List<S> list;
for(size_t i=0; i<queue.size(); i++){
list.add(queue.at(i).first);
}
return list;
}
};
};
#endif /* PAIRQUEUE_H */