/* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2002 * * Last modified: * $Date: 2006-08-04 16:05:34 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $ * $Revision: 3514 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * See the file "LICENSE" for information on usage and * redistribution of this file, and for a * DISCLAIMER OF ALL WARRANTIES. * */ #ifndef __GECODE_SUPPORT_DYNAMICARRAY_HH__ #define __GECODE_SUPPORT_DYNAMICARRAY_HH__ #include "gecode/kernel.hh" #include #include namespace Gecode { namespace Support { /** * \brief Array with arbitrary number of elements * * Requires \code #include "gecode/support/dynamic-array.hh" \endcode * \ingroup FuncSupport */ template class DynamicArray { private: /// Size of array int n; /// Array elements T* x; /// Resize to at least \a n + 1 elements void resize(int n); public: /// Initialize with size \a m DynamicArray(int m = 32); /// Copy elements from array \a a DynamicArray(const DynamicArray& a); /// Release memory ~DynamicArray(void); /// Assign array (copy elements from \a a) const DynamicArray& operator =(const DynamicArray& a); /// Return element at position \a i (possibly resize) T& operator[](int i); /// Return element at position \a i const T& operator [](int) const; /// Cast in to pointer of type \a T operator T*(void); }; template forceinline DynamicArray::DynamicArray(int m) : n(m), x(Memory::bmalloc(n)) {} template forceinline DynamicArray::DynamicArray(const DynamicArray& a) : n(a.n), x(Memory::bmalloc(n)) { (void) Memory::bcopy(x,a.x,n); } template forceinline DynamicArray::~DynamicArray(void) { Memory::free(x); } template forceinline const DynamicArray& DynamicArray::operator =(const DynamicArray& a) { if (this != &a) { if (n < a.n) { Memory::free(x); n = a.n; x = Memory::bmalloc(n); } (void) Memory::bcopy(x,a.x,n); } return *this; } template void DynamicArray::resize(int i) { int m = std::max(i+1, (3*n)/2); x = Memory::brealloc(x,n,m); n = m; } template forceinline T& DynamicArray::operator [](int i) { if (i >= n) resize(i); assert(n > i); return x[i]; } template forceinline const T& DynamicArray::operator [](int i) const { assert(n > i); return x[i]; } template forceinline DynamicArray::operator T*(void) { return x; } }} #endif // STATISTICS: support-any