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

240 lines
5.2 KiB
C++

/*
* File: Set.h
* Author: jgaebler
*
* Created on January 30, 2014, 3:33 PM
*/
#pragma once
#ifndef SET_H
#define SET_H
#include "common/Defines.h"
#include "common/Exception.h"
#include <set>
#include <vector>
namespace ubeeme {
namespace moversight {
/**
* @class Set
* @brief Provides a set for Moversight objects. Within the set, each object is unique.
* @ingroup Moversight
* @author Jan G&auml;bler
*/
template <typename T>
class Set {
public:
Set();
Set(const Set & orig);
virtual ~Set();
Set & add(const T & t);
void add(const std::vector<T> v);
void clear();
size_t size() const;
bool contains(const T & a) const;
void remove(const T & a);
T get(size_t index) const;
void pop();
T first() const;
std::set<T> & getInternal();
T getMinElem() const;
std::vector<T> toVector() const;
protected:
typedef std::set<T> TSet;
TSet set;
};
/**
* @brief Constructor
*/
template <typename T>
Set<T>::Set() {
}
/**
* @brief Copy constructor
* @param orig the list to copy
*/
template <typename T>
Set<T>::Set(const Set & orig) : set(orig.set) {
}
/**
* @brief Destructor
*/
template <typename T>
Set<T>::~Set() {
}
/**
* @brief Clears the list
*/
template <typename T>
void
Set<T>::clear() {
set.clear();
}
/**
* @brief Adds a object to the list.
* @param t the object to add
* @return This list.
*/
template <typename T>
Set<T> &
Set<T>::add(const T & t) {
set.insert(t);
return *this;
}
/**
* @brief Adds a vector to the set.
* @param v Stores the elements to add to the set.
*/
template <typename T>
void
Set<T>::add(const std::vector<T> v){
set.insert(v.begin(), v.end());
}
/**
* @brief Determines the size of the list
* @return the list size
*/
template <typename T>
size_t
Set<T>::size() const {
return set.size();
}
/**
* @brief Checks, if a given object element of this list
* @param a the object to check
* @returns true, if the given object element of the list, false otherwise
*/
template <typename T>
bool
Set<T>::contains(const T & a) const {
typename TSet::const_iterator it = set.find(a);
if(it != set.end()){
return true;
}
return false;
}
/**
* @brief Returns the object from the set, which is located at the given index.
* @throws IndexOutOfBounceException if the given index larger than the current size of the set
* @param index the index of the element to return
*/
template <typename T>
T
Set<T>::get(size_t index) const {
if (index > set.size()) {
throw IndexOutOfBounceException("index out of bounce");
}//End if
auto it = std::next(set.begin(), index);
return *it;
}
/**
* @brief Removes a given object from the set.
* @param a the object to remove
*/
template <typename T>
void
Set<T>::remove(const T & a) {
typename TSet::iterator it = set.find(a);
if(it != set.end()){
set.erase(it);
}
}
/**
* @brief Remove the first element of the list.
*/
template <typename T>
void
Set<T>::pop() {
set.erase(set.begin());
}
/**
* @brief Returns the first element of the queue.
* @return The first element of the queue.
*/
template <typename T>
T
Set<T>::first() const {
return *set.begin();
}
/**
* @brief Permits access to the internal data structure.
* @return The internal data structure.
*/
template <typename T>
std::set<T>&
Set<T>::getInternal() {
return set;
}
/**
* @brief Returns the smallest element of the set.
* @return The smallest element of the set.
*/
template <typename T>
T
Set<T>::getMinElem() const {
return * std::min_element(set.begin(), set.end());
}
/**
* @brief Converts the set into a vector.
* @return A vector, representing the set.
*/
template <typename T>
std::vector<T>
Set<T>::toVector() const {
std::vector<T> v;
v.insert(v.begin(), set.begin(), set.end());
return v;
}
}//End Moversight
}//End uBeeMe
#endif /* SET_H */