Generated on Tue Apr 18 10:22:14 2017 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  *  Last modified:
00010  *     $Date: 2016-06-18 14:20:49 +0200 (Sat, 18 Jun 2016) $ by $Author: schulte $
00011  *     $Revision: 15118 $
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, class A>
00048   class DynamicArray {
00049   private:
00051     A& a;
00053     int n;
00055     T*  x;
00057     void resize(int n);
00058   public:
00060     DynamicArray(A& a0, int n = 32);
00062     DynamicArray(A& a0, unsigned int n);
00064     DynamicArray(const DynamicArray<T,A>& da);
00066     ~DynamicArray(void);
00067 
00069     const DynamicArray<T,A>& operator =(const DynamicArray<T,A>& da);
00070 
00072     T& operator [](int i);
00074     T& operator [](unsigned int i);
00076     const T& operator [](int i) const;
00078     const T& operator [](unsigned int i) const;
00079 
00081     operator T*(void);
00082   };
00083 
00084 
00085   template<class T, class A>
00086   forceinline
00087   DynamicArray<T,A>::DynamicArray(A& a0, int n0)
00088     : a(a0), n(n0), x(a.template alloc<T>(n)) {}
00089 
00090   template<class T, class A>
00091   forceinline
00092   DynamicArray<T,A>::DynamicArray(A& a0, unsigned int n0)
00093     : a(a0), n(static_cast<int>(n0)), x(a.template alloc<T>(n)) {}
00094 
00095   template<class T, class A>
00096   forceinline
00097   DynamicArray<T,A>::DynamicArray(const DynamicArray<T,A>& da)
00098     : a(da.a), n(da.n), x(a.template alloc<T>(n)) {
00099     (void) heap.copy<T>(x,da.x,n);
00100   }
00101 
00102   template<class T, class A>
00103   forceinline
00104   DynamicArray<T,A>::~DynamicArray(void) {
00105     a.free(x,n);
00106   }
00107 
00108   template<class T, class A>
00109   forceinline const DynamicArray<T,A>&
00110   DynamicArray<T,A>::operator =(const DynamicArray<T,A>& da) {
00111     if (this != &da) {
00112       if (n < da.n) {
00113         a.free(x,n); n = da.n; x = a.template alloc<T>(n);
00114       }
00115       (void) heap.copy(x,da.x,n);
00116     }
00117     return *this;
00118   }
00119 
00120   template<class T, class A>
00121   void
00122   DynamicArray<T,A>::resize(int i) {
00123     int m = std::max(i+1, (3*n)/2);
00124     x = a.realloc(x,n,m);
00125     n = m;
00126   }
00127 
00128   template<class T, class A>
00129   forceinline T&
00130   DynamicArray<T,A>::operator [](int i) {
00131     if (i >= n) resize(i);
00132     assert(n > i);
00133     return x[i];
00134   }
00135 
00136   template<class T, class A>
00137   forceinline T&
00138   DynamicArray<T,A>::operator [](unsigned int i) {
00139     return operator [](static_cast<int>(i));
00140   }
00141 
00142   template<class T, class A>
00143   forceinline const T&
00144   DynamicArray<T,A>::operator [](int i) const {
00145     assert(n > i);
00146     return x[i];
00147   }
00148 
00149   template<class T, class A>
00150   forceinline const T&
00151   DynamicArray<T,A>::operator [](unsigned int i) const {
00152     return operator [](static_cast<int>(i));
00153   }
00154 
00155   template<class T, class A>
00156   forceinline
00157   DynamicArray<T,A>::operator T*(void) {
00158     return x;
00159   }
00160 
00161 }}
00162 
00163 // STATISTICS: support-any