diff --git a/src/core/ptrvector/Algorithm.h b/src/core/ptrvector/Algorithm.h deleted file mode 100644 index c00063d702e55c804b1b02ceadf44845a1ff1069..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/Algorithm.h +++ /dev/null @@ -1,95 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file Algorithm.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - - -//************************************************************************************************* -// Includes -//************************************************************************************************* - -#include <boost/type_traits/is_base_of.hpp> - -namespace walberla { - -//================================================================================================= -// -// POLYMORPHIC COUNT -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Counts the pointer to objects with dynamic type \a D. - * - * \param first Iterator to the first pointer of the pointer range. - * \param last Iterator to the pointer one past the last pointer of the pointer range. - * \return The number of objects with dynamic type \a D. - * - * This function traverses the range \f$ [first,last) \f$ of pointers to objects with static - * type \a S and counts all polymorphic pointers to objects of dynamic type \a D. Note that - * in case \a D is not a type derived from \a S, a compile time error is created! - */ -template< typename D // Dynamic type of the objects - , typename S > // Static type of the objects -inline size_t polymorphicCount( S *const * first, S *const * last ) -{ - static_assert(boost::is_base_of<S, D>::value && !boost::is_base_of<D, S>::value, "D has to be strictly derived from S"); - - size_t count( 0 ); - for( S *const * it=first; it!=last; ++it ) - if( dynamic_cast<D*>( *it ) ) ++count; - return count; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// POLYMORPHIC FIND -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Finds the next pointer to an object with dynamic type \a D. - * - * \param first Iterator to the first pointer of the pointer range. - * \param last Iterator to the pointer one past the last pointer of the pointer range. - * \return The next pointer to an object with dynamic type \a D. - * - * This function traverses the range \f$ [first,last) \f$ of pointers to objects with static - * type \a S until it finds the next polymorphic pointer to an object of dynamic type \a D. - * Note that in case \a D is not a type derived from \a S, a compile time error is created! - */ -template< typename D // Dynamic type of the objects - , typename S > // Static type of the objects -inline S *const * polymorphicFind( S *const * first, S *const * last ) -{ - static_assert(boost::is_base_of<S, D>::value && !boost::is_base_of<D, S>::value, "D has to be strictly derived from S"); - - while( first != last && !dynamic_cast<D*>( *first ) ) ++first; - return first; -} -//************************************************************************************************* - -} // namespace diff --git a/src/core/ptrvector/Null.h b/src/core/ptrvector/Null.h deleted file mode 100644 index 0f08813a0244eacfe1f82e9f9249308e924c932c..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/Null.h +++ /dev/null @@ -1,289 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file Null.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Safe C++ NULL pointer implementation. - * \ingroup util - * - * This implementation offers a remedy for the use of the NULL pointer in C++. For this, the - * NULL macro is replaced by an instance of the Null class, which can only be assigned and - * compared with pointers and pointers-to-member. Therefore the use of NULL regains the type - * safety it lost in C++ due to the strict C++ type system.\n - * The NULL pointer is used exactly as before: - - \code - int* pi = NULL; - if( pi == NULL ) {...} - \endcode - */ -class Null -{ -public: - //**Constructor********************************************************************************* - /*!\name Constructor */ - //@{ - inline Null(); - //@} - //********************************************************************************************** - - //**Destructor********************************************************************************** - // No explicitly declared destructor. - //********************************************************************************************** - - //**Conversion operators************************************************************************ - /*!\name Conversion operators */ - //@{ - template< typename T > - inline operator T*() const; - - template< typename T, typename C > - inline operator T C::*() const; - //@} - //********************************************************************************************** - - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - template< typename T > - inline bool equal( const T* rhs ) const; - - template< typename T, typename C > - inline bool equal( const T C::* rhs ) const; - //@} - //********************************************************************************************** - -private: - //**Forbidden operations************************************************************************ - /*!\name Forbidden operations */ - //@{ - Null( const Null& n ); //!< Copy constructor (private & undefined) - Null& operator=( const Null& n ); //!< Copy assignment operator (private & undefined) - void* operator&() const; //!< Address operator (private & undefined) - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// CONSTRUCTOR -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief The default constructor of the Null class. - */ -inline Null::Null() -{} -//************************************************************************************************* - - - - -//================================================================================================= -// -// CONVERSION OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Conversion operator to a pointer. - * - * This conversion operator offers a type safe conversion of zero to a pointer of any kind. - */ -template< typename T > -inline Null::operator T*() const -{ - return 0; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Conversion operator to a pointer-to-member. - * - * This conversion operator offers the type safe conversion of zero to a pointer-to-member of - * any kind. - */ -template< typename T, typename C > -inline Null::operator T C::*() const -{ - return 0; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Comparison between Null and a pointer. - * - * The function offers a type safe comparison between zero and an arbitrary pointer. - */ -template< typename T > -inline bool Null::equal( const T* rhs ) const -{ - return rhs == 0; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Comparison between Null and a pointer-to-member. - * - * The function offers a type safe comparison between zero and an arbitrary pointer-to-member. - */ -template< typename T, typename C > -inline bool Null::equal( const T C::* rhs ) const -{ - return rhs == 0; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// GLOBAL OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\name Null operators */ -//@{ -template< typename T > -inline bool operator==( const Null& lhs, const T& rhs ); - -template< typename T > -inline bool operator==( const T& lhs, const Null& rhs ); - -template< typename T > -inline bool operator!=( const Null& lhs, const T& rhs ); - -template< typename T > -inline bool operator!=( const T& lhs, const Null& rhs ); -//@} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Equality comparison between Null and a pointer or pointer-to-member. - * - * This operator takes a reference to an object of type T instead of a pointer of a pointer- - * to-member to avoid the ambiguity with the built-in pointer comparison operators. However, - * only pointers and pointers-to-member can be compared to Null. - */ -template< typename T > -inline bool operator==( const Null& lhs, const T& rhs ) -{ - return lhs.equal( rhs ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Equality comparison between a pointer or pointer-to-member and Null. - * - * This operator takes a reference to an object of type T instead of a pointer of a pointer- - * to-member to avoid the ambiguity with the built-in pointer comparison operators. However, - * only pointers and pointers-to-member can be compared to Null. - */ -template< typename T > -inline bool operator==( const T& lhs, const Null& rhs ) -{ - return rhs.equal( lhs ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Inequality comparison between Null and a pointer or pointer-to-member. - * - * This operator takes a reference to an object of type T instead of a pointer of a pointer- - * to-member to avoid the ambiguity with the built-in pointer comparison operators. However, - * only pointers and pointers-to-member can be compared to Null. - */ -template< typename T > -inline bool operator!=( const Null& lhs, const T& rhs ) -{ - return !lhs.equal( rhs ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Inequality comparison between a pointer or pointer-to-member and Null. - * - * This operator takes a reference to an object of type T instead of a pointer of a pointer- - * to-member to avoid the ambiguity with the built-in pointer comparison operators. However, - * only pointers and pointers-to-member can be compared to Null. - */ -template< typename T > -inline bool operator!=( const T& lhs, const Null& rhs ) -{ - return !rhs.equal( lhs ); -} -//************************************************************************************************* - -} // namespace - - - - -////================================================================================================= -//// -//// NULL DEFINITION -//// -////================================================================================================= - -//#ifdef NULL -//# undef NULL -//#endif - -////************************************************************************************************* -///*!\brief Global NULL pointer. -// * \ingroup util -// * -// * This instance of the Null class replaces the NULL macro to ensure a type-safe NULL pointer. -// */ -//const walberla::Null NULL; -////************************************************************************************************* diff --git a/src/core/ptrvector/PtrIterator.h b/src/core/ptrvector/PtrIterator.h deleted file mode 100644 index a6ecef7f3b6194ead74e78f2b52e030dd3c9b704..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/PtrIterator.h +++ /dev/null @@ -1,548 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file PtrIterator.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - - -//************************************************************************************************* -// Includes -//************************************************************************************************* - -#include <iterator> -#include <core/ptrvector/Null.h> - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Implementation of an iterator for pointer vectors. - * \ingroup util - * - * The PtrIterator class follows the example of the random-access iterator classes of the STL. - * However, the focus of this iterator implementation is the use with (polymorphic) pointers. - * Since the physics engine makes heavy use of polymorphic pointers, this implementation eases - * the use of iterators over a range of pointers and improves the semantics on these pointers.\n - * - * In contrast to the STL iterators, the PtrIterator class slightly changes the meaning of the - * access operators. Consider the following example: - - \code - // Definition of class A - class A - { - public: - A( int i=0 ):i_(i) {} - - void set( int i ) { i_ = i; } - int get() const { return i_; } - - private: - int i_; - }; - - // Definition of a pointer vector for class A - typedef PtrVector<A> AVector; - - AVector vector; - AVector::Iterator it = vector.begin(); - - // The subscript operator returns a handle to the underlying object - A* a1 = it[0]; - - // The dereference operator returns a handle to the underlying object - A* a2 = *it; - - // The member access operator offers direct access to the underlying object - it->set( 2 ); - \endcode - - * The constant iterators (iterator over constant objects) prohibit the access to non-const - * member functions. Therefore the following operation results in a compile-time error: - - \code - AVector vector; - AVector::ConstIterator it = vector.begin(); - - it->set( 2 ); // Compile-time error! - \endcode - */ -template< typename Type > -class PtrIterator -{ -public: - //**Type definitions**************************************************************************** - // pe naming convention - typedef std::random_access_iterator_tag IteratorCategory; //!< The iterator category. - typedef Type* ValueType; //!< Type of the underlying pointers. - typedef Type* const PointerType; //!< Pointer return type. - typedef ValueType const& ReferenceType; //!< Reference return type. - typedef ValueType const* IteratorType; //!< Type of the internal pointer. - typedef std::ptrdiff_t DifferenceType; //!< Difference between two iterators. - - // STL iterator requirements - typedef IteratorCategory iterator_category; //!< The iterator category. - typedef ValueType value_type; //!< Type of the underlying pointers. - typedef PointerType pointer; //!< Pointer return type. - typedef ReferenceType reference; //!< Reference return type. - typedef DifferenceType difference_type; //!< Difference between two iterators. - //********************************************************************************************** - - //**Constructors******************************************************************************** - /*!\name Constructors */ - //@{ - inline PtrIterator(); - explicit inline PtrIterator( const IteratorType& it ); - - template< typename Other > - inline PtrIterator( const PtrIterator<Other>& it ); - - // No explicitly declared copy constructor. - //@} - //********************************************************************************************** - - //**Destructor********************************************************************************** - // No explicitly declared destructor. - //********************************************************************************************** - - //**Copy assignment operator******************************************************************** - // No explicitly declared copy assignment operator. - //********************************************************************************************** - - //**Operators*********************************************************************************** - /*!\name Operators */ - //@{ - inline PtrIterator& operator++(); - inline PtrIterator operator++( int ); - inline PtrIterator& operator--(); - inline PtrIterator operator--( int ); - inline PtrIterator& operator+=( DifferenceType n ); - inline PtrIterator operator+ ( DifferenceType n ) const; - inline PtrIterator& operator-=( DifferenceType n ); - inline PtrIterator operator- ( DifferenceType n ) const; - inline DifferenceType operator- ( const PtrIterator& it ) const; - //@} - //********************************************************************************************** - - //**Access operators**************************************************************************** - /*!\name Access operators */ - //@{ - inline PointerType& operator[]( DifferenceType n ) const; - inline PointerType& operator*() const; - inline PointerType& operator->() const; - //@} - //********************************************************************************************** - - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - inline const IteratorType& base() const; - //@} - //********************************************************************************************** - -private: - //**Member variables**************************************************************************** - /*!\name Member variables */ - //@{ - IteratorType it_; //!< Pointer to the current memory location. - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// CONSTRUCTORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Default constructor for PtrIterator. - */ -template< typename Type > -inline PtrIterator<Type>::PtrIterator() - : it_(NULL) // Pointer to the current memory location -{} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Standard constructor for PtrIterator. - * - * \param it The value of the iterator. - */ -template< typename Type > -inline PtrIterator<Type>::PtrIterator( const IteratorType& it ) - : it_(it) // Pointer to the current memory location -{} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Conversion constructor from different PtrIterator instances. - * - * \param it The foreign PtrIterator instance to be copied. - */ -template< typename Type > -template< typename Other > -inline PtrIterator<Type>::PtrIterator( const PtrIterator<Other>& it ) - : it_( it.base() ) // Pointer to the current memory location -{} -//************************************************************************************************* - - - - -//================================================================================================= -// -// OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Pre-increment operator. - * - * \return Reference to the incremented pointer iterator. - */ -template< typename Type > -inline PtrIterator<Type>& PtrIterator<Type>::operator++() -{ - ++it_; - return *this; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Post-increment operator. - * - * \return The incremented pointer iterator. - */ -template< typename Type > -inline PtrIterator<Type> PtrIterator<Type>::operator++( int ) -{ - PtrIterator tmp( *this ); - ++it_; - return tmp; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Pre-decrement operator. - * - * \return Reference to the decremented pointer iterator. - */ -template< typename Type > -inline PtrIterator<Type>& PtrIterator<Type>::operator--() -{ - --it_; - return *this; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Post-decrement operator. - * - * \return The decremented pointer iterator. - */ -template< typename Type > -inline PtrIterator<Type> PtrIterator<Type>::operator--( int ) -{ - PtrIterator tmp( *this ); - --it_; - return tmp; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Shifting the iterator by \a n elements to the higher elements. - * - * \param n The number of elements. - * \return Reference to the shifted pointer iterator. - */ -template< typename Type > -inline PtrIterator<Type>& PtrIterator<Type>::operator+=( DifferenceType n ) -{ - it_ += n; - return *this; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Shifting the iterator by \a n elements to the higher elements. - * - * \param n The number of elements. - * \return The shifted pointer iterator. - */ -template< typename Type > -inline PtrIterator<Type> PtrIterator<Type>::operator+( DifferenceType n ) const -{ - return PtrIterator( it_ + n ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Shifting the iterator by \a n elements to the lower elements. - * - * \param n The number of elements. - * \return Reference to the shifted pointer iterator. - */ -template< typename Type > -inline PtrIterator<Type>& PtrIterator<Type>::operator-=( DifferenceType n ) -{ - it_ -= n; - return *this; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Shifting the iterator by \a n elements to the lower elements. - * - * \param n The number of elements. - * \return The shifted pointer iterator. - */ -template< typename Type > -inline PtrIterator<Type> PtrIterator<Type>::operator-( DifferenceType n ) const -{ - return PtrIterator( it_ - n ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Calculating the number of elements between two pointer iterators. - * - * \param it The right hand side iterator. - * \return The number of elements between the two pointer iterators. - */ -template< typename Type > -inline typename PtrIterator<Type>::DifferenceType PtrIterator<Type>::operator-( const PtrIterator& it ) const -{ - return it_ - it.it_; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// ACCESS OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Subscript operator for the direct element access. - * - * \param index Access index. Accesses the element \a index elements away from the current iterator position. - * \return Handle to the accessed element. - */ -template< typename Type > -inline typename PtrIterator<Type>::PointerType& PtrIterator<Type>::operator[]( DifferenceType index ) const -{ - return it_[index]; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns a handle to the element at the current iterator position. - * - * \return Handle to the element at the current iterator position. - */ -template< typename Type > -inline typename PtrIterator<Type>::PointerType& PtrIterator<Type>::operator*() const -{ - return *it_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Direct access to the element at the current iterator position. - * - * \return Reference to the element at the current iterator position. - */ -template< typename Type > -inline typename PtrIterator<Type>::PointerType& PtrIterator<Type>::operator->() const -{ - return *it_; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Access to the underlying member of the pointer iterator. - * - * \return Pointer to the current memory location. - */ -template< typename Type > -inline const typename PtrIterator<Type>::IteratorType& PtrIterator<Type>::base() const -{ - return it_; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// GLOBAL OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\name PtrIterator operators */ -//@{ -template< typename TypeL, typename TypeR > -inline bool operator==( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ); - -template< typename TypeL, typename TypeR > -inline bool operator!=( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ); - -template< typename TypeL, typename TypeR > -inline bool operator<( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ); - -template< typename TypeL, typename TypeR > -inline bool operator>( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ); - -template< typename TypeL, typename TypeR > -inline bool operator<=( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ); - -template< typename TypeL, typename TypeR > -inline bool operator>=( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ); -//@} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Equality comparison between two PtrIterator objects. - * - * \param lhs The left-hand side pointer iterator. - * \param rhs The right-hand side pointer iterator. - * \return \a true if the iterators point to the same element, \a false if not. - */ -template< typename TypeL, typename TypeR > -inline bool operator==( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ) -{ - return lhs.base() == rhs.base(); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Inequality comparison between two PtrIterator objects. - * - * \param lhs The left-hand side pointer iterator. - * \param rhs The right-hand side pointer iterator. - * \return \a true if the iterators don't point to the same element, \a false if they do. - */ -template< typename TypeL, typename TypeR > -inline bool operator!=( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ) -{ - return lhs.base() != rhs.base(); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Less-than comparison between two PtrIterator objects. - * - * \param lhs The left-hand side pointer iterator. - * \param rhs The right-hand side pointer iterator. - * \return \a true if the left-hand side iterator points to a lower element, \a false if not. - */ -template< typename TypeL, typename TypeR > -inline bool operator<( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ) -{ - return lhs.base() < rhs.base(); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Greater-than comparison between two PtrIterator objects. - * - * \param lhs The left-hand side pointer iterator. - * \param rhs The right-hand side pointer iterator. - * \return \a true if the left-hand side iterator points to a higher element, \a false if not. - */ -template< typename TypeL, typename TypeR > -inline bool operator>( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ) -{ - return lhs.base() > rhs.base(); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Less-or-equal-than comparison between two PtrIterator objects. - * - * \param lhs The left-hand side pointer iterator. - * \param rhs The right-hand side pointer iterator. - * \return \a true if the left-hand side iterator points to a lower or the same element, \a false if not. - */ -template< typename TypeL, typename TypeR > -inline bool operator<=( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ) -{ - return lhs.base() <= rhs.base(); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Greater-or-equal-than comparison between two PtrIterator objects. - * - * \param lhs The left-hand side pointer iterator. - * \param rhs The right-hand side pointer iterator. - * \return \a true if the left-hand side iterator points to a higher or the same element, \a false if not. - */ -template< typename TypeL, typename TypeR > -inline bool operator>=( const PtrIterator<TypeL>& lhs, const PtrIterator<TypeR>& rhs ) -{ - return lhs.base() >= rhs.base(); -} -//************************************************************************************************* - -} // namespace diff --git a/src/core/ptrvector/PtrVector.h b/src/core/ptrvector/PtrVector.h deleted file mode 100644 index 2e62290407295ed080e6b619f7242e709ee952fe..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/PtrVector.h +++ /dev/null @@ -1,2604 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file PtrVector.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - - -//************************************************************************************************* -// Includes -//************************************************************************************************* - -#include <algorithm> -#include <stdexcept> -#include <core/debug/Debug.h> -#include <core/ptrvector/Algorithm.h> -#include <core/ptrvector/Null.h> -#include <core/ptrvector/policies/PtrDelete.h> -#include <core/ptrvector/policies/OptimalGrowth.h> -#include <core/ptrvector/PtrIterator.h> -#include <core/Template.h> - -#include <boost/type_traits/is_base_of.hpp> -#include <boost/type_traits/is_convertible.hpp> - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Implementation of a vector for (polymorphic) pointers. - * \ingroup util - * - * \section basics Basic usage - * - * The \a std::vector is one of the standard libraries most useful tools. It is the standard - * solution for a dynamically allocated, automatically growing, and memory managed array. It - * provides fast random access to its elements, since a vector guarantees that the elements - * lie adjacent in memory and manages the dynamically allocated memory according to the RAII - * idiom.\n - * Yet there are some situations, where users of \a std::vector experience several drawbacks, - * especially when \a std::vector is used in combination with pointers. For instance, a - * \a const_iterator over a range of pointers will not allow the stored pointers to change, - * but the objects behind the pointers remain changeable. The following example illustrates - * that it is possible to change the values of \a double values through an iterator-to-const: - - \code - typedef std::vector<double*> Doubles; - - Doubles doubles; // Creating a vector for pointers to double values - - // Filling the vector with pointers to double values. All values are initialized with 1. - for( size_t i=0; i<10; ++i ) - doubles.push_back( new double( 1.0 ) ); - - // Accessing the first rigid body - Doubles::const_iterator first = doubles.begin(); - **first = 2.0; // Changes the double value through an iterator-to-const - \endcode - - * The basic reason for this behavior is that \a std::vector is unaware of the fact that it - * stores pointers instead of objects and therefore the pointer are considered constant, not - * the objects behind the pointer.\n - * Another drawback of \a std::vector is the fact that during destruction of a vector object - * the dynamically allocated bodies are not deleted. Again, \a std::vector is unaware of the - * special property of pointers and therefore does not apply any kind of deletion policy. It - * basically calls the default destructor for pointers, which in turn does nothing and - * especially does not destroy the attached objects.\n - * A different approach is taken by the Boost \a ptr_vector. A \a ptr_vector is perfectly - * aware of the fact that is stores pointers to dynamically objects (and in consequence may - * only be used with pointers to dynamically allocated objects) and takes full responsibility - * for these resources. However, in order to accomplish this task, \a ptr_vector completely - * abstracts from the fact that it stores pointers and provides a view as if it would contain - * objects instead of pointers. However, unfortunately this strict memory management creates - * problems in the context of \b walberla, where vectors to pointers are used both internally - * (including proper resource management) and outside by the user (without any resource - * management).\n - * \b walberla provides a special vector container for pointers, which is - * a cross of the functionalities of the \a std::vector and \a ptr_vector. The \b walberla PtrVector - * is not a RAII class in the classical sense (as for instance the Boost \a ptr_vector) since - * it does not strictly encapsule the resource management. As in the case of \a std::vector, - * it still is the responsibility of a user of PtrVector to manage the resources accordingly. - * However, it perfectly fits the requirements of \b walberla: PtrVector can be used internally - * to store pointers to dynamically allocated objects and resources within RAII classes, and - * outside of \b walberla by a user as storage for handles to resources that are managed elsewhere. - * In contrast to the \a boost::ptr_vector, the PtrVector provides full access to the contained - * pointers, but its iterators work similar to the \a ptr_vector - * iterator and only provide access to the objects behind the pointers, creating the illusion - * that objects are stored instead of pointers: - - \code - typedef walberla::PtrVector<double> Doubles; - Doubles doubles; // Creating an empty PtrVector for pointers to double values - - doubles.pushBack( new double(1.0) ); // A new pointer-to-double is added to the vector - - double_vector::iterator first = doubles.begin(); - *first = 2.0; // No indirection needed - - Doubles::ConstIterator second( first+1 ); - *second = 3.0; // Compile time error! It is not possible to change double - // values via an iterator-to-const - \endcode - - * Notice the differences in the usage of the iterator in contrast to the \a std::vector and - * \a boost::ptr_vector. In contrast to them the functions of PtrVector follow the naming - * convention of the \b walberla physics engine (i.e. pushBack instead of push_back). In addition, - * the underlying iterator adds an additional dereference to all access operators, which eases - * the access to the underlying objects: - - \code - // STL style: - **first = 2.0; - - // walberla style: - *first = 2.0; - \endcode - - * A noteworthy difference between the STL vector and the pointer vector is the used template - * argument: instead of the pointer type, the \b walberla pointer vector is only provided with the - * type of the underlying objects: - - \code - // STL style: - std::vector<double*> vector; - - // walberla style: - walberla::PtrVector<double> vector; - \endcode - - * Additionally, the \b walberla pointer vector offers some limited possibilities to configure the - * memory management and the growth of the internal storage, and implements special features - * for polymorphic pointers, as for instance a convenient way to iterate over a subset of - * polymorphic objects contained in the pointer vector.\n\n - * - * - * \section polymorphic Polymorphic pointers - * - * For polymorphic pointers, the PtrVector class additionally offers two special iterators to - * iterate over all objects of a specific type: the CastIterator and ConstCastIterator. - - \code - // Definition of class A and the derived type B - class A { ... }; - class B : public A { ... }; - - // Definition of function f for non-const pointer vectors - void f( PtrVector<A>& vector ) - { - PtrVector<A>::CastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::CastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - ... - } - - // Definition of function f for const pointer vectors - void f( const PtrVector<A>& vector ) - { - PtrVector<A>::ConstCastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::ConstCastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - } - \endcode - - * In the example, the cast iterators are used to iterate over all objects of type \a B within - * the pointer vector, where \a B must be a type derived from \a A. The attempt to use these - * iterators for types that are not derived from \a A results in a compile time error. Note that - * the usage of the cast iterators is computationally more expensive than the use of the standard - * iterators. Therefore these iterators should not be used unless a down-cast is really necessary, - * e.g. in order to access a type specific function.\n\n - * - * - * \section container Using a pointer vector within other container classes - * - * If a pointer vector is used within an other container and is used to store polymorphic pointers, - * you might face the problem of not being able to create type definitions for the cast iterators. - * Whereas it is possible to create typedefs for the standard iterators, it is unfortunately not - * possible (yet) to create type definitions for template classes. In order to create a new return - * type within the container, the following approach could be taken: - - \code - template< typename A > - class Container - { - public: - template< typename C > - struct CastIterator : public PtrVector<A>::CastIterator<C> - { - CastIterator( const PtrVector<A>::CastIterator<C>& it ) - : PtrVector<A>::CastIterator<C>( it ) // Initializing the base class - {} - }; - - template< typename C > - CastIterator<C> begin(); - - template< typename C > - CastIterator<C> end(); - - private: - PtrVector<A> vector_; - }; - \endcode - - * Instead of a typedef within the Container class, a new class CastIterator is derived from the - * PtrVector::CastIterator class. This approach acts similar as the typedef as a user can now - * use the Container as follows: - - \code - class A { ... }; - class B : public A { ... }; - - Container<A>::CastIterator<B> begin; - \endcode - - * This provides the same abstraction from the internal implementation as the desired typedef. The - * same approach could be taken for a ConstCastIterator definition. However, to keep the code - * footprint small you should consider using the original type instead.\n\n - * - * - * \section adaptions Adapting a pointer vector - * - * The growth and deletion behavior of the PtrVector class can be adapted to any specific task. The - * second template argument of the PtrVector specifies the growth rate. The following growth rates - * can be selected: - * - * - ConstantGrowth - * - LinearGrowth - * - OptimalGrowth (the default behavior) - * - * The third template argument of the PtrVector specifies the deletion behavior for the case that - * the pointer vector is destroyed. Note that the deletion behavior has only limited effect on - * the memory management of the contained resources. For instance, copying a PtrVector always - * results in a shallow copy, i.e., the contained resources are not copied/cloned. Therefore the - * deletion policy should be considered a convenience functionality in the context of a resource - * managing class. The following policies can be selected: - * - * - NoDelete : No deletion of the contained pointers (the default behavior). - * - PtrDelete : Applies \a delete to all contained pointers. - * - ArrayDelete : Applies \a delete[] to all contained pointers.\n\n - */ -template< typename T // Type - , typename D = PtrDelete // Deletion policy - , typename G = OptimalGrowth > // Growth policy -class PtrVector -{ -private: - //**Friend declarations************************************************************************* - /*! \cond internal */ - template< typename T2, typename D2, typename G2 > friend class PtrVector; - /*! \endcond */ - //********************************************************************************************** - -public: - //**Type definitions**************************************************************************** - typedef T* ValueType; //!< Type of the underlying values. - typedef T* PointerType; //!< Pointer to a non-const object. - typedef const T* ConstPointerType; //!< Pointer to a const object. - typedef T*& ReferenceType; //!< Reference to a non-const object. - typedef T*const& ConstReferenceType; //!< Reference to a const object. - typedef size_t SizeType; //!< Size type of the pointer vector. - typedef PtrIterator<T> Iterator; //!< Iterator over non-const objects. - typedef PtrIterator<const T> ConstIterator; //!< Iterator over const objects. - typedef D DeletionPolicy; //!< Type of the deletion policy. - typedef G GrowthPolicy; //!< Type of the growth policy. - - // STL iterator requirements - typedef ValueType value_type; //!< Type of the underlying values. - typedef PointerType pointer; //!< Pointer to a non-const object. - typedef ConstPointerType const_pointer; //!< Pointer to a const object. - typedef ReferenceType reference; //!< Reference to a non-const object. - typedef ConstReferenceType const_reference; //!< Reference to a const object. - typedef SizeType size_type; //!< Size type of the pointer vector. - //********************************************************************************************** - - //**Forward declarations for nested classes***************************************************** - template< typename C > class CastIterator; - template< typename C > class ConstCastIterator; - //********************************************************************************************** - - //**Constructors******************************************************************************** - /*!\name Constructors */ - //@{ - explicit inline PtrVector( SizeType initCapacity = 0 ); - inline PtrVector( const PtrVector& pv ); - - template< typename T2, typename D2, typename G2 > - inline PtrVector( const PtrVector<T2,D2,G2>& pv ); - //@} - //********************************************************************************************** - - //**Destructor********************************************************************************** - /*!\name Destructor */ - //@{ - inline ~PtrVector(); - //@} - //********************************************************************************************** - - //**Assignment operators************************************************************************ - /*!\name Assignment operators */ - //@{ - PtrVector& operator=( const PtrVector& pv ); - - template< typename T2, typename D2, typename G2 > - PtrVector& operator=( const PtrVector<T2,D2,G2>& pv ); - //@} - //********************************************************************************************** - - //**Get functions******************************************************************************* - /*!\name Get functions */ - //@{ - inline SizeType maxSize() const; - inline SizeType size() const; - template< typename C > inline SizeType size() const; - inline SizeType capacity() const; - inline bool isEmpty() const; - //@} - //********************************************************************************************** - - //**Access functions**************************************************************************** - /*!\name Access functions */ - //@{ - inline ReferenceType operator[]( SizeType index ); - inline ConstReferenceType operator[]( SizeType index ) const; - inline ReferenceType front(); - inline ConstReferenceType front() const; - inline ReferenceType back(); - inline ConstReferenceType back() const; - //@} - //********************************************************************************************** - - //**Iterator functions************************************************************************** - /*!\name Iterator functions */ - //@{ - inline Iterator begin(); - inline ConstIterator begin() const; - template< typename C > inline CastIterator<C> begin(); - template< typename C > inline ConstCastIterator<C> begin() const; - - inline Iterator end(); - inline ConstIterator end() const; - template< typename C > inline CastIterator<C> end(); - template< typename C > inline ConstCastIterator<C> end() const; - //@} - //********************************************************************************************** - - //**Element functions*************************************************************************** - /*!\name Element functions */ - //@{ - inline void pushBack ( PointerType p ); - inline void popBack (); - inline void releaseBack(); - - template< typename IteratorType > - inline void assign( IteratorType first, IteratorType last ); - - inline Iterator insert( Iterator pos, PointerType p ); - - template< typename IteratorType > - inline void insert( Iterator pos, IteratorType first, IteratorType last ); - - /*! \cond internal */ - template< typename IteratorType > - inline void insert( Iterator pos, IteratorType* first, IteratorType* last ); - /*! \endcond */ - - inline Iterator erase ( Iterator pos ); - template< typename C > inline CastIterator<C> erase ( CastIterator<C> pos ); - inline Iterator release( Iterator pos ); - template< typename C > inline CastIterator<C> release( CastIterator<C> pos ); - inline void clear (); - //@} - //********************************************************************************************** - - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - void reserve( SizeType newCapacity ); - inline void swap( PtrVector& pv ) /* throw() */; - //@} - //********************************************************************************************** - -private: - //**Helper functions**************************************************************************** - /*!\name Helper functions */ - //@{ - inline size_t calcCapacity ( size_t minCapacity ) const; - inline void deleteElement( PointerType ptr ) const; - //@} - //********************************************************************************************** - - //**Insertion helper functions****************************************************************** - /*!\name Insertion helper functions */ - //@{ - void insert( T**const pos, PointerType p ); - - /*! \cond internal */ - template< typename IteratorType > - inline void insert( Iterator pos, IteratorType first, IteratorType last, std::input_iterator_tag ); - - template< typename IteratorType > - inline void insert( Iterator pos, IteratorType first, IteratorType last, std::random_access_iterator_tag ); - /*! \endcond */ - - template< typename IteratorType > - void insert( T** pos, IteratorType first, IteratorType last, SizeType n ); - //@} - //********************************************************************************************** - - //**Member variables**************************************************************************** - /*!\name Member variables */ - //@{ - SizeType size_; //!< The current size of the pointer vector. - SizeType capacity_; //!< The capacity of the pointer vector. - PointerType* begin_; //!< Pointer to the first element of the pointer vector. - PointerType* end_; //!< Pointer to the last element of the pointer vector. - //@} - //********************************************************************************************** - -public: - //**CastIterator/ConstCastIterator comparison operators***************************************** - // The following comparison operators cannot be defined as namespace or member functions - // but have to be injected into the surrounding scope via the Barton-Nackman trick since - // the template arguments of nested templates cannot be deduced (C++ standard 14.8.2.4/4). - /*!\name CastIterator/ConstCastIterator comparison operators */ - //@{ - - //********************************************************************************************** - /*!\brief Equality comparison between two CastIterator objects. - // - // \param lhs The left hand side cast iterator. - // \param rhs The right hand side cast iterator. - // \return \a true if the iterators point to the same element, \a false if not. - */ - template< typename L, typename R > - friend inline bool operator==( const CastIterator<L>& lhs, const CastIterator<R>& rhs ) - { - return lhs.base() == rhs.base(); - } - //********************************************************************************************** - - //********************************************************************************************** - /*!\brief Equality comparison between a CastIterator and a ConstCastIterator. - // - // \param lhs The left hand side cast iterator. - // \param rhs The right hand side constant cast iterator. - // \return \a true if the iterators point to the same element, \a false if not. - */ - template< typename L, typename R > - friend inline bool operator==( const CastIterator<L>& lhs, const ConstCastIterator<R>& rhs ) - { - return lhs.base() == rhs.base(); - } - //********************************************************************************************** - - //********************************************************************************************** - /*!\brief Equality comparison between a ConstCastIterator and a CastIterator. - // - // \param lhs The left hand side constant cast iterator. - // \param rhs The right hand side cast iterator. - // \return \a true if the iterators point to the same element, \a false if not. - */ - template< typename L, typename R > - friend inline bool operator==( const ConstCastIterator<L>& lhs, const CastIterator<R>& rhs ) - { - return lhs.base() == rhs.base(); - } - //********************************************************************************************** - - //********************************************************************************************** - /*!\brief Equality comparison between two ConstCastIterator objects. - // - // \param lhs The left hand side constant cast iterator. - // \param rhs The right hand side constant cast iterator. - // \return \a true if the iterators point to the same element, \a false if not. - */ - template< typename L, typename R > - friend inline bool operator==( const ConstCastIterator<L>& lhs, const ConstCastIterator<R>& rhs ) - { - return lhs.base() == rhs.base(); - } - //********************************************************************************************** - - //********************************************************************************************** - /*!\brief Inequality comparison between two CastIterator objects. - // - // \param lhs The left hand side cast iterator. - // \param rhs The right hand side cast iterator. - // \return \a true if the iterators don't point to the same element, \a false if they do. - */ - template< typename L, typename R > - friend inline bool operator!=( const CastIterator<L>& lhs, const CastIterator<R>& rhs ) - { - return lhs.base() != rhs.base(); - } - //********************************************************************************************** - - //********************************************************************************************** - /*!\brief Inequality comparison between a CastIterator and a ConstCastIterator. - // - // \param lhs The left hand side cast iterator. - // \param rhs The right hand side constant cast iterator. - // \return \a true if the iterators don't point to the same element, \a false if they do. - */ - template< typename L, typename R > - friend inline bool operator!=( const CastIterator<L>& lhs, const ConstCastIterator<R>& rhs ) - { - return lhs.base() != rhs.base(); - } - //********************************************************************************************** - - //********************************************************************************************** - /*!\brief Inequality comparison between a ConstCastIterator and a CastIterator. - // - // \param lhs The left hand side constant cast iterator. - // \param rhs The right hand side cast iterator. - // \return \a true if the iterators don't point to the same element, \a false if they do. - */ - template< typename L, typename R > - friend inline bool operator!=( const ConstCastIterator<L>& lhs, const CastIterator<R>& rhs ) - { - return lhs.base() != rhs.base(); - } - //********************************************************************************************** - - //********************************************************************************************** - /*!\brief Inequality comparison between two ConstCastIterator objects. - // - // \param lhs The left hand side constant cast iterator. - // \param rhs The right hand side constant cast iterator. - // \return \a true if the iterators don't point to the same element, \a false if they do. - */ - template< typename L, typename R > - friend inline bool operator!=( const ConstCastIterator<L>& lhs, const ConstCastIterator<R>& rhs ) - { - return lhs.base() != rhs.base(); - } - //********************************************************************************************** - - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// CONSTRUCTORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Standard constructor for PtrVector. - * - * \param initCapacity The initial capacity of the pointer vector. - * - * The default initial capacity of the pointer vector is specified by the selected growth policy. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline PtrVector<T,D,G>::PtrVector( SizeType initCapacity ) - : size_( 0 ) // Current size of the pointer vector - , capacity_( initCapacity ) // Capacity of the pointer vector - , begin_( new PointerType[initCapacity] ) // Pointer to the first element - , end_( begin_ ) // Pointer to the last element -{} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Copy constructor for PtrVector. - * - * \param pv The pointer vector to be copied. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline PtrVector<T,D,G>::PtrVector( const PtrVector& pv ) - : size_( pv.size_ ) // Current size of the pointer vector - , capacity_( pv.size_ ) // Capacity of the pointer vector - , begin_( new PointerType[capacity_] ) // Pointer to the first element - , end_( begin_+size_ ) // Pointer to the last element -{ - for( SizeType i=0; i<size_; ++i ) - begin_[i] = pv.begin_[i]; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Conversion constructor from different PtrVector instances. - * - * \param pv The pointer vector to be copied. - */ -template< typename T // Type of the pointer vector - , typename D // Deletion policy of the pointer vector - , typename G > // Growth policy of the pointer vector -template< typename T2 // Type of the foreign pointer vector - , typename D2 // Deletion policy of the foreign pointer vector - , typename G2 > // Growth policy of the foreign pointer vector -inline PtrVector<T,D,G>::PtrVector( const PtrVector<T2,D2,G2>& pv ) - : size_( pv.size_ ) // Current size of the pointer vector - , capacity_( pv.size_ ) // Capacity of the pointer vector - , begin_( new PointerType[capacity_] ) // Pointer to the first element - , end_( begin_+size_ ) // Pointer to the last element -{ - for( SizeType i=0; i<size_; ++i ) - begin_[i] = pv.begin_[i]; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// DESTRUCTOR -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Destructor for PtrVector. - * - * In the destructor, the selected deletion policy is applied to all elements of the pointer - * vector. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline PtrVector<T,D,G>::~PtrVector() -{ - for( PointerType* it=begin_; it!=end_; ++it ) - deleteElement( *it ); - delete [] begin_; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// ASSIGNMENT OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Copy assignment operator for PtrVector. - * - * \param pv The pointer vector to be copied. - * \return Reference to the assigned pointer vector. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -PtrVector<T,D,G>& PtrVector<T,D,G>::operator=( const PtrVector& pv ) -{ - if( &pv == this ) return *this; - - if( pv.size_ > capacity_ ) { - PointerType* newBegin( new PointerType[pv.size_] ); - end_ = std::copy( pv.begin_, pv.end_, newBegin ); - std::swap( begin_, newBegin ); - delete [] newBegin; - - size_ = pv.size_; - capacity_ = pv.size_; - } - else { - end_ = std::copy( pv.begin_, pv.end_, begin_ ); - size_ = pv.size_; - } - - return *this; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Assignment operator for different PtrVector instances. - * - * \param pv The pointer vector to be copied. - * \return Reference to the assigned pointer vector. - */ -template< typename T // Type of the pointer vector - , typename D // Deletion policy of the pointer vector - , typename G > // Growth policy of the pointer vector -template< typename T2 // Type of the foreign pointer vector - , typename D2 // Deletion policy of the foreign pointer vector - , typename G2 > // Growth policy of the foreign pointer vector -PtrVector<T,D,G>& PtrVector<T,D,G>::operator=( const PtrVector<T2,D2,G2>& pv ) -{ - if( pv.size_ > capacity_ ) { - PointerType* newBegin( new PointerType[pv.size_] ); - end_ = std::copy( pv.begin_, pv.end_, newBegin ); - std::swap( begin_, newBegin ); - delete [] newBegin; - - size_ = pv.size_; - capacity_ = pv.size_; - } - else { - end_ = std::copy( pv.begin_, pv.end_, begin_ ); - size_ = pv.size_; - } - - return *this; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// GET FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Returns the maximum possible size of a pointer vector. - * - * \return The maximum possible size. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::SizeType PtrVector<T,D,G>::maxSize() const -{ - return SizeType(-1) / sizeof(PointerType); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns the current size of the pointer vector. - * - * \return The current size. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::SizeType PtrVector<T,D,G>::size() const -{ - return size_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns the total number of objects of type \a C contained in the pointer vector. - * - * \return The total number of objects of type \a C. - * - * This function calculates the total number of objects of type \a C within the pointer vector, - * where \a C is a type derived from the type \a T of objects contained in the pointer vector. - * The attempt to use this function for types that are not derived from \a T results in a - * compile time error. - - \code - // Definition of class A and the derived type B - class A { ... }; - class B : public A { ... }; - - // Definition of a pointer vector for class A - typedef PtrVector<A> AVector; - AVector vector; - - AVector::SizeType total = vector.size(); // Calculating the total number of pointers - AVector::SizeType numB = vector.size<B>(); // Calculating the total number of B objects - \endcode - - * \b Note: The total number of objects of type \a C is not cached inside the pointer vector - * but is calculated each time the function is called. Using the templated version of size() - * to calculate the total number objects of type \a C is therefore more expensive than using - * the non-template version of size() to get the total number of pointers in the vector! - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::SizeType PtrVector<T,D,G>::size() const -{ - // The polymorphicCount() function returns the number of objects with dynamic type 'C' - // contained in the range [begin,end). An equivalent code might look like this: - // - // SizeType count( 0 ); - // for( PointerType* it=begin_; it!=end_; ++it ) - // if( dynamic_cast<C*>( *it ) ) ++count; - // return count; - // - // However, the specialization of polymorphicCount() for special type combinations is - // much more efficient (and easier) than the specialization of this function! - return polymorphicCount<C>( begin_, end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns the capacity of the pointer vector. - * - * \return The capacity. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::SizeType PtrVector<T,D,G>::capacity() const -{ - return capacity_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns \a true if the pointer vector has no elements. - * - * \return \a true if the pointer vector is empty, \a false if it is not. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline bool PtrVector<T,D,G>::isEmpty() const -{ - return size_ == 0; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// ACCESS FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Subscript operator for the direct access to the pointer vector elements. - * - * \param index Access index. The index has to be in the range \f$[0..size-1]\f$. - * \return Handle to the accessed element. - * - * \b Note: No runtime check is performed to insure the validity of the access index. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::ReferenceType PtrVector<T,D,G>::operator[]( SizeType index ) -{ - return *(begin_+index); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Subscript operator for the direct access to the pointer vector elements. - * - * \param index Access index. The index has to be in the range \f$[0..size-1]\f$. - * \return Handle to the accessed element. - * - * \b Note: No runtime check is performed to insure the validity of the access index. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::ConstReferenceType PtrVector<T,D,G>::operator[]( SizeType index ) const -{ - return *(begin_+index); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns a reference to the first element of the pointer vector. - * - * \return Handle to the first element. - * - * \b Note: No runtime check is performed if the first element exists! - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::ReferenceType PtrVector<T,D,G>::front() -{ - WALBERLA_ASSERT( size_ > 0, "Pointer vector is empty, invalid access to the front element" ); - return *begin_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns a reference to the first element of the pointer vector. - * - * \return Handle to the first element. - * - * \b Note: No runtime check is performed if the first element exists! - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::ConstReferenceType PtrVector<T,D,G>::front() const -{ - WALBERLA_ASSERT( size_ > 0, "Pointer vector is empty, invalid access to the front element" ); - return *begin_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns a reference to the last element of the pointer vector. - * - * \return Handle to the last element. - * - * \b Note: No runtime check is performed if the last element exists! - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::ReferenceType PtrVector<T,D,G>::back() -{ - WALBERLA_ASSERT( size_ > 0, "Pointer vector is empty, invalid access to the back element" ); - return *(end_-1); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns a reference to the last element of the pointer vector. - * - * \return Handle to the last element. - * - * \b Note: No runtime check is performed if the last element exists! - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::ConstReferenceType PtrVector<T,D,G>::back() const -{ - WALBERLA_ASSERT( size_ > 0, "Pointer vector is empty, invalid access to the back element" ); - return *(end_-1); -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// ITERATOR FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Returns an iterator to the beginning of the pointer vector. - * - * \return Iterator to the beginning of the pointer vector. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::Iterator PtrVector<T,D,G>::begin() -{ - return Iterator( begin_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns an iterator to the beginning of the pointer vector. - * - * \return Iterator to the beginning of the pointer vector. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::ConstIterator PtrVector<T,D,G>::begin() const -{ - return ConstIterator( begin_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns an iterator to the first element of type \a C within the pointer vector. - * - * \return Iterator to the first element of type \a C. - * - * This function returns an iterator to the first element of type \a C within in the pointer - * vector, where \a C is a type derived from the type \a T of objects contained in the pointer - * vector. In case there is no element of type \a C contained in the vector, an iterator just - * past the last element of the pointer vector is returned. In combination with the according - * end function (see example), this iterator allows to iterate over all objects of type \a C - * in the range of the pointer vector. The attempt to use this function for types that are not - * derived from \a T results in a compile time error. - - \code - // Definition of class A and the derived type B - class A { ... }; - class B : public A { ... }; - - // Definition of function f for non-const pointer vectors - void f( PtrVector<A>& vector ) - { - PtrVector<A>::CastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::CastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - ... - } - - // Definition of function f for const pointer vectors - void f( const PtrVector<A>& vector ) - { - PtrVector<A>::ConstCastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::ConstCastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - } - \endcode - - * \b Note: Using the templated versions of begin() and end() to traverse all elements of type - * \a C in the element range of the pointer vector is more expensive than using the non-template - * versions to traverse the entire range of elements. Use this function only if you require a - * type-specific member of type \a C. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C> PtrVector<T,D,G>::begin() -{ - return CastIterator<C>( begin_, end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns an iterator to the first element of type \a C within the pointer vector. - * - * \return Iterator to the first element of type \a C. - * - * This function returns an iterator to the first element of type \a C within in the pointer - * vector, where \a C is a type derived from the type \a T of objects contained in the pointer - * vector. In case there is no element of type \a C contained in the vector, an iterator just - * past the last element of the pointer vector is returned. In combination with the according - * end function (see example), this iterator allows to iterate over all objects of type \a C - * in the range of the pointer vector. The attempt to use this function for types that are not - * derived from \a T results in a compile time error. - - \code - // Definition of class A and the derived type B - class A { ... }; - class B : public A { ... }; - - // Definition of function f for non-const pointer vectors - void f( PtrVector<A>& vector ) - { - PtrVector<A>::CastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::CastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - ... - } - - // Definition of function f for const pointer vectors - void f( const PtrVector<A>& vector ) - { - PtrVector<A>::ConstCastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::ConstCastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - } - \endcode - - * \b Note: Using the templated versions of begin() and end() to traverse all elements of type - * \a C in the element range of the pointer vector is more expensive than using the non-template - * version to traverse the entire range of elements. Use this function only if you require a - * type-specific member of type \a C. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE ConstCastIterator<C> PtrVector<T,D,G>::begin() const -{ - return ConstCastIterator<C>( begin_, end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns an iterator just past the last element of the pointer vector. - * - * \return Iterator just past the last element of the pointer vector. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::Iterator PtrVector<T,D,G>::end() -{ - return Iterator( end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns an iterator just past the last element of the pointer vector. - * - * \return Iterator just past the last element of the pointer vector. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::ConstIterator PtrVector<T,D,G>::end() const -{ - return ConstIterator( end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns an iterator just past the last element of the pointer vector. - * - * \return Iterator just past the last element of the pointer vector. - * - * This function returns an iterator just past the last element of the pointer vector. In - * combination with the according begin function (see example), this iterator allows to iterate - * over all objects of type \a C in the range of the pointer vector. The attempt to use this - * function for types that are not derived from \a T results in a compile time error. - - \code - // Definition of class A and the derived type B - class A { ... }; - class B : public A { ... }; - - // Definition of function f for non-const pointer vectors - void f( PtrVector<A>& vector ) - { - PtrVector<A>::CastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::CastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - ... - } - - // Definition of function f for const pointer vectors - void f( const PtrVector<A>& vector ) - { - PtrVector<A>::ConstCastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::ConstCastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - } - \endcode - - * \b Note: Using the templated versions of begin() and end() to traverse all elements of type - * \a C in the element range of the pointer vector is more expensive than using the non-template - * versions to traverse the entire range of elements. Use this function only if you require a - * type-specific member of type \a C. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C> PtrVector<T,D,G>::end() -{ - return CastIterator<C>( end_, end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Returns an iterator just past the last element of the pointer vector. - * - * \return Iterator just past the last element of the pointer vector. - * - * This function returns an iterator just past the last element of the pointer vector. In - * combination with the according begin function (see example), this iterator allows to iterate - * over all objects of type \a C in the range of the pointer vector. The attempt to use this - * function for types that are not derived from \a T results in a compile time error. - - \code - // Definition of class A and the derived type B - class A { ... }; - class B : public A { ... }; - - // Definition of function f for non-const pointer vectors - void f( PtrVector<A>& vector ) - { - PtrVector<A>::CastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::CastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - ... - } - - // Definition of function f for const pointer vectors - void f( const PtrVector<A>& vector ) - { - PtrVector<A>::ConstCastIterator<B> begin = vector.begin<B>(); - PtrVector<A>::ConstCastIterator<B> end = vector.end<B>(); - - // Loop over all objects of type B contained in the vector - for( ; begin!=end; ++begin ) - } - \endcode - - * \b Note: Using the templated versions of begin() and end() to traverse all elements of type - * \a C in the element range of the pointer vector is more expensive than using the non-template - * version to traverse the entire range of elements. Use this function only if you require a - * type-specific member of type \a C. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE ConstCastIterator<C> PtrVector<T,D,G>::end() const -{ - return ConstCastIterator<C>( end_, end_ ); -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// ELEMENT FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Adding an element to the end of the pointer vector. - * - * \param p The pointer to be added to the end of the pointer vector. - * \return void - * \exception std::length_error Maximum pointer vector length exceeded. - * - * The pushBack function runs in constant time. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline void PtrVector<T,D,G>::pushBack( PointerType p ) -{ - if( size_ != capacity_ ) { - *end_ = p; - ++end_; - ++size_; - } - else { - insert( end_, p ); - } -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Removing an element from the end of the pointer vector. - * - * \return void - * - * This function removes the element at the end of the pointer vector, i.e. the element - * is deleted according to the deletion policy and removed from the vector. Note that in - * case the deletion policy is NoDelete, this function is identical to the releaseBack() - * function. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline void PtrVector<T,D,G>::popBack() -{ - deleteElement( *--end_ ); - --size_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Releasing the element at the end of the pointer vector. - * - * \return void - * - * This function releases the element at the end of the pointer vector, i.e. the element is - * removed without applying the deletion policy. Therefore the responsibility to delete the - * element is passed to the function caller. Note that in case the deletion policy is NoDelete, - * this function is identical to the popBack() function. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline void PtrVector<T,D,G>::releaseBack() -{ - --end_; - --size_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Assigning a range of elements to the pointer vector. - * - * \param first Iterator to the first element of the element range. - * \param last Iterator to the element one past the last element of the element range. - * \return void - * \exception std::length_error Maximum pointer vector length exceeded. - * - * This functions assigns the elements in the range \f$ [first,last) \f$ to the pointer vector. - * All elements previously contained in the pointer vector are removed. The assign function runs - * in linear time. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename IteratorType > -inline void PtrVector<T,D,G>::assign( IteratorType first, IteratorType last ) -{ - clear(); - insert( end(), first, last ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Inserting an element into the pointer vector. - * - * \param pos The position before which the element is inserted. - * \param p The pointer to be inserted into the pointer vector. - * \return Iterator to the inserted element. - * \exception std::length_error Maximum pointer vector length exceeded. - * - * The insert function runs in linear time. Note however that inserting elements into a pointer - * vector can be a relatively time-intensive operation. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::Iterator PtrVector<T,D,G>::insert( Iterator pos, PointerType p ) -{ - T** const base = const_cast<T**>( pos.base() ); - const auto diff( base - begin_ ); - - if( size_ != capacity_ && base == end_ ) { - *end_ = p; - ++end_; - ++size_; - } - else { - insert( base, p ); - } - - return Iterator( begin_+diff ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Inserting a range of elements into the pointer vector. - * - * \param pos The position before which the elements are inserted. - * \param first Iterator to the first element of the element range. - * \param last Iterator to the element one past the last element of the element range. - * \return void - * \exception std::length_error Maximum pointer vector length exceeded. - * - * This functions inserts the elements in the range \f$ [first,last) \f$ into the pointer vector. - * The insert function runs in linear time. Note however that inserting elements into a pointer - * vector can be a relatively time-intensive operation. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename IteratorType > -inline void PtrVector<T,D,G>::insert( Iterator pos, IteratorType first, IteratorType last ) -{ - insert( pos, first, last, typename IteratorType::iterator_category() ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*! \cond internal */ -/*!\brief Inserting a range of elements into the pointer vector. - * - * \param pos The position before which the elements are inserted. - * \param first Pointer to the first element of the element range. - * \param last Pointer to the element one past the last element of the element range. - * \return void - * \exception std::length_error Maximum pointer vector length exceeded. - * - * This functions inserts the elements in the range \f$ [first,last) \f$ into the pointer vector. - * The insert function runs in linear time. Note however that inserting elements into a pointer - * vector can be a relatively time-intensive operation. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename IteratorType > -inline void PtrVector<T,D,G>::insert( Iterator pos, IteratorType* first, IteratorType* last ) -{ - insert( pos, first, last, std::random_access_iterator_tag() ); -} -/*! \endcond */ -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Removing an element from the pointer vector. - * - * \param pos The position of the element to be removed. - * \return Iterator to the element after the erased element. - * - * This function erases an element from the pointer vector, i.e. the element is deleted - * according to the deletion policy of the pointer vector and removed from the vector. - * Note that in case the deletion policy is NoDelete, this function is identical to the - * release() function. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::Iterator PtrVector<T,D,G>::erase( Iterator pos ) -{ - T** const base = const_cast<T**>( pos.base() ); - deleteElement( *base ); - std::copy( base+1, end_, base ); - - --size_; - --end_; - - return pos; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Removing an element from the pointer vector. - * - * \param pos The position of the element to be removed. - * \return Iterator to the element after the erased element. - * - * This function erases an element from the pointer vector, i.e. the element is deleted - * according to the deletion policy of the pointer vector and removed from the vector. - * Note that in case the deletion policy is NoDelete, this function is identical to the - * release() function. - * - * Note: The cast iterator \a pos is not invalidated but every other cast iterators including those - * returned by end<C>() are. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C> - PtrVector<T,D,G>::erase( CastIterator<C> pos ) -{ - T** const base = const_cast<T**>( pos.base() ); - deleteElement( *base ); - std::copy( base+1, end_, base ); - - --size_; - --end_; - - return CastIterator<C>( base, end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Releasing an element from the pointer vector. - * - * \param pos The position of the element to be released. - * \return Iterator to the element after the released element. - * - * This function releases an element from the pointer vector, i.e. the element is removed - * without applying the deletion policy. Therefore the responsibility to delete the element - * is passed to the function caller. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline typename PtrVector<T,D,G>::Iterator PtrVector<T,D,G>::release( Iterator pos ) -{ - T** const base = const_cast<T**>( pos.base() ); - std::copy( base+1, end_, base ); - - --size_; - --end_; - - return pos; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Releasing an element from the pointer vector. - * - * \param pos The position of the element to be released. - * \return Iterator to the element after the released element. - * - * This function releases an element from the pointer vector, i.e. the element is removed - * without applying the deletion policy. Therefore the responsibility to delete the element - * is passed to the function caller. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C> - PtrVector<T,D,G>::release( CastIterator<C> pos ) -{ - T** const base = const_cast<T**>( pos.base() ); - std::copy( base+1, end_, base ); - - --size_; - --end_; - - return CastIterator<C>( base, end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Removing all elements from the pointer vector. - * - * \return void - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline void PtrVector<T,D,G>::clear() -{ - for( PointerType* it=begin_; it!=end_; ++it ) - deleteElement( *it ); - - end_ = begin_; - size_ = 0; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Setting the minimum capacity of the pointer vector. - * - * \param newCapacity The new minimum capacity of the pointer vector. - * \return void - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -void PtrVector<T,D,G>::reserve( SizeType newCapacity ) -{ - if( newCapacity > capacity_ ) - { - // Calculating the new capacity - newCapacity = calcCapacity( newCapacity ); - - // Allocating a new array - PointerType* tmp = new PointerType[newCapacity]; - - // Replacing the old array - std::copy( begin_, end_, tmp ); - std::swap( tmp, begin_ ); - capacity_ = newCapacity; - end_ = begin_ + size_; - delete [] tmp; - } -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Swapping the contents of two pointer vectors. - * - * \param pv The pointer vector to be swapped. - * \return void - * \exception no-throw guarantee. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline void PtrVector<T,D,G>::swap( PtrVector& pv ) /* throw() */ -{ - // By using the 'std::swap' function to swap all member variables, - // the function can give the nothrow guarantee. - std::swap( size_, pv.size_ ); - std::swap( capacity_, pv.capacity_ ); - std::swap( begin_, pv.begin_ ); - std::swap( end_, pv.end_ ); -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// HELPER FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Calculating the new capacity of the vector based on its growth policy. - * - * \param minCapacity The minimum necessary capacity. - * \return The new capacity. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline size_t PtrVector<T,D,G>::calcCapacity( size_t minCapacity ) const -{ - WALBERLA_ASSERT( minCapacity > capacity_, "Invalid new vector capacity" ); - const size_t newCapacity( GrowthPolicy()( capacity_, minCapacity ) ); - WALBERLA_ASSERT( newCapacity > capacity_, "Invalid new vector capacity" ); - return newCapacity; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Deleting an element of the pointer vector according to the deletion policy. - * - * \param ptr The element to be deleted. - * \return void - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline void PtrVector<T,D,G>::deleteElement( PointerType ptr ) const -{ - DeletionPolicy()( ptr ); -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// INSERTION HELPER FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Inserting an element into the pointer vector. - * - * \param pos The position before which the element is inserted. - * \param p The pointer to be inserted into the pointer vector. - * \return void - * \exception std::length_error Maximum pointer vector length exceeded. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -void PtrVector<T,D,G>::insert( T**const pos, PointerType p ) -{ - if( size_ != capacity_ ) { - std::copy_backward( pos, end_, end_+1 ); - *pos = p; - ++end_; - ++size_; - } - else if( size_ == maxSize() ) { - throw std::length_error( "Maximum pointer vector length exceeded!" ); - } - else { - SizeType newCapacity( calcCapacity( capacity_+1 ) ); - if( newCapacity > maxSize() || newCapacity < capacity_ ) newCapacity = maxSize(); - - PointerType* newBegin = new PointerType[newCapacity]; - PointerType* newEnd = std::copy( begin_, pos, newBegin ); - *newEnd = p; - ++newEnd; - end_ = std::copy( pos, end_, newEnd ); - - std::swap( newBegin, begin_ ); - delete [] newBegin; - capacity_ = newCapacity; - ++size_; - } -} -//************************************************************************************************* - - -//************************************************************************************************* -/*! \cond internal */ -/*!\brief Inserting a range of elements into the pointer vector. - * - * \param pos The position before which the elements are inserted. - * \param first Iterator to the first element of the element range. - * \param last Iterator to the element one past the last element of the element range. - * \return void - * \exception std::length_error Maximum pointer vector length exceeded. - * - * This functions inserts the elements in the range \f$ [first,last) \f$ into the pointer vector. - * The iterators are treated as input iterators. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename IteratorType > -inline void PtrVector<T,D,G>::insert( Iterator pos, IteratorType first, IteratorType last, - std::input_iterator_tag ) -{ - for( ; first!=last; ++first ) { - pos = insert( pos, *first ); - ++pos; - } -} -/*! \endcond */ -//************************************************************************************************* - - -//************************************************************************************************* -/*! \cond internal */ -/*!\brief Inserting a range of elements into the pointer vector. - * - * \param pos The position before which the elements are inserted. - * \param first Iterator to the first element of the element range. - * \param last Iterator to the element one past the last element of the element range. - * \return void - * \exception std::length_error Maximum pointer vector length exceeded. - * - * This functions inserts the elements in the range \f$ [first,last) \f$ into the pointer vector. - * The iterators are treated as random access iterators. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename IteratorType > -inline void PtrVector<T,D,G>::insert( Iterator pos, IteratorType first, IteratorType last, - std::random_access_iterator_tag ) -{ - T** const base = const_cast<T**>( pos.base() ); - const SizeType diff( last - first ); - - if( size_+diff <= capacity_ && base == end_ ) { - for( ; first!=last; ++first, ++end_ ) { - *end_ = *first; - } - size_ += diff; - } - else { - insert( base, first, last, diff ); - } -} -/*! \endcond */ -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Inserting a range of elements into the pointer vector. - * - * \param pos The position before which the elements are inserted. - * \param first Iterator to the first element of the element range. - * \param last Iterator to the element one past the last element of the element range. - * \param n The number of elements to be inserted. - * \return void - * \exception std::length_error Maximum pointer vector length exceeded. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename IteratorType > -void PtrVector<T,D,G>::insert( T** pos, IteratorType first, IteratorType last, SizeType n ) -{ - const SizeType newSize( size_ + n ); - - if( newSize <= capacity_ ) { - std::copy_backward( pos, end_, end_+n ); - for( ; first!=last; ++first, ++pos ) { - *pos = *first; - } - end_ += n; - size_ = newSize; - } - else if( newSize > maxSize() || newSize < size_ ) { - throw std::length_error( "Maximum pointer vector length exceeded!" ); - } - else { - PointerType* newBegin = new PointerType[newSize]; - PointerType* newEnd = std::copy( begin_, pos, newBegin ); - - for( ; first!=last; ++first, ++newEnd ) { - *newEnd = *first; - } - - end_ = std::copy( pos, end_, newEnd ); - - std::swap( newBegin, begin_ ); - delete [] newBegin; - capacity_ = newSize; - size_ = newSize; - } -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// GLOBAL OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\name PtrVector operators */ -//@{ -template< typename T, typename D, typename G > -inline bool operator==( const PtrVector<T,D,G>& lhs, const PtrVector<T,D,G>& rhs ); - -template< typename T, typename D, typename G > -inline bool operator!=( const PtrVector<T,D,G>& lhs, const PtrVector<T,D,G>& rhs ); - -template< typename T, typename D, typename G > -inline void swap( PtrVector<T,D,G>& a, PtrVector<T,D,G>& b ) /* throw() */; -//@} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Equality comparison between two pointer vectors. - * - * \param lhs The left hand side pointer vector. - * \param rhs The right hand side pointer vector. - * \return \a true if the two pointer vectors are equal, \a false if they are not. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline bool operator==( const PtrVector<T,D,G>& lhs, const PtrVector<T,D,G>& rhs ) -{ - return lhs.size() == rhs.size() && std::equal( lhs.begin(), lhs.end(), rhs.begin() ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Inequality comparison between two pointer vectors. - * - * \param lhs The left hand side pointer vector. - * \param rhs The right hand side pointer vector. - * \return \a true if the two pointer vectors are inequal, \a false if they are not. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline bool operator!=( const PtrVector<T,D,G>& lhs, const PtrVector<T,D,G>& rhs ) -{ - return lhs.size() != rhs.size() || !std::equal( lhs.begin(), lhs.end(), rhs.begin() ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Swapping the contents of two pointer vectors. - * - * \param a The first pointer vector to be swapped. - * \param b The second pointer vector to be swapped. - * \return void - * \exception no-throw guarantee. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -inline void swap( PtrVector<T,D,G>& a, PtrVector<T,D,G>& b ) /* throw() */ -{ - a.swap( b ); -} -//************************************************************************************************* - - - - - - - - -//================================================================================================= -// -// NESTED CLASS PTRVECTOR::CASTITERATOR -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Dynamic cast iterator for polymorphic pointer vectors. - * \ingroup util - * - * The CastIterator class is part of the PtrVector class and represent a forward iterator - * over all elements of type \a C contained in a range of elements of type \a T, where \a C - * is a type derived from \a T. - - \code - class A { ... }; - class B : public class A { ... }; - - PtrVector<A>::CastIterator<B> begin; - PtrVector<A>::CastIterator<B> end; - - // Loop over all elements of type B within the range [begin..end) - for( ; begin!=end; ++begin ) - ... - \endcode - - * \b Note: Using a CastIterator is computationally more expensive than using a standard - * iterator over all elements contained in the vector. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -class PtrVector<T,D,G>::CastIterator -{ -public: - //**Type definitions**************************************************************************** - // pe naming convention - typedef std::forward_iterator_tag IteratorCategory; //!< The iterator category. - typedef C* ValueType; //!< Type of the underlying pointers. - typedef C* PointerType; //!< Pointer return type. - typedef C* const& ReferenceType; //!< Reference return type. - typedef ptrdiff_t DifferenceType; //!< Difference between two iterators. - typedef T* const* IteratorType; //!< Type of the internal pointer. - - // STL iterator requirements - typedef IteratorCategory iterator_category; //!< The iterator category. - typedef ValueType value_type; //!< Type of the underlying pointers. - typedef PointerType pointer; //!< Pointer return type. - typedef ReferenceType reference; //!< Reference return type. - typedef DifferenceType difference_type; //!< Difference between two iterators. - //********************************************************************************************** - - //**Constructors******************************************************************************** - /*!\name Constructors */ - //@{ - inline CastIterator(); - inline CastIterator( IteratorType begin, IteratorType end ); - - template< typename Other > - inline CastIterator( const CastIterator<Other>& it ); - - // No explicitly declared copy constructor. - //@} - //********************************************************************************************** - - //**Destructor********************************************************************************** - // No explicitly declared destructor. - //********************************************************************************************** - - //**Copy assignment operator******************************************************************** - // No explicitly declared copy assignment operator. - //********************************************************************************************** - - //**Operators*********************************************************************************** - /*!\name Operators */ - //@{ - inline CastIterator& operator++(); - inline CastIterator operator++( int ); - //@} - //********************************************************************************************** - - //**Access operators**************************************************************************** - /*!\name Access operators */ - //@{ - inline PointerType operator*() const; - inline PointerType operator->() const; - //@} - //********************************************************************************************** - - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - inline const IteratorType& base() const; - inline const IteratorType& stop() const; - //@} - //********************************************************************************************** - -private: - //**Member variables**************************************************************************** - /*!\name Member variables */ - //@{ - IteratorType cur_; //!< Pointer to the current memory location. - IteratorType end_; //!< Pointer to the element one past the last element in the element range. - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// CONSTRUCTOR -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Default constructor for CastIterator. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline PtrVector<T,D,G>::CastIterator<C>::CastIterator() - : cur_(NULL) // Pointer to the current memory location - , end_(NULL) // Pointer to the element one past the last element in the element range -{ - static_assert(boost::is_base_of<T, C>::value && !boost::is_base_of<C, T>::value, "C has to be strictly derived from T"); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Standard constructor for CastIterator. - * - * \param begin The beginning of the element range. - * \param end The end of the element range. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline PtrVector<T,D,G>::CastIterator<C>::CastIterator( IteratorType begin, IteratorType end ) - : cur_(begin) // Pointer to the current memory location - , end_(end) // Pointer to the element one past the last element in the element range -{ - static_assert(boost::is_base_of<T, C>::value && !boost::is_base_of<C, T>::value, "C has to be strictly derived from T"); - // The polymorphicFind() function finds the next pointer to an object with dynamic type 'C' - // contained in the range [cur_,end). An equivalent code might look like this: - // - // while( cur_ != end_ && !dynamic_cast<C*>( *cur_ ) ) ++cur_; - // - // However, the specialization of polymorphicFind() for special type combinations is much - // more efficient (and way easier!) than the specialization of this function! - cur_ = polymorphicFind<C>( cur_, end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Conversion constructor from different CastIterator instances. - * - * \param it The foreign CastIterator instance to be copied. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -template< typename Other > // The foreign cast iterator type -inline PtrVector<T,D,G>::CastIterator<C>::CastIterator( const CastIterator<Other>& it ) - : cur_( it.base() ) // Pointer to the current memory location - , end_( it.stop() ) // Pointer to the element one past the last element in the element range -{ - static_assert(boost::is_base_of<C, T>::value && !boost::is_base_of<C, T>::value, "C has to be strictly derived from T"); - static_assert(boost::is_convertible<Other*, C*>::value, "Other must be convertible to C" ); -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Pre-increment operator. - * - * \return Reference to the incremented cast iterator. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C>& - PtrVector<T,D,G>::CastIterator<C>::operator++() -{ - // The polymorphicFind() function finds the next pointer to an object with dynamic type 'C' - // contained in the range [cur_+1,end). An equivalent code might look like this: - // - // while( ++cur_ != end_ && !dynamic_cast<C*>( *cur_ ) ) {} - // - // However, the specialization of polymorphicFind() for special type combinations is much - // more efficient (and way easier!) than the specialization of this function! - cur_ = polymorphicFind<C>( ++cur_, end_ ); - - return *this; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Post-increment operator. - * - * \return The incremented cast iterator. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C> - PtrVector<T,D,G>::CastIterator<C>::operator++( int ) -{ - CastIterator tmp( *this ); - - // The polymorphicFind() function finds the next pointer to an object with dynamic type 'C' - // contained in the range [cur_+1,end). An equivalent code might look like this: - // - // while( ++cur_ != end_ && !dynamic_cast<C*>( *cur_ ) ) {} - // - // However, the specialization of polymorphicFind() for special type combinations is much - // more efficient (and way easier!) than the specialization of this function! - cur_ = polymorphicFind<C>( ++cur_, end_ ); - - return tmp; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// ACCESS OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Returns a handle to the element at the current iterator position. - * - * \return Handle to the element at the current iterator position. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C>::PointerType - PtrVector<T,D,G>::CastIterator<C>::operator*() const -{ - return static_cast<C*>( *cur_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Direct access to the element at the current iterator position. - * - * \return Reference to the element at the current iterator position. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C>::PointerType - PtrVector<T,D,G>::CastIterator<C>::operator->() const -{ - return static_cast<C*>( *cur_ ); -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Direct access to the current memory location of the cast iterator. - * - * \return Pointer to the current memory location. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline const typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C>::IteratorType& - PtrVector<T,D,G>::CastIterator<C>::base() const -{ - return cur_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Direct access to the final memory location of the cast iterator. - * - * \return Pointer to the final memory location. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline const typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<C>::IteratorType& - PtrVector<T,D,G>::CastIterator<C>::stop() const -{ - return end_; -} -//************************************************************************************************* - - - - - - - - -//================================================================================================= -// -// NESTED CLASS PTRVECTOR::CONSTCASTITERATOR -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Dynamic cast iterator for polymorphic pointer vectors. - * \ingroup util - * - * The ConstCastIterator class is part of the PtrVector class and represent a forward iterator - * over all elements of type \a C contained in a range of elements of type \a T, where \a C - * is a type derived from \a T. The ConstCastIterator is the counterpart of CastIterator for - * constant vectors. - - \code - class A { ... }; - class B : public class A { ... }; - - PtrVector<A>::ConstCastIterator<B> begin; - PtrVector<A>::ConstCastIterator<B> end; - - // Loop over all elements of type B within the range [begin..end) - for( ; begin!=end; ++begin ) - ... - \endcode - - * \b Note: Using a ConstCastIterator is computationally more expensive than using a standard - * iterator over all elements contained in the vector. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -class PtrVector<T,D,G>::ConstCastIterator -{ -public: - //**Type definitions**************************************************************************** - // pe naming convention - typedef std::forward_iterator_tag IteratorCategory; //!< The iterator category. - typedef const C* ValueType; //!< Type of the underlying pointers. - typedef const C* PointerType; //!< Pointer return type. - typedef const C* const& ReferenceType; //!< Reference return type. - typedef ptrdiff_t DifferenceType; //!< Difference between two iterators. - typedef const T* const* IteratorType; //!< Type of the internal pointer. - - // STL iterator requirements - typedef IteratorCategory iterator_category; //!< The iterator category. - typedef ValueType value_type; //!< Type of the underlying pointers. - typedef PointerType pointer; //!< Pointer return type. - typedef ReferenceType reference; //!< Reference return type. - typedef DifferenceType difference_type; //!< Difference between two iterators. - //********************************************************************************************** - - //**Constructors******************************************************************************** - /*!\name Constructors */ - //@{ - inline ConstCastIterator(); - inline ConstCastIterator( IteratorType begin, IteratorType end ); - - template< typename Other > - inline ConstCastIterator( const ConstCastIterator<Other>& it ); - - template< typename Other > - inline ConstCastIterator( const typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<Other>& it ); - - // No explicitly declared copy constructor. - //@} - //********************************************************************************************** - - //**Destructor********************************************************************************** - // No explicitly declared destructor. - //********************************************************************************************** - - //**Copy assignment operator******************************************************************** - // No explicitly declared copy assignment operator. - //********************************************************************************************** - - //**Operators*********************************************************************************** - /*!\name Operators */ - //@{ - inline ConstCastIterator& operator++(); - inline ConstCastIterator operator++( int ); - //@} - //********************************************************************************************** - - //**Access operators**************************************************************************** - /*!\name Access operators */ - //@{ - inline PointerType operator*() const; - inline PointerType operator->() const; - //@} - //********************************************************************************************** - - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - inline const IteratorType& base() const; - inline const IteratorType& stop() const; - //@} - //********************************************************************************************** - -private: - //**Member variables**************************************************************************** - /*!\name Member variables */ - //@{ - IteratorType cur_; //!< Pointer to the current memory location. - IteratorType end_; //!< Pointer to the element one past the last element in the element range. - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// CONSTRUCTOR -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Default constructor for ConstCastIterator. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline PtrVector<T,D,G>::ConstCastIterator<C>::ConstCastIterator() - : cur_(NULL) // Pointer to the current memory location - , end_(NULL) // Pointer to the element one past the last element in the element range -{ - static_assert(boost::is_base_of<C, T>::value && !boost::is_base_of<C, T>::value, "C has to be strictly derived from T"); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Standard constructor for ConstCastIterator. - * - * \param begin The beginning of the element range. - * \param end The end of the element range. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline PtrVector<T,D,G>::ConstCastIterator<C>::ConstCastIterator( IteratorType begin, IteratorType end ) - : cur_(begin) // Pointer to the current memory location - , end_(end) // Pointer to the element one past the last element in the element range -{ - // The polymorphicFind() function finds the next pointer to an object with dynamic type 'C' - // contained in the range [cur_,end). An equivalent code might look like this: - // - // while( cur_ != end_ && !dynamic_cast<C*>( *cur_ ) ) ++cur_; - // - // However, the specialization of polymorphicFind() for special type combinations is much - // more efficient (and way easier!) than the specialization of this function! - cur_ = polymorphicFind<C>( cur_, end_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Conversion constructor from different ConstCastIterator instances. - * - * \param it The foreign ConstCastIterator instance to be copied. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -template< typename Other > // The foreign constant cast iterator type -inline PtrVector<T,D,G>::ConstCastIterator<C>::ConstCastIterator( const ConstCastIterator<Other>& it ) - : cur_( it.base() ) // Pointer to the current memory location - , end_( it.stop() ) // Pointer to the element one past the last element in the element range -{ - static_assert(boost::is_base_of<C, T>::value && !boost::is_base_of<C, T>::value, "C has to be strictly derived from T"); - static_assert(boost::is_convertible<Other*, C*>::value, "Other must be convertible to C" ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Conversion constructor from CastIterator instances. - * - * \param it The foreign CastIterator instance to be copied. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -template< typename Other > // The foreign cast iterator type -inline PtrVector<T,D,G>::ConstCastIterator<C>::ConstCastIterator( const typename PtrVector<T,D,G>::WALBERLA_TEMPLATE CastIterator<Other>& it ) - : cur_( it.base() ) // Pointer to the current memory location - , end_( it.stop() ) // Pointer to the element one past the last element in the element range -{ - static_assert(boost::is_base_of<C, T>::value && !boost::is_base_of<C, T>::value, "C has to be strictly derived from T"); - static_assert(boost::is_convertible<Other*, C*>::value, "Other must be convertible to C" ); -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Pre-increment operator. - * - * \return Reference to the incremented cast iterator. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE ConstCastIterator<C>& - PtrVector<T,D,G>::ConstCastIterator<C>::operator++() -{ - // The polymorphicFind() function finds the next pointer to an object with dynamic type 'C' - // contained in the range [cur_+1,end). An equivalent code might look like this: - // - // while( ++cur_ != end_ && !dynamic_cast<const C*>( *cur_ ) ) {} - // - // However, the specialization of polymorphicFind() for special type combinations is much - // more efficient (and way easier!) than the specialization of this function! - cur_ = polymorphicFind<const C>( ++cur_, end_ ); - - return *this; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Post-increment operator. - * - * \return The incremented cast iterator. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE ConstCastIterator<C> - PtrVector<T,D,G>::ConstCastIterator<C>::operator++( int ) -{ - ConstCastIterator tmp( *this ); - - // The polymorphicFind() function finds the next pointer to an object with dynamic type 'C' - // contained in the range [cur_+1,end). An equivalent code might look like this: - // - // while( ++cur_ != end_ && !dynamic_cast<const C*>( *cur_ ) ) {} - // - // However, the specialization of polymorphicFind() for special type combinations is much - // more efficient (and way easier!) than the specialization of this function! - cur_ = polymorphicFind<const C>( ++cur_, end_ ); - - return tmp; -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// ACCESS OPERATORS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Returns a handle to the element at the current iterator position. - * - * \return Handle to the element at the current iterator position. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE ConstCastIterator<C>::PointerType - PtrVector<T,D,G>::ConstCastIterator<C>::operator*() const -{ - return static_cast<const C*>( *cur_ ); -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Direct access to the element at the current iterator position. - * - * \return Reference to the element at the current iterator position. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline typename PtrVector<T,D,G>::WALBERLA_TEMPLATE ConstCastIterator<C>::PointerType - PtrVector<T,D,G>::ConstCastIterator<C>::operator->() const -{ - return static_cast<const C*>( *cur_ ); -} -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Direct access to the current memory location of the constant cast iterator. - * - * \return Pointer to the current memory location. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline const typename PtrVector<T,D,G>::WALBERLA_TEMPLATE ConstCastIterator<C>::IteratorType& - PtrVector<T,D,G>::ConstCastIterator<C>::base() const -{ - return cur_; -} -//************************************************************************************************* - - -//************************************************************************************************* -/*!\brief Direct access to the final memory location of the constant cast iterator. - * - * \return Pointer to the final memory location. - */ -template< typename T // Type - , typename D // Deletion policy - , typename G > // Growth policy -template< typename C > // Cast type -inline const typename PtrVector<T,D,G>::WALBERLA_TEMPLATE ConstCastIterator<C>::IteratorType& - PtrVector<T,D,G>::ConstCastIterator<C>::stop() const -{ - return end_; -} -//************************************************************************************************* -} diff --git a/src/core/ptrvector/policies/ArrayDelete.h b/src/core/ptrvector/policies/ArrayDelete.h deleted file mode 100644 index bf6b605abc8d82a6612b9943aa596ba4fbb2bae6..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/policies/ArrayDelete.h +++ /dev/null @@ -1,88 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file ArrayDelete.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - - -//************************************************************************************************* -// Includes -//************************************************************************************************* - -#include <boost/checked_delete.hpp> - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Array-delete policy class. - * \ingroup util - * - * The ArrayDelete policy functor class applies an array delete operation to the given argument. - * Note that the array delete operation is NOT permitted for inclomplete types (i.e. declared - * but undefined data types). The attempt to apply an ArrayDelete functor to a pointer to an - * array of objects of incomplete type results in a compile time error! - */ -struct ArrayDelete -{ - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - template< typename Type > - inline void operator()( Type ptr ) const; - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Implementation of the array-delete policy. - * - * \param ptr The pointer to the array to be deleted. - * \return void - * - * This function applies an array delete operation to the given argument. Note that the array - * delete operation is NOT permitted for inclomplete types (i.e. declared but undefined data - * types). The attempt to use this function for a pointer to an array of objects of incomplete - * type results in a compile time error! - */ -template< typename Type > -inline void ArrayDelete::operator()( Type ptr ) const -{ - boost::checked_array_delete( ptr ); -} -//************************************************************************************************* - -} // namespace diff --git a/src/core/ptrvector/policies/ConstantGrowth.h b/src/core/ptrvector/policies/ConstantGrowth.h deleted file mode 100644 index 2d20250ca78ac865eac5a1d362c35258f28ddaf2..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/policies/ConstantGrowth.h +++ /dev/null @@ -1,94 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file ConstantGrowth.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - -#include <core/DataTypes.h> - -//************************************************************************************************* -// Includes -//************************************************************************************************* - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Constant growth policy class. - * \ingroup util - * - * The ConstantGrowth policy class implements a constant growth strategy. It can be customized - * for any purpose: the \a Growth template argument specifies the constant increase of the given - * size. - */ -template< size_t Growth > -struct ConstantGrowth -{ - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - inline size_t operator()( size_t oldSize, size_t minSize ) const; - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - -//************************************************************************************************* -/*! \cond internal */ -/*!\brief Specialization of the constant growth policy for 0 growth. - * \ingroup util - */ -template<> -struct ConstantGrowth<0>; -/*! \endcond */ -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Returns a new size depending on the given old size and the required minimum size. - * - * \param old The old size. - * \param minimum The required minimum size. - * \return The new size (at least the required minimum size). - */ -template< size_t Growth > -inline size_t ConstantGrowth<Growth>::operator()( size_t old, size_t minimum ) const -{ - const size_t needed( math::max<size_t>( old+Growth, minimum ) ); - return ( ( needed )?( 4 * ( (needed-1)/4+1 ) ):( 0 ) ); -} -//************************************************************************************************* - -} // namespace diff --git a/src/core/ptrvector/policies/LinearGrowth.h b/src/core/ptrvector/policies/LinearGrowth.h deleted file mode 100644 index 314b79380525dda287747d986be18a28a15010c4..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/policies/LinearGrowth.h +++ /dev/null @@ -1,106 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file LinearGrowth.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - - -//************************************************************************************************* -// Includes -//************************************************************************************************* - -#include <core/math/Utility.h> -#include <core/DataTypes.h> - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Linear growth policy class. - * \ingroup util - * - * The LinearGrowth policy class implements a linear growth strategy. It can be customized for - * any purpose: the \a Growth template argument specifies the factor of the size growth. - */ -template< size_t Growth > -struct LinearGrowth -{ - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - inline size_t operator()( size_t oldSize, size_t minSize ) const; - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - -//************************************************************************************************* -/*! \cond internal */ -/*!\brief Specialization of the linear growth policy for 0 growth. - * \ingroup util - */ -template<> -struct LinearGrowth<0>; -/*! \endcond */ -//************************************************************************************************* - - -//************************************************************************************************* -/*! \cond internal */ -/*!\brief Specialization of the linear growth policy for 1 growth. - * \ingroup util - */ -template<> -struct LinearGrowth<1>; -/*! \endcond */ -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Returns a new size depending on the given old size and the required minimum size. - * - * \param old The old size. - * \param minimum The required minimum size. - * \return The new size (at least the required minimum size). - */ -template< size_t Growth > -inline size_t LinearGrowth<Growth>::operator()( size_t old, size_t minimum ) const -{ - const size_t needed( math::max<size_t>( old*Growth, minimum ) ); - return ( ( needed )?( 4 * ( (needed-1)/4+1 ) ):( 0 ) ); -} -//************************************************************************************************* - -} // namespace diff --git a/src/core/ptrvector/policies/NoDelete.h b/src/core/ptrvector/policies/NoDelete.h deleted file mode 100644 index 14b24aae3c1b48891a0b39ed02be7366b7d6a978..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/policies/NoDelete.h +++ /dev/null @@ -1,69 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file NoDelete.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief No-delete policy class. - * \ingroup util - */ -struct NoDelete -{ - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - template< typename Type > - inline void operator()( const Type& ptr ) const; - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Implementation of the no-delete policy. - * - * \param ptr The pointer to delete. - * \return void - */ -template< typename Type > -inline void NoDelete::operator()( const Type& /*ptr*/ ) const -{} -//************************************************************************************************* - -} // namespace diff --git a/src/core/ptrvector/policies/OptimalGrowth.h b/src/core/ptrvector/policies/OptimalGrowth.h deleted file mode 100644 index 59abed396b4ac6dccb2a35077f49f085e465c3c7..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/policies/OptimalGrowth.h +++ /dev/null @@ -1,85 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file OptimalGrowth.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - - -//************************************************************************************************* -// Includes -//************************************************************************************************* - -#include <core/math/Utility.h> -#include <core/DataTypes.h> - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Optimal growth policy class. - * \ingroup util - * - * The OptimalGrowth policy class implements the optimal growth strategy suggested by Andrew - * Koenig for the std::vector class (see Andrew Koenig's column in the September 1998 issue of - * JOOP (Journal of Object-Oriented Programming), or the Dr. Dobb's article 'C++ Made Easier: - * How Vectors Grow', 2001). It applies an exponential growth strategy using a factor of 1.5 - * and additionally ensures that the sizes returns are always multiples of four. - */ -struct OptimalGrowth -{ - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - inline size_t operator()( size_t oldSize, size_t minSize ) const; - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Returns a new size depending on the given old size and the required minimum size. - * - * \param old The old size. - * \param minimum The required minimum size. - * \return The new size (at least the required minimum size). - */ -inline size_t OptimalGrowth::operator()( size_t old, size_t minimum ) const -{ - const size_t needed( math::max( static_cast<size_t>( real_c(old)*1.5 ), minimum ) ); - return ( ( needed )?( 4 * ( (needed-1)/4 + 1 ) ):( 0 ) ); -} -//************************************************************************************************* - -} // namespace diff --git a/src/core/ptrvector/policies/PtrDelete.h b/src/core/ptrvector/policies/PtrDelete.h deleted file mode 100644 index 1940275e2ece96848af8982873160b8bb41c7cd8..0000000000000000000000000000000000000000 --- a/src/core/ptrvector/policies/PtrDelete.h +++ /dev/null @@ -1,88 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file PtrDelete.h -//! \ingroup core -//! \author Klaus Iglberger -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#pragma once - - -//************************************************************************************************* -// Includes -//************************************************************************************************* - -#include <boost/checked_delete.hpp> - -namespace walberla { - -//================================================================================================= -// -// CLASS DEFINITION -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Pointer-delete policy class. - * \ingroup util - * - * The PtrDelete policy functor class applies a delete operation to the given argument. Note that - * the delete operation is NOT permitted for inclomplete types (i.e. declared but undefined data - * types). The attempt to apply a PtrDelete functor to a pointer to an object of incomplete type - * results in a compile time error! - */ -struct PtrDelete -{ - //**Utility functions*************************************************************************** - /*!\name Utility functions */ - //@{ - template< typename Type > - inline void operator()( Type ptr ) const; - //@} - //********************************************************************************************** -}; -//************************************************************************************************* - - - - -//================================================================================================= -// -// UTILITY FUNCTIONS -// -//================================================================================================= - -//************************************************************************************************* -/*!\brief Implementation of the pointer-delete policy. - * - * \param ptr The pointer to delete. - * \return void - * - * This function applies a standard delete operation to the given argument. Note that the delete - * operation is NOT permitted for inclomplete types (i.e. declared but undefined data types). The - * attempt to use this function for a pointer to an object of incomplete type results in a compile - * time error! - */ -template< typename Type > -inline void PtrDelete::operator()( Type ptr ) const -{ - boost::checked_delete( ptr ); -} -//************************************************************************************************* - -} // namespace diff --git a/src/pe/rigidbody/RigidBody.h b/src/pe/rigidbody/RigidBody.h index c457cb88ff4c9b289a854f5ef3f0dd1fad368271..fdebc3ff61424cbbdcc213d45bb2d473b6d07014 100644 --- a/src/pe/rigidbody/RigidBody.h +++ b/src/pe/rigidbody/RigidBody.h @@ -31,8 +31,6 @@ #include "core/NonCopyable.h" #include "core/DataTypes.h" -#include "core/ptrvector/PtrVector.h" -#include "core/ptrvector/policies/NoDelete.h" #include "core/debug/Debug.h" #include "core/logging/Logging.h" #include "core/math/AABB.h" diff --git a/src/pe/rigidbody/RigidBodyCastIterator.h b/src/pe/rigidbody/RigidBodyCastIterator.h index 22b148efff5627917b5516e5eaeb10c75b2541c2..7241baa63ce3b6eac5bafb20be5571016f992526 100644 --- a/src/pe/rigidbody/RigidBodyCastIterator.h +++ b/src/pe/rigidbody/RigidBodyCastIterator.h @@ -32,26 +32,9 @@ namespace pe { //************************************************************************************************* /*!\brief Dynamic cast iterator for polymorphic pointer vectors. - * \ingroup util * - * The CastIterator class is part of the PtrVector class and represent a forward iterator - * over all elements of type \a C contained in a range of elements of type \a T, where \a C - * is a type derived from \a T. - - \code - class A { ... }; - class B : public class A { ... }; - - PtrVector<A>::CastIterator<B> begin; - PtrVector<A>::CastIterator<B> end; - - // Loop over all elements of type B within the range [begin..end) - for( ; begin!=end; ++begin ) - ... - \endcode - - * \b Note: Using a CastIterator is computationally more expensive than using a standard - * iterator over all elements contained in the vector. + * The RigidBodyCastIterator is a forward iterator which only selects elements of type C. + * Dereferencing this iterator will implicitly cast to C. */ template <typename C> class RigidBodyCastIterator diff --git a/src/pe/rigidbody/RigidBodyIterator.h b/src/pe/rigidbody/RigidBodyIterator.h index a5401e3ba966d939c5f3cefba6cddf09f47bd20c..8e2be67d2f299dde26ebb313b4a17e4c22642301 100644 --- a/src/pe/rigidbody/RigidBodyIterator.h +++ b/src/pe/rigidbody/RigidBodyIterator.h @@ -32,55 +32,9 @@ namespace pe { //************************************************************************************************* /*!\brief Implementation of an iterator for pointer vectors. - * \ingroup util * - * The PtrIterator class follows the example of the random-access iterator classes of the STL. - * However, the focus of this iterator implementation is the use with (polymorphic) pointers. - * Since the physics engine makes heavy use of polymorphic pointers, this implementation eases - * the use of iterators over a range of pointers and improves the semantics on these pointers.\n - * - * In contrast to the STL iterators, the PtrIterator class slightly changes the meaning of the - * access operators. Consider the following example: - - \code - // Definition of class A - class A - { - public: - A( int i=0 ):i_(i) {} - - void set( int i ) { i_ = i; } - int get() const { return i_; } - - private: - int i_; - }; - - // Definition of a pointer vector for class A - typedef PtrVector<A> AVector; - - AVector vector; - AVector::Iterator it = vector.begin(); - - // The subscript operator returns a handle to the underlying object - A* a1 = it[0]; - - // The dereference operator returns a handle to the underlying object - A* a2 = *it; - - // The member access operator offers direct access to the underlying object - it->set( 2 ); - \endcode - - * The constant iterators (iterator over constant objects) prohibit the access to non-const - * member functions. Therefore the following operation results in a compile-time error: - - \code - AVector vector; - AVector::ConstIterator it = vector.begin(); - - it->set( 2 ); // Compile-time error! - \endcode + * This iterator implementation will automatically dereference twice at dereference calls! + * Therefore the return types are not unique_ptrs but RigidBodys. */ class RigidBodyIterator { diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt index 6885cf97661fa507cc240a45207b63a62aec4a43..ddb9907147e78dbba1d6a9d5fd07be6f2dc033e6 100644 --- a/tests/core/CMakeLists.txt +++ b/tests/core/CMakeLists.txt @@ -143,13 +143,6 @@ waLBerla_execute_test( NAME SetReductionTest27 COMMAND $<TARGET_FILE:SetReductio waLBerla_compile_test( FILES mpi/ProbeVsExtraMessage.cpp DEPENDS postprocessing) -############## -# ptrvector # -############## - -waLBerla_compile_test( NAME PtrVector FILES ptrvector/PtrVector.cpp ) -waLBerla_execute_test( NAME PtrVector ) - ############## # selectable # ############## diff --git a/tests/core/ptrvector/PtrVector.cpp b/tests/core/ptrvector/PtrVector.cpp deleted file mode 100644 index 8ae38bdeaff93a71ac5187d7c223f87670de0b7f..0000000000000000000000000000000000000000 --- a/tests/core/ptrvector/PtrVector.cpp +++ /dev/null @@ -1,98 +0,0 @@ -//====================================================================================================================== -// -// This file is part of waLBerla. waLBerla is free software: you can -// redistribute it and/or modify it under the terms of the GNU General Public -// License as published by the Free Software Foundation, either version 3 of -// the License, or (at your option) any later version. -// -// waLBerla is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. -// -// You should have received a copy of the GNU General Public License along -// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. -// -//! \file PtrVector.cpp -//! \ingroup field -//! \author Sebastian Eibl <sebastian.eibl@fau.de> -// -//====================================================================================================================== - -#include "core/ptrvector/PtrVector.h" -#include "core/ptrvector/policies/NoDelete.h" - -#include "core/debug/TestSubsystem.h" - -using namespace walberla; - -struct A{ - int c; - int val; - static int refCount; - - explicit A(int v = 0) : c(0), val(v) { ++refCount; } - virtual ~A(){ --refCount; } -}; - -struct B : public A { - B() : A(1) {} -}; - -struct C : public A { - C() : A(2) {} -}; - -int A::refCount = 0; - -int main( int /*argc*/, char** /*argv*/ ) -{ - debug::enterTestMode(); - { - auto a = new A(); - auto b = new B(); - auto c = new C(); - - WALBERLA_CHECK_EQUAL(A::refCount, 3); - - PtrVector<A, PtrDelete> vec; - vec.pushBack(a); - vec.pushBack(b); - vec.pushBack(c); - - WALBERLA_CHECK_EQUAL(A::refCount, 3); - - for (auto it = vec.begin(); it!=vec.end(); ++it){ - ++(it->c); - } - for (auto it = vec.begin<B>(); it!=vec.end<B>(); ++it){ - ++(it->c); - } - - WALBERLA_CHECK_EQUAL(a->c, 1); - WALBERLA_CHECK_EQUAL(b->c, 2); - - vec.erase(++vec.begin()); - int sum = 0; - for (auto it = vec.begin(); it!=vec.end(); ++it){ - sum += it->val; - } - - WALBERLA_CHECK_EQUAL(sum, 2); - } - WALBERLA_CHECK_EQUAL(A::refCount, 0); - - auto a = new A(); - auto b = new B(); - WALBERLA_CHECK_EQUAL(A::refCount, 2); - { - PtrVector<A, NoDelete> vec; - vec.pushBack(a); - vec.pushBack(b); - WALBERLA_CHECK_EQUAL(A::refCount, 2); - } - WALBERLA_CHECK_EQUAL(A::refCount, 2); - - delete a; - delete b; -}