shared-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 * Guido Tack <tack@gecode.org> 00006 * 00007 * Contributing authors: 00008 * Gregory Crosswhite <gcross@phys.washington.edu> 00009 * 00010 * Copyright: 00011 * Gregory Crosswhite, 2011 00012 * Christian Schulte, 2003 00013 * Guido Tack, 2004 00014 * 00015 * Last modified: 00016 * $Date: 2011-01-27 13:03:05 +0100 (Thu, 27 Jan 2011) $ by $Author: schulte $ 00017 * $Revision: 11570 $ 00018 * 00019 * This file is part of Gecode, the generic constraint 00020 * development environment: 00021 * http://www.gecode.org 00022 * 00023 * Permission is hereby granted, free of charge, to any person obtaining 00024 * a copy of this software and associated documentation files (the 00025 * "Software"), to deal in the Software without restriction, including 00026 * without limitation the rights to use, copy, modify, merge, publish, 00027 * distribute, sublicense, and/or sell copies of the Software, and to 00028 * permit persons to whom the Software is furnished to do so, subject to 00029 * the following conditions: 00030 * 00031 * The above copyright notice and this permission notice shall be 00032 * included in all copies or substantial portions of the Software. 00033 * 00034 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00035 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00036 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00037 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00038 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00039 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00040 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00041 * 00042 */ 00043 00044 #include <cstdarg> 00045 #include <iostream> 00046 #include <sstream> 00047 00048 namespace Gecode { 00049 00057 template<class T> 00058 class SharedArray : public SharedHandle { 00059 protected: 00061 class SAO : public SharedHandle::Object { 00062 private: 00064 T* a; 00066 int n; 00067 public: 00069 SAO(int n); 00071 virtual SharedHandle::Object* copy(void) const; 00073 virtual ~SAO(void); 00074 00076 T& operator [](int i); 00078 const T& operator [](int i) const; 00079 00081 int size(void) const; 00082 }; 00083 public: 00085 00086 00087 typedef T value_type; 00089 typedef T& reference; 00091 typedef const T& const_reference; 00093 typedef T* pointer; 00095 typedef const T* const_pointer; 00097 typedef T* iterator; 00099 typedef const T* const_iterator; 00101 typedef std::reverse_iterator<T*> reverse_iterator; 00103 typedef std::reverse_iterator<const T*> const_reverse_iterator; 00105 00113 SharedArray(void); 00115 SharedArray(int n); 00123 void init(int n); 00125 SharedArray(const SharedArray& a); 00127 SharedArray(const ArgArrayBase<T>& a); 00128 00130 T& operator [](int i); 00132 const T& operator [](int i) const; 00133 00135 int size(void) const; 00136 00138 00139 00140 iterator begin(void); 00142 const_iterator begin(void) const; 00144 iterator end(void); 00146 const_iterator end(void) const; 00148 reverse_iterator rbegin(void); 00150 const_reverse_iterator rbegin(void) const; 00152 reverse_iterator rend(void); 00154 const_reverse_iterator rend(void) const; 00156 }; 00157 00162 template<class Char, class Traits, class T> 00163 std::basic_ostream<Char,Traits>& 00164 operator <<(std::basic_ostream<Char,Traits>& os, 00165 const SharedArray<T>& x); 00166 00167 00168 /* 00169 * Implementation 00170 * 00171 */ 00172 00173 /* 00174 * Shared arrays 00175 * 00176 */ 00177 template<class T> 00178 forceinline 00179 SharedArray<T>::SAO::SAO(int n0) : n(n0) { 00180 a = (n>0) ? heap.alloc<T>(n) : NULL; 00181 } 00182 00183 template<class T> 00184 SharedHandle::Object* 00185 SharedArray<T>::SAO::copy(void) const { 00186 SAO* o = new SAO(n); 00187 for (int i=n; i--;) 00188 o->a[i] = a[i]; 00189 return o; 00190 } 00191 00192 template<class T> 00193 SharedArray<T>::SAO::~SAO(void) { 00194 if (n>0) { 00195 heap.free<T>(a,n); 00196 } 00197 } 00198 00199 template<class T> 00200 forceinline T& 00201 SharedArray<T>::SAO::operator [](int i) { 00202 assert((i>=0) && (i<n)); 00203 return a[i]; 00204 } 00205 00206 template<class T> 00207 forceinline const T& 00208 SharedArray<T>::SAO::operator [](int i) const { 00209 assert((i>=0) && (i<n)); 00210 return a[i]; 00211 } 00212 00213 template<class T> 00214 forceinline int 00215 SharedArray<T>::SAO::size(void) const { 00216 return n; 00217 } 00218 00219 00220 00221 template<class T> 00222 forceinline 00223 SharedArray<T>::SharedArray(void) {} 00224 00225 template<class T> 00226 forceinline 00227 SharedArray<T>::SharedArray(int n) 00228 : SharedHandle(new SAO(n)) {} 00229 00230 template<class T> 00231 forceinline 00232 SharedArray<T>::SharedArray(const SharedArray<T>& sa) 00233 : SharedHandle(sa) {} 00234 00235 template<class T> 00236 forceinline void 00237 SharedArray<T>::init(int n) { 00238 assert(object() == NULL); 00239 object(new SAO(n)); 00240 } 00241 00242 template<class T> 00243 forceinline T& 00244 SharedArray<T>::operator [](int i) { 00245 assert(object() != NULL); 00246 return (*static_cast<SAO*>(object()))[i]; 00247 } 00248 00249 template<class T> 00250 forceinline const T& 00251 SharedArray<T>::operator [](int i) const { 00252 assert(object() != NULL); 00253 return (*static_cast<SAO*>(object()))[i]; 00254 } 00255 00256 template<class T> 00257 forceinline 00258 SharedArray<T>::SharedArray(const ArgArrayBase<T>& a) 00259 : SharedHandle(new SAO(a.size())) { 00260 for (int i=a.size(); i--; ) 00261 operator [](i)=a[i]; 00262 } 00263 00264 template<class T> 00265 forceinline int 00266 SharedArray<T>::size(void) const { 00267 assert(object() != NULL); 00268 return static_cast<SAO*>(object())->size(); 00269 } 00270 00271 template<class T> 00272 forceinline typename SharedArray<T>::iterator 00273 SharedArray<T>::begin(void) { 00274 assert(object() != NULL); 00275 return &(*static_cast<SAO*>(object()))[0]; 00276 } 00277 00278 template<class T> 00279 forceinline typename SharedArray<T>::const_iterator 00280 SharedArray<T>::begin(void) const { 00281 assert(object() != NULL); 00282 return &(*static_cast<SAO*>(object()))[0]; 00283 } 00284 00285 template<class T> 00286 forceinline typename SharedArray<T>::iterator 00287 SharedArray<T>::end(void) { 00288 assert(object() != NULL); 00289 return &(*static_cast<SAO*>(object()))[0] + size(); 00290 } 00291 00292 template<class T> 00293 forceinline typename SharedArray<T>::const_iterator 00294 SharedArray<T>::end(void) const { 00295 assert(object() != NULL); 00296 return &(*static_cast<SAO*>(object()))[0] + size(); 00297 } 00298 00299 template<class T> 00300 forceinline typename SharedArray<T>::reverse_iterator 00301 SharedArray<T>::rbegin(void) { 00302 assert(object() != NULL); 00303 return reverse_iterator(&(*static_cast<SAO*>(object()))[0] + size()); 00304 } 00305 00306 template<class T> 00307 forceinline typename SharedArray<T>::const_reverse_iterator 00308 SharedArray<T>::rbegin(void) const { 00309 assert(object() != NULL); 00310 return const_reverse_iterator(&(*static_cast<SAO*>(object()))[0] + size()); 00311 } 00312 00313 template<class T> 00314 forceinline typename SharedArray<T>::reverse_iterator 00315 SharedArray<T>::rend(void) { 00316 assert(object() != NULL); 00317 return reverse_iterator(&(*static_cast<SAO*>(object()))[0]); 00318 } 00319 00320 template<class T> 00321 forceinline typename SharedArray<T>::const_reverse_iterator 00322 SharedArray<T>::rend(void) const { 00323 assert(object() != NULL); 00324 return const_reverse_iterator(&(*static_cast<SAO*>(object()))[0]); 00325 } 00326 00327 template<class Char, class Traits, class T> 00328 std::basic_ostream<Char,Traits>& 00329 operator <<(std::basic_ostream<Char,Traits>& os, 00330 const SharedArray<T>& x) { 00331 std::basic_ostringstream<Char,Traits> s; 00332 s.copyfmt(os); s.width(0); 00333 s << '{'; 00334 if (x.size() > 0) { 00335 s << x[0]; 00336 for (int i=1; i<x.size(); i++) 00337 s << ", " << x[i]; 00338 } 00339 s << '}'; 00340 return os << s.str(); 00341 } 00342 00343 } 00344 00345 // STATISTICS: kernel-other