Generated on Wed Nov 1 15:04:48 2006 for Gecode by doxygen 1.4.5

dynamic-array.hh

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2002
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:05:34 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3514 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #ifndef __GECODE_SUPPORT_DYNAMICARRAY_HH__
00023 #define __GECODE_SUPPORT_DYNAMICARRAY_HH__
00024 
00025 #include "gecode/kernel.hh"
00026 
00027 #include <algorithm>
00028 #include <cassert>
00029 
00030 namespace Gecode { namespace Support {
00031 
00038   template <class T>
00039   class DynamicArray {
00040   private:
00042     int n;
00044     T*  x;
00046     void resize(int n);
00047   public:
00049     DynamicArray(int m = 32);
00051     DynamicArray(const DynamicArray<T>& a);
00053     ~DynamicArray(void);
00054 
00056     const DynamicArray<T>& operator =(const DynamicArray<T>& a);
00057 
00059     T& operator[](int i);
00061     const T& operator [](int) const;
00062 
00064     operator T*(void);
00065   };
00066 
00067 
00068   template <class T>
00069   forceinline
00070   DynamicArray<T>::DynamicArray(int m)
00071     : n(m), x(Memory::bmalloc<T>(n)) {}
00072 
00073   template <class T>
00074   forceinline
00075   DynamicArray<T>::DynamicArray(const DynamicArray<T>& a)
00076     : n(a.n), x(Memory::bmalloc<T>(n)) {
00077     (void) Memory::bcopy<T>(x,a.x,n);
00078   }
00079 
00080   template <class T>
00081   forceinline
00082   DynamicArray<T>::~DynamicArray(void) {
00083     Memory::free(x);
00084   }
00085 
00086   template <class T>
00087   forceinline const DynamicArray<T>&
00088   DynamicArray<T>::operator =(const DynamicArray<T>& a) {
00089     if (this != &a) {
00090       if (n < a.n) {
00091         Memory::free(x); n = a.n; x = Memory::bmalloc<T>(n);
00092       }
00093       (void) Memory::bcopy(x,a.x,n);
00094     }
00095     return *this;
00096   }
00097 
00098   template <class T>
00099   void
00100   DynamicArray<T>::resize(int i) {
00101     int m = std::max(i+1, (3*n)/2);
00102     x = Memory::brealloc(x,n,m);
00103     n = m;
00104   }
00105 
00106   template <class T>
00107   forceinline T&
00108   DynamicArray<T>::operator [](int i) {
00109     if (i >= n) resize(i);
00110     assert(n > i);
00111     return x[i];
00112   }
00113 
00114   template <class T>
00115   forceinline const T&
00116   DynamicArray<T>::operator [](int i) const {
00117     assert(n > i);
00118     return x[i];
00119   }
00120 
00121   template <class T>
00122   forceinline
00123   DynamicArray<T>::operator T*(void) {
00124     return x;
00125   }
00126 
00127 }}
00128 
00129 #endif
00130 
00131 // STATISTICS: support-any