Generated on Mon Jul 6 18:09:16 2009 for Gecode by doxygen 1.5.9

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-02-05 11:48:53 +0100 (Thu, 05 Feb 2009) $ by $Author: schulte $
00013  *     $Revision: 8155 $
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);
00100 
00102     T& operator [](int i);
00104     const T& operator [](int i) const;
00105 
00107     int size(void) const;
00108   };
00109 
00114   template<class Char, class Traits, class T>
00115   std::basic_ostream<Char,Traits>&
00116   operator <<(std::basic_ostream<Char,Traits>& os,
00117              const SharedArray<T>& x);
00118 
00119 
00120   /*
00121    * Implementation
00122    *
00123    */
00124 
00125   /*
00126    * Shared arrays
00127    *
00128    */
00129   template <class T>
00130   forceinline
00131   SharedArray<T>::SAO::SAO(int n0) : n(n0) {
00132     a = (n>0) ? heap.alloc<T>(n) : NULL;
00133   }
00134 
00135   template <class T>
00136   SharedHandle::Object*
00137   SharedArray<T>::SAO::copy(void) const {
00138     SAO* o = new SAO(n);
00139     for (int i=n; i--;)
00140       o->a[i] = a[i];
00141     return o;
00142   }
00143 
00144   template <class T>
00145   SharedArray<T>::SAO::~SAO(void) {
00146     if (n>0) {
00147       heap.free<T>(a,n);
00148     }
00149   }
00150 
00151   template <class T>
00152   forceinline T&
00153   SharedArray<T>::SAO::operator [](int i) {
00154     assert((i>=0) && (i<n));
00155     return a[i];
00156   }
00157 
00158   template <class T>
00159   forceinline const T&
00160   SharedArray<T>::SAO::operator [](int i) const {
00161     assert((i>=0) && (i<n));
00162     return a[i];
00163   }
00164 
00165   template <class T>
00166   forceinline int
00167   SharedArray<T>::SAO::size(void) const {
00168     return n;
00169   }
00170 
00171 
00172 
00173   template <class T>
00174   forceinline
00175   SharedArray<T>::SharedArray(void) {}
00176 
00177   template <class T>
00178   forceinline
00179   SharedArray<T>::SharedArray(int n)
00180     : SharedHandle(new SAO(n)) {}
00181 
00182   template <class T>
00183   forceinline
00184   SharedArray<T>::SharedArray(const SharedArray<T>& sa)
00185     : SharedHandle(sa) {}
00186 
00187   template <class T>
00188   forceinline void
00189   SharedArray<T>::init(int n) {
00190     assert(object() == NULL);
00191     object(new SAO(n));
00192   }
00193 
00194   template <class T>
00195   forceinline T&
00196   SharedArray<T>::operator [](int i) {
00197     assert(object() != NULL);
00198     return (*static_cast<SAO*>(object()))[i];
00199   }
00200 
00201   template <class T>
00202   forceinline const T&
00203   SharedArray<T>::operator [](int i) const {
00204     assert(object() != NULL);
00205     return (*static_cast<SAO*>(object()))[i];
00206   }
00207 
00208   template <class T>
00209   forceinline int
00210   SharedArray<T>::size(void) const {
00211     assert(object() != NULL);
00212     return static_cast<SAO*>(object())->size();
00213   }
00214 
00215   template<class Char, class Traits, class T>
00216   std::basic_ostream<Char,Traits>&
00217   operator <<(std::basic_ostream<Char,Traits>& os,
00218              const SharedArray<T>& x) {
00219     std::basic_ostringstream<Char,Traits> s;
00220     s.copyfmt(os); s.width(0);
00221     s << '{';
00222     if (x.size() > 0) {
00223       s << x[0];
00224       for (int i=1; i<x.size(); i++)
00225         s << ", " << x[i];
00226     }
00227     s << '}';
00228     return os << s.str();
00229   }
00230 
00231 }
00232 
00233 // STATISTICS: kernel-other