this repo has no description
at develop 4.4 kB view raw
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * 6 * Copyright: 7 * Christian Schulte, 2002 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34#include <algorithm> 35 36namespace Gecode { namespace Support { 37 38 /** 39 * \brief Array with arbitrary number of elements 40 * 41 * \ingroup FuncSupport 42 */ 43 template<class T, class A> 44 class DynamicArray { 45 private: 46 /// Memory allocator 47 A& a; 48 /// Size of array 49 int n; 50 /// Array elements 51 T* x; 52 /// Resize to at least \a n + 1 elements 53 void resize(int n); 54 public: 55 /// Initialize with size \a n 56 DynamicArray(A& a0, int n = 32); 57 /// Initialize with size \a n 58 DynamicArray(A& a0, unsigned int n); 59 /// Copy elements from array \a da 60 DynamicArray(const DynamicArray<T,A>& da); 61 /// Release memory 62 ~DynamicArray(void); 63 64 /// Assign array (copy elements from \a a) 65 const DynamicArray<T,A>& operator =(const DynamicArray<T,A>& da); 66 67 /// Return element at position \a i (possibly resize) 68 T& operator [](int i); 69 /// Return element at position \a i (possibly resize) 70 T& operator [](unsigned int i); 71 /// Return element at position \a i 72 const T& operator [](int i) const; 73 /// Return element at position \a i 74 const T& operator [](unsigned int i) const; 75 76 /// Cast in to pointer of type \a T 77 operator T*(void); 78 }; 79 80 81 template<class T, class A> 82 forceinline 83 DynamicArray<T,A>::DynamicArray(A& a0, int n0) 84 : a(a0), n(n0), x(a.template alloc<T>(n)) {} 85 86 template<class T, class A> 87 forceinline 88 DynamicArray<T,A>::DynamicArray(A& a0, unsigned int n0) 89 : a(a0), n(static_cast<int>(n0)), x(a.template alloc<T>(n)) {} 90 91 template<class T, class A> 92 forceinline 93 DynamicArray<T,A>::DynamicArray(const DynamicArray<T,A>& da) 94 : a(da.a), n(da.n), x(a.template alloc<T>(n)) { 95 (void) heap.copy<T>(x,da.x,n); 96 } 97 98 template<class T, class A> 99 forceinline 100 DynamicArray<T,A>::~DynamicArray(void) { 101 a.free(x,n); 102 } 103 104 template<class T, class A> 105 forceinline const DynamicArray<T,A>& 106 DynamicArray<T,A>::operator =(const DynamicArray<T,A>& da) { 107 if (this != &da) { 108 if (n < da.n) { 109 a.free(x,n); n = da.n; x = a.template alloc<T>(n); 110 } 111 (void) heap.copy(x,da.x,n); 112 } 113 return *this; 114 } 115 116 template<class T, class A> 117 void 118 DynamicArray<T,A>::resize(int i) { 119 int m = std::max(i+1, (3*n)/2); 120 x = a.realloc(x,n,m); 121 n = m; 122 } 123 124 template<class T, class A> 125 forceinline T& 126 DynamicArray<T,A>::operator [](int i) { 127 if (i >= n) resize(i); 128 assert(n > i); 129 return x[i]; 130 } 131 132 template<class T, class A> 133 forceinline T& 134 DynamicArray<T,A>::operator [](unsigned int i) { 135 return operator [](static_cast<int>(i)); 136 } 137 138 template<class T, class A> 139 forceinline const T& 140 DynamicArray<T,A>::operator [](int i) const { 141 assert(n > i); 142 return x[i]; 143 } 144 145 template<class T, class A> 146 forceinline const T& 147 DynamicArray<T,A>::operator [](unsigned int i) const { 148 return operator [](static_cast<int>(i)); 149 } 150 151 template<class T, class A> 152 forceinline 153 DynamicArray<T,A>::operator T*(void) { 154 return x; 155 } 156 157}} 158 159// STATISTICS: support-any