Generated on Mon Aug 25 11:35:44 2008 for Gecode by doxygen 1.5.6

dynamic-array.icc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2002
00008  *
00009  *  Last modified:
00010  *     $Date: 2007-09-11 14:20:32 +0200 (Tue, 11 Sep 2007) $ by $Author: schulte $
00011  *     $Revision: 4971 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include <algorithm>
00039 
00040 namespace Gecode { namespace Support {
00041 
00047   template <class T>
00048   class DynamicArray {
00049   private:
00051     int n;
00053     T*  x;
00055     void resize(int n);
00056   public:
00058     DynamicArray(int m = 32);
00060     DynamicArray(const DynamicArray<T>& a);
00062     ~DynamicArray(void);
00063 
00065     const DynamicArray<T>& operator =(const DynamicArray<T>& a);
00066 
00068     T& operator[](int i);
00070     const T& operator [](int) const;
00071 
00073     operator T*(void);
00074   };
00075 
00076 
00077   template <class T>
00078   forceinline
00079   DynamicArray<T>::DynamicArray(int m)
00080     : n(m), x(Memory::bmalloc<T>(static_cast<size_t>(n))) {}
00081 
00082   template <class T>
00083   forceinline
00084   DynamicArray<T>::DynamicArray(const DynamicArray<T>& a)
00085     : n(a.n), x(Memory::bmalloc<T>(n)) {
00086     (void) Memory::bcopy<T>(x,a.x,n);
00087   }
00088 
00089   template <class T>
00090   forceinline
00091   DynamicArray<T>::~DynamicArray(void) {
00092     Memory::free(x);
00093   }
00094 
00095   template <class T>
00096   forceinline const DynamicArray<T>&
00097   DynamicArray<T>::operator =(const DynamicArray<T>& a) {
00098     if (this != &a) {
00099       if (n < a.n) {
00100         Memory::free(x); n = a.n; x = Memory::bmalloc<T>(n);
00101       }
00102       (void) Memory::bcopy(x,a.x,n);
00103     }
00104     return *this;
00105   }
00106 
00107   template <class T>
00108   void
00109   DynamicArray<T>::resize(int i) {
00110     int m = std::max(i+1, (3*n)/2);
00111     x = Memory::brealloc(x,static_cast<size_t>(n),static_cast<size_t>(m));
00112     n = m;
00113   }
00114 
00115   template <class T>
00116   forceinline T&
00117   DynamicArray<T>::operator [](int i) {
00118     if (i >= n) resize(i);
00119     assert(n > i);
00120     return x[i];
00121   }
00122 
00123   template <class T>
00124   forceinline const T&
00125   DynamicArray<T>::operator [](int i) const {
00126     assert(n > i);
00127     return x[i];
00128   }
00129 
00130   template <class T>
00131   forceinline
00132   DynamicArray<T>::operator T*(void) {
00133     return x;
00134   }
00135 
00136 }}
00137 
00138 // STATISTICS: support-any