536 lines
14 KiB
C++
536 lines
14 KiB
C++
// -*- C++ -*-
|
|
/*
|
|
* File: List.h
|
|
* Author: jgaebler
|
|
*
|
|
* Created on August 18, 2010, 8:43 AM
|
|
*/
|
|
#pragma once
|
|
|
|
#ifndef LIST_H
|
|
#define LIST_H
|
|
|
|
#include "common/Defines.h"
|
|
#include "common/Exception.h"
|
|
|
|
#include <type_traits>
|
|
|
|
namespace ubeeme {
|
|
namespace moversight {
|
|
|
|
/**
|
|
* @class List
|
|
* @brief Provides a list for Moversight objects
|
|
* @ingroup Moversight
|
|
* @author Jan Gäbler
|
|
*/
|
|
template <typename T>
|
|
class List {
|
|
public:
|
|
|
|
List();
|
|
List(const List & orig);
|
|
virtual ~List();
|
|
|
|
List & add(const T & t);
|
|
List & add(const List<T> & tList);
|
|
List & add(const T* tArray, size_t laenge);
|
|
List & add(const std::vector<T> t);
|
|
|
|
void clear();
|
|
|
|
size_t size() const;
|
|
|
|
bool contains(const T & a) const;
|
|
|
|
void remove(const T & a);
|
|
void remove(T & a);
|
|
void remove(const List<T> & tList);
|
|
|
|
List<T> & unify(List<T> & tList);
|
|
|
|
T & get(size_t index);
|
|
T const & get(size_t index) const;
|
|
|
|
void sort();
|
|
|
|
void pop();
|
|
T & first();
|
|
|
|
bool operator==(const List<T> & other) const;
|
|
|
|
std::vector<T> & getInternal();
|
|
const std::vector<T> & getInternal() const;
|
|
|
|
T & getMinElem();
|
|
|
|
protected:
|
|
|
|
typedef std::vector<T> TList;
|
|
TList queue;
|
|
|
|
public:
|
|
|
|
template <typename L>
|
|
class Iterator : public std::iterator<std::forward_iterator_tag, L> {
|
|
|
|
L * pos;
|
|
|
|
friend class List<T>;
|
|
|
|
explicit Iterator(L * aPos);
|
|
|
|
public:
|
|
|
|
Iterator(Iterator<typename std::remove_const<L>::type > const & other);
|
|
|
|
L & operator*();
|
|
|
|
Iterator<L> & operator++(); // prefix
|
|
Iterator<L> operator++(int); //postfix
|
|
|
|
bool operator !=(const Iterator<L> & other) const;
|
|
bool operator ==(const Iterator<L> & other) const;
|
|
|
|
};
|
|
|
|
typedef Iterator<T> iterator;
|
|
typedef Iterator<const T> const_iterator;
|
|
|
|
iterator begin();
|
|
const_iterator begin() const;
|
|
|
|
iterator end();
|
|
const_iterator end() const;
|
|
|
|
};
|
|
|
|
/**
|
|
* @brief Creates a new iterator.
|
|
* @param aPos A pointer to the start index of the iterator
|
|
*/
|
|
template <typename T>
|
|
template <typename L>
|
|
List<T>::Iterator<L>::Iterator(L * aPos) : pos(aPos) {
|
|
}
|
|
|
|
/**
|
|
* @brief Copy constructor
|
|
* @param other The instance to copy
|
|
*/
|
|
template <typename T>
|
|
template <typename L>
|
|
List<T>::Iterator<L>::Iterator(Iterator< typename std::remove_const<L>::type > const & other) : pos(other.pos) {
|
|
}
|
|
|
|
/**
|
|
* @brief De-reference operator
|
|
* @return The instance to which the iterator points currently.
|
|
*/
|
|
template <typename T>
|
|
template <typename L>
|
|
L &
|
|
List<T>::Iterator<L>::operator*() {
|
|
return * pos;
|
|
}
|
|
|
|
/**
|
|
* @brief Increment operator
|
|
* @return A reference to the local instance.
|
|
*/
|
|
template <typename T>
|
|
template <typename L>
|
|
typename List<T>::template Iterator<L> &
|
|
List<T>::Iterator<L>::operator++() { // prefix
|
|
++pos;
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* @brief Increment operator in postfix notation.
|
|
* @param Pseudoparameter
|
|
* @return A copy of the instance before the increment was applied.
|
|
*/
|
|
template <typename T>
|
|
template <typename L>
|
|
typename List<T>::template Iterator<L>
|
|
List<T>::Iterator<L>::operator++(int) { //postfix
|
|
Iterator<L> tmp = *this;
|
|
++(*this);
|
|
return tmp;
|
|
}
|
|
|
|
/**
|
|
* @brief Not equal operator
|
|
* @param other The instance to compare
|
|
* @return True, if the given operator not equal to the local one, false otherwise.
|
|
*/
|
|
template <typename T>
|
|
template <typename L>
|
|
bool
|
|
List<T>::Iterator<L>::operator !=(const List<T>::Iterator<L> & other) const {
|
|
return pos != other.pos;
|
|
}
|
|
|
|
/**
|
|
* @brief Equal operator
|
|
* @param other The instance to compare
|
|
* @return True, if the given operator equal to the local one, false otherwise.
|
|
*/
|
|
template <typename T>
|
|
template <typename L>
|
|
bool
|
|
List<T>::Iterator<L>::operator ==(const List<T>::Iterator<L> & other) const {
|
|
return pos == other.pos;
|
|
}
|
|
|
|
/**
|
|
* @brief Return iterator to beginning
|
|
* Returns an iterator pointing to the first element in the list.
|
|
* @return An iterator to the beginning of the list.
|
|
*/
|
|
template <typename T>
|
|
typename List<T>::iterator
|
|
List<T>::begin() {
|
|
return iterator(&queue[0]);
|
|
}
|
|
|
|
/**
|
|
* @brief Return iterator to end
|
|
* Returns an iterator referring to the past-the-end element in the list.
|
|
* @return An iterator to the element past the end of the sequence.
|
|
*/
|
|
template <typename T>
|
|
typename List<T>::iterator
|
|
List<T>::end() {
|
|
return iterator(&queue[queue.size()]);
|
|
}
|
|
|
|
/**
|
|
* @brief Return const iterator to beginning
|
|
* Returns an const iterator pointing to the first element in the list.
|
|
* @return An const iterator to the beginning of the list.
|
|
*/
|
|
template <typename T>
|
|
typename List<T>::const_iterator
|
|
List<T>::begin() const {
|
|
return iterator(const_cast<T*>(&queue[0]));
|
|
}
|
|
|
|
/**
|
|
* @brief Return const iterator to end
|
|
* Returns an const iterator referring to the past-the-end element in the list.
|
|
* @return An const iterator to the element past the end of the sequence.
|
|
*/
|
|
template <typename T>
|
|
typename List<T>::const_iterator
|
|
List<T>::end() const {
|
|
return iterator(const_cast<T*>(&queue[queue.size()]));
|
|
}
|
|
|
|
/**
|
|
* @brief Output operator.
|
|
* @param out The output stream.
|
|
* @param l The list.
|
|
* @return The modified output stream.
|
|
*/
|
|
template <typename T>
|
|
std::ostream&
|
|
operator<<(std::ostream & out, const List<T> & l) {
|
|
std::stringstream buf;
|
|
buf << "[";
|
|
|
|
for (auto e = l.begin(); e != l.end(); e++) {
|
|
buf << (*e);
|
|
}
|
|
buf << "]";
|
|
return out << buf.str();
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor
|
|
*/
|
|
template <typename T>
|
|
List<T>::List() {
|
|
}
|
|
|
|
/**
|
|
* @brief Copy constructor
|
|
* @param orig the list to copy
|
|
*/
|
|
template <typename T>
|
|
List<T>::List(const List & orig) : queue(orig.queue) {
|
|
}
|
|
|
|
/**
|
|
* @brief Destructor
|
|
*/
|
|
template <typename T>
|
|
List<T>::~List() {
|
|
clear();
|
|
}
|
|
|
|
/**
|
|
* @brief Clears the list
|
|
*/
|
|
template <typename T>
|
|
void
|
|
List<T>::clear() {
|
|
queue.clear();
|
|
}
|
|
|
|
/**
|
|
* @brief Adds a object to the list.
|
|
* @param t the object to add
|
|
* @return This list.
|
|
*/
|
|
template <typename T>
|
|
List<T> &
|
|
List<T>::add(const T & t) {
|
|
|
|
queue.push_back(t);
|
|
return *this;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Adds a object list to the list.
|
|
* @param tList the list to add
|
|
* @return This list.
|
|
*/
|
|
template <typename T>
|
|
List<T> &
|
|
List<T>::add(const List<T> & tList) {
|
|
|
|
queue.insert(queue.end(), tList.begin(), tList.end());
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
*@brief Adds a std:vector of Ts to the list
|
|
*@param t The std:vector to add.
|
|
*@return This list.
|
|
*/
|
|
template <typename T>
|
|
List<T> &
|
|
List<T>::add(const std::vector<T> t) {
|
|
|
|
queue.insert(queue.end(), t.begin(), t.end());
|
|
return *this;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Adds an array to the list.
|
|
* @param tArray - the array to add
|
|
* @param length - length of the array
|
|
* @return This list.
|
|
*/
|
|
template <typename T>
|
|
List<T> &
|
|
List<T>::add(const T* tArray, size_t length) {
|
|
|
|
//queue.insert(queue.begin(), std::begin(tArray), std::end(tArray)); //C++11
|
|
queue.insert(tArray, tArray + sizeof(tArray)/sizeof(tArray[0]));
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* @brief Unifies the current list with the given list. The resulting list is sorted and contains no duplicated objects.
|
|
* @param tList the list to unify
|
|
* @return This resulting list, sorted and without duplicates.
|
|
*/
|
|
template <typename T>
|
|
List<T> &
|
|
List<T>::unify(List<T> & tList) {
|
|
|
|
add(tList);
|
|
sort();
|
|
|
|
typename TList::iterator it;
|
|
it = std::unique(queue.begin(), queue.end());
|
|
queue.resize(std::distance(queue.begin(), it));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Determines the size of the list
|
|
* @return the list size
|
|
*/
|
|
template <typename T>
|
|
size_t
|
|
List<T>::size() const {
|
|
|
|
return queue.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
|
|
List<T>::contains(const T & a) const {
|
|
|
|
return std::find(queue.begin(), queue.end(), a) != queue.end();
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the object from the list, which is located at the given index.
|
|
* @throws IndexOutOfBounceException if the given index larger than the current size of the list
|
|
* @param index the index of the element to return
|
|
*/
|
|
template <typename T>
|
|
T &
|
|
List<T>::get(size_t index) {
|
|
|
|
if (index > queue.size()) {
|
|
throw IndexOutOfBounceException("index out of bounce");
|
|
}//End if
|
|
|
|
return queue[index];
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the object from the list, which is located at the given index.
|
|
* @throws IndexOutOfBounceException if the given index larger than the current size of the list
|
|
* @param index the index of the element to return
|
|
*/
|
|
template <typename T>
|
|
T const &
|
|
List<T>::get(size_t index) const {
|
|
|
|
if (index > queue.size()) {
|
|
throw IndexOutOfBounceException("index out of bounce");
|
|
}//End if
|
|
|
|
return queue[index];
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Removes a given object from the list.
|
|
* @param a the object to remove
|
|
*/
|
|
template <typename T>
|
|
void
|
|
List<T>::remove(const T & a) {
|
|
|
|
T b = a;
|
|
remove(b);
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Removes a given object from the list.
|
|
* @param a the object to remove
|
|
*/
|
|
template <typename T>
|
|
void
|
|
List<T>::remove(T & a) {
|
|
|
|
queue.erase(std::remove(queue.begin(),
|
|
queue.end(),
|
|
a),
|
|
queue.end());
|
|
}
|
|
|
|
/**
|
|
* @brief Removes a list of objects from the list.
|
|
* @param tList the list of objects to remove
|
|
*/
|
|
template <typename T>
|
|
void
|
|
List<T>::remove(const List<T> & tList) {
|
|
|
|
for (size_t i = 0; i < tList.size(); i++) {
|
|
remove(tList.get(i));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Sorts the list
|
|
*/
|
|
template <typename T>
|
|
void
|
|
List<T>::sort() {
|
|
|
|
std::sort(queue.begin(), queue.end());
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Compares the local list with a foreign list, by sorting and
|
|
* comparing the both list afterwards. The method uses a local copy of
|
|
* each list for sorting and comparing. The source lists remain
|
|
* unmodified.
|
|
* @param other The list to compare.
|
|
* @return True, if the list pairwise the same, false otherwise.
|
|
*/
|
|
template <typename T>
|
|
bool
|
|
List<T>::operator ==(const List<T>& other) const {
|
|
|
|
TList localQueue(queue);
|
|
TList foreignQueue(other.queue);
|
|
|
|
std::sort(localQueue.begin(), localQueue.end());
|
|
std::sort(foreignQueue.begin(), foreignQueue.end());
|
|
|
|
return (localQueue == foreignQueue);
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Remove the first element of the list.
|
|
*/
|
|
template <typename T>
|
|
void
|
|
List<T>::pop() {
|
|
queue.erase(queue.begin());
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the first element of the queue.
|
|
* @return The first element of the queue.
|
|
*/
|
|
template <typename T>
|
|
T &
|
|
List<T>::first() {
|
|
return queue.front();
|
|
}
|
|
|
|
template <typename T>
|
|
std::vector<T>&
|
|
List<T>::getInternal() {
|
|
return queue;
|
|
}
|
|
|
|
template <typename T>
|
|
const std::vector<T>&
|
|
List<T>::getInternal() const {
|
|
return queue;
|
|
}
|
|
|
|
/**
|
|
* @brief Provides access to the minimal element within the list
|
|
* @return The minimal element of the list
|
|
*/
|
|
template <typename T>
|
|
T &
|
|
List<T>::getMinElem() {
|
|
return * std::min_element(queue.begin(), queue.end());
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#endif /* LIST_H */
|
|
|