Generated on Thu Apr 11 13:59:20 2019 for Gecode by doxygen 1.6.3

dynamic-array.hpp

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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <algorithm>
00035 
00036 namespace Gecode { namespace Support {
00037 
00043   template<class T, class A>
00044   class DynamicArray {
00045   private:
00047     A& a;
00049     int n;
00051     T*  x;
00053     void resize(int n);
00054   public:
00056     DynamicArray(A& a0, int n = 32);
00058     DynamicArray(A& a0, unsigned int n);
00060     DynamicArray(const DynamicArray<T,A>& da);
00062     ~DynamicArray(void);
00063 
00065     const DynamicArray<T,A>& operator =(const DynamicArray<T,A>& da);
00066 
00068     T& operator [](int i);
00070     T& operator [](unsigned int i);
00072     const T& operator [](int i) const;
00074     const T& operator [](unsigned int i) const;
00075 
00077     operator T*(void);
00078   };
00079 
00080 
00081   template<class T, class A>
00082   forceinline
00083   DynamicArray<T,A>::DynamicArray(A& a0, int n0)
00084     : a(a0), n(n0), x(a.template alloc<T>(n)) {}
00085 
00086   template<class T, class A>
00087   forceinline
00088   DynamicArray<T,A>::DynamicArray(A& a0, unsigned int n0)
00089     : a(a0), n(static_cast<int>(n0)), x(a.template alloc<T>(n)) {}
00090 
00091   template<class T, class A>
00092   forceinline
00093   DynamicArray<T,A>::DynamicArray(const DynamicArray<T,A>& da)
00094     : a(da.a), n(da.n), x(a.template alloc<T>(n)) {
00095     (void) heap.copy<T>(x,da.x,n);
00096   }
00097 
00098   template<class T, class A>
00099   forceinline
00100   DynamicArray<T,A>::~DynamicArray(void) {
00101     a.free(x,n);
00102   }
00103 
00104   template<class T, class A>
00105   forceinline const DynamicArray<T,A>&
00106   DynamicArray<T,A>::operator =(const DynamicArray<T,A>& da) {
00107     if (this != &da) {
00108       if (n < da.n) {
00109         a.free(x,n); n = da.n; x = a.template alloc<T>(n);
00110       }
00111       (void) heap.copy(x,da.x,n);
00112     }
00113     return *this;
00114   }
00115 
00116   template<class T, class A>
00117   void
00118   DynamicArray<T,A>::resize(int i) {
00119     int m = std::max(i+1, (3*n)/2);
00120     x = a.realloc(x,n,m);
00121     n = m;
00122   }
00123 
00124   template<class T, class A>
00125   forceinline T&
00126   DynamicArray<T,A>::operator [](int i) {
00127     if (i >= n) resize(i);
00128     assert(n > i);
00129     return x[i];
00130   }
00131 
00132   template<class T, class A>
00133   forceinline T&
00134   DynamicArray<T,A>::operator [](unsigned int i) {
00135     return operator [](static_cast<int>(i));
00136   }
00137 
00138   template<class T, class A>
00139   forceinline const T&
00140   DynamicArray<T,A>::operator [](int i) const {
00141     assert(n > i);
00142     return x[i];
00143   }
00144 
00145   template<class T, class A>
00146   forceinline const T&
00147   DynamicArray<T,A>::operator [](unsigned int i) const {
00148     return operator [](static_cast<int>(i));
00149   }
00150 
00151   template<class T, class A>
00152   forceinline
00153   DynamicArray<T,A>::operator T*(void) {
00154     return x;
00155   }
00156 
00157 }}
00158 
00159 // STATISTICS: support-any