Generated on Tue Jul 27 2010 21:59:17 for Gecode by doxygen 1.7.1

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  *  Copyright:
00008  *     Christian Schulte, 2003
00009  *     Guido Tack, 2004
00010  *
00011  *  Last modified:
00012  *     $Date: 2009-11-03 11:29:41 +0100 (Tue, 03 Nov 2009) $ by $Author: schulte $
00013  *     $Revision: 10026 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include <cstdarg>
00041 #include <iostream>
00042 #include <sstream>
00043 
00044 namespace Gecode {
00045 
00053   template<class T>
00054   class SharedArray : public SharedHandle {
00055   protected:
00057     class SAO : public SharedHandle::Object {
00058     private:
00060       T*  a;
00062       int n;
00063     public:
00065       SAO(int n);
00067       virtual SharedHandle::Object* copy(void) const;
00069       virtual ~SAO(void);
00070 
00072       T& operator [](int i);
00074       const T& operator [](int i) const;
00075 
00077       int size(void) const;
00078     };
00079   public:
00087     SharedArray(void);
00089     SharedArray(int n);
00097     void init(int n);
00099     SharedArray(const SharedArray& a);
00101     SharedArray(const ArgArrayBase<T>& a);
00102 
00104     T& operator [](int i);
00106     const T& operator [](int i) const;
00107 
00109     int size(void) const;
00110   };
00111 
00116   template<class Char, class Traits, class T>
00117   std::basic_ostream<Char,Traits>&
00118   operator <<(std::basic_ostream<Char,Traits>& os,
00119              const SharedArray<T>& x);
00120 
00121 
00122   /*
00123    * Implementation
00124    *
00125    */
00126 
00127   /*
00128    * Shared arrays
00129    *
00130    */
00131   template<class T>
00132   forceinline
00133   SharedArray<T>::SAO::SAO(int n0) : n(n0) {
00134     a = (n>0) ? heap.alloc<T>(n) : NULL;
00135   }
00136 
00137   template<class T>
00138   SharedHandle::Object*
00139   SharedArray<T>::SAO::copy(void) const {
00140     SAO* o = new SAO(n);
00141     for (int i=n; i--;)
00142       o->a[i] = a[i];
00143     return o;
00144   }
00145 
00146   template<class T>
00147   SharedArray<T>::SAO::~SAO(void) {
00148     if (n>0) {
00149       heap.free<T>(a,n);
00150     }
00151   }
00152 
00153   template<class T>
00154   forceinline T&
00155   SharedArray<T>::SAO::operator [](int i) {
00156     assert((i>=0) && (i<n));
00157     return a[i];
00158   }
00159 
00160   template<class T>
00161   forceinline const T&
00162   SharedArray<T>::SAO::operator [](int i) const {
00163     assert((i>=0) && (i<n));
00164     return a[i];
00165   }
00166 
00167   template<class T>
00168   forceinline int
00169   SharedArray<T>::SAO::size(void) const {
00170     return n;
00171   }
00172 
00173 
00174 
00175   template<class T>
00176   forceinline
00177   SharedArray<T>::SharedArray(void) {}
00178 
00179   template<class T>
00180   forceinline
00181   SharedArray<T>::SharedArray(int n)
00182     : SharedHandle(new SAO(n)) {}
00183 
00184   template<class T>
00185   forceinline
00186   SharedArray<T>::SharedArray(const SharedArray<T>& sa)
00187     : SharedHandle(sa) {}
00188 
00189   template<class T>
00190   forceinline void
00191   SharedArray<T>::init(int n) {
00192     assert(object() == NULL);
00193     object(new SAO(n));
00194   }
00195 
00196   template<class T>
00197   forceinline T&
00198   SharedArray<T>::operator [](int i) {
00199     assert(object() != NULL);
00200     return (*static_cast<SAO*>(object()))[i];
00201   }
00202 
00203   template<class T>
00204   forceinline const T&
00205   SharedArray<T>::operator [](int i) const {
00206     assert(object() != NULL);
00207     return (*static_cast<SAO*>(object()))[i];
00208   }
00209 
00210   template<class T>
00211   forceinline
00212   SharedArray<T>::SharedArray(const ArgArrayBase<T>& a)
00213     : SharedHandle(new SAO(a.size())) {
00214     for (int i=a.size(); i--; )
00215       operator [](i)=a[i];
00216   }
00217 
00218   template<class T>
00219   forceinline int
00220   SharedArray<T>::size(void) const {
00221     assert(object() != NULL);
00222     return static_cast<SAO*>(object())->size();
00223   }
00224 
00225   template<class Char, class Traits, class T>
00226   std::basic_ostream<Char,Traits>&
00227   operator <<(std::basic_ostream<Char,Traits>& os,
00228              const SharedArray<T>& x) {
00229     std::basic_ostringstream<Char,Traits> s;
00230     s.copyfmt(os); s.width(0);
00231     s << '{';
00232     if (x.size() > 0) {
00233       s << x[0];
00234       for (int i=1; i<x.size(); i++)
00235         s << ", " << x[i];
00236     }
00237     s << '}';
00238     return os << s.str();
00239   }
00240 
00241 }
00242 
00243 // STATISTICS: kernel-other