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: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $ 00011 * $Revision: 9692 $ 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(const DynamicArray<T,A>& da); 00064 ~DynamicArray(void); 00065 00067 const DynamicArray<T,A>& operator =(const DynamicArray<T,A>& da); 00068 00070 T& operator [](int i); 00072 const T& operator [](int) const; 00073 00075 operator T*(void); 00076 }; 00077 00078 00079 template<class T, class A> 00080 forceinline 00081 DynamicArray<T,A>::DynamicArray(A& a0, int n0) 00082 : a(a0), n(n0), x(a.template alloc<T>(n)) {} 00083 00084 template<class T, class A> 00085 forceinline 00086 DynamicArray<T,A>::DynamicArray(const DynamicArray<T,A>& da) 00087 : a(da.a), n(da.n), x(a.template alloc<T>(n)) { 00088 (void) heap.copy<T>(x,da.x,n); 00089 } 00090 00091 template<class T, class A> 00092 forceinline 00093 DynamicArray<T,A>::~DynamicArray(void) { 00094 a.free(x,n); 00095 } 00096 00097 template<class T, class A> 00098 forceinline const DynamicArray<T,A>& 00099 DynamicArray<T,A>::operator =(const DynamicArray<T,A>& da) { 00100 if (this != &da) { 00101 if (n < da.n) { 00102 a.free(x,n); n = da.n; x = a.template alloc<T>(n); 00103 } 00104 (void) heap.copy(x,da.x,n); 00105 } 00106 return *this; 00107 } 00108 00109 template<class T, class A> 00110 void 00111 DynamicArray<T,A>::resize(int i) { 00112 int m = std::max(i+1, (3*n)/2); 00113 x = a.realloc(x,n,m); 00114 n = m; 00115 } 00116 00117 template<class T, class A> 00118 forceinline T& 00119 DynamicArray<T,A>::operator [](int i) { 00120 if (i >= n) resize(i); 00121 assert(n > i); 00122 return x[i]; 00123 } 00124 00125 template<class T, class A> 00126 forceinline const T& 00127 DynamicArray<T,A>::operator [](int i) const { 00128 assert(n > i); 00129 return x[i]; 00130 } 00131 00132 template<class T, class A> 00133 forceinline 00134 DynamicArray<T,A>::operator T*(void) { 00135 return x; 00136 } 00137 00138 }} 00139 00140 // STATISTICS: support-any