Generated on Mon Aug 25 11:35:40 2008 for Gecode by doxygen 1.5.6

shared-array.icc

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: 2007-11-13 21:33:44 +0100 (Tue, 13 Nov 2007) $ by $Author: schulte $
00013  *     $Revision: 5290 $
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 
00043 namespace Gecode {
00044 
00052   template <class T>
00053   class SharedArray : public SharedHandle {
00054   protected:
00056     class SAO : public SharedHandle::Object {
00057     private:
00059       T*  a;
00061       int n;
00062     public:
00064       SAO(int n);
00066       virtual SharedHandle::Object* copy(void) const;
00068       virtual ~SAO(void);
00069       
00071       T& operator[](int i);
00073       const T& operator[](int i) const;
00074       
00076       int size(void) const;
00077     };
00078   public:
00086     SharedArray(void);
00088     SharedArray(int n);
00096     void init(int n);
00098     SharedArray(const SharedArray& a);
00099 
00101     T& operator[](int i);
00103     const T& operator[](int i) const;
00104 
00106     int size(void) const;
00107     
00109     Reflection::Arg* spec(Reflection::VarMap& vm) const;
00110     
00112     SharedArray(Reflection::VarMap& vm, Reflection::Arg* arg);
00113     
00114   };
00115 
00116 }
00117 
00122 template<class T>
00123 std::ostream& operator<<(std::ostream& os, const Gecode::SharedArray<T>& x);
00124 
00125 namespace Gecode {
00126 
00127   /*
00128    * Implementation
00129    *
00130    */
00131 
00132   /*
00133    * Shared arrays
00134    *
00135    */
00136   template <class T>
00137   forceinline
00138   SharedArray<T>::SAO::SAO(int n0) : n(n0) {
00139     a = (n>0) ? static_cast<T*>(Memory::malloc(sizeof(T)*n)) : NULL;
00140   }
00141 
00142   template <class T>
00143   SharedHandle::Object*
00144   SharedArray<T>::SAO::copy(void) const {
00145     SAO* o = new SAO(n);
00146     for (int i=n; i--;)
00147       new (&(o->a[i])) T(a[i]);
00148     return o;
00149   }
00150 
00151   template <class T>
00152   SharedArray<T>::SAO::~SAO(void) {
00153     if (n>0) {
00154       for (int i=n; i--;)
00155         a[i].~T();
00156       Memory::free(a);
00157     }
00158   }
00159 
00160   template <class T>
00161   forceinline T&
00162   SharedArray<T>::SAO::operator[](int i) {
00163     assert((i>=0) && (i<n));
00164     return a[i];
00165   }
00166 
00167   template <class T>
00168   forceinline const T&
00169   SharedArray<T>::SAO::operator[](int i) const {
00170     assert((i>=0) && (i<n));
00171     return a[i];
00172   }
00173 
00174   template <class T>
00175   forceinline int
00176   SharedArray<T>::SAO::size(void) const {
00177     return n;
00178   }
00179 
00180 
00181 
00182   template <class T>
00183   forceinline
00184   SharedArray<T>::SharedArray(void) {}
00185 
00186   template <class T>
00187   forceinline
00188   SharedArray<T>::SharedArray(int n) 
00189     : SharedHandle(new SAO(n)) {}
00190 
00191   template <class T>
00192   forceinline
00193   SharedArray<T>::SharedArray(const SharedArray<T>& sa) 
00194     : SharedHandle(sa) {}
00195 
00196   template <class T>
00197   forceinline void
00198   SharedArray<T>::init(int n) {
00199     assert(object() == NULL);
00200     object(new SAO(n));
00201   }
00202 
00203   template <class T>
00204   forceinline T&
00205   SharedArray<T>::operator[](int i) {
00206     assert(object() != NULL);
00207     return (*static_cast<SAO*>(object()))[i];
00208   }
00209 
00210   template <class T>
00211   forceinline const T&
00212   SharedArray<T>::operator[](int i) const {
00213     assert(object() != NULL);
00214     return (*static_cast<SAO*>(object()))[i];
00215   }
00216 
00217   template <class T>
00218   forceinline int
00219   SharedArray<T>::size(void) const {
00220     assert(object() != NULL);
00221     return static_cast<SAO*>(object())->size();
00222   }
00223 
00225   template <class T>
00226   class SharedArraySerialization {
00227   public:
00228     static Reflection::Arg* t(const T& t) { return NULL; }
00229     static T t(Reflection::Arg* arg) { T tt; return tt; }
00230   };
00231 
00233   template <>
00234   class SharedArraySerialization<int> {
00235   public:
00236     static Reflection::Arg* t(const int& t) {
00237       return Reflection::Arg::newInt(t);
00238     }
00239     static int t(Reflection::Arg* arg) {
00240       return arg->toInt();
00241     }
00242   };
00243 
00244   template <class T>
00245   Reflection::Arg*
00246   SharedArray<T>::spec(Reflection::VarMap& vm) const {
00247     int sharedIndex = vm.getSharedIndex(object());
00248     if (sharedIndex >= 0)
00249       return Reflection::Arg::newSharedReference(sharedIndex);
00250     Reflection::ArrayArg* a = Reflection::Arg::newArray(size());
00251     for (int i=size(); i--; )
00252       (*a)[i] = SharedArraySerialization<T>::t((*this)[i]);
00253     vm.putMasterObject(object());
00254     return Reflection::Arg::newSharedObject(a);
00255   }
00256   
00257   template <class T>
00258   SharedArray<T>::SharedArray(Reflection::VarMap& vm,
00259                               Reflection::Arg* arg) {
00260     if (arg->isSharedReference()) {
00261       object(static_cast<SAO*>(vm.getSharedObject(arg->toSharedReference())));
00262     } else {
00263       Reflection::ArrayArg* a = arg->toSharedObject()->toArray();
00264       object(new SAO(a->size()));
00265       for (int i=a->size(); i--; ) {
00266         (*this)[i] = SharedArraySerialization<T>::t((*a)[i]);
00267       }
00268       vm.putMasterObject(object());
00269     }
00270   }
00271 
00272 }
00273 
00274 template<class T>
00275 std::ostream& 
00276 operator<<(std::ostream& os, const Gecode::SharedArray<T>& x) {
00277   os << '{';
00278   if (x.size() > 0) {
00279     os << x[0];
00280     for (int i=1; i<x.size(); i++)
00281       os << ", " << x[i];
00282   }
00283   return os << '}';
00284 }
00285 
00286 // STATISTICS: kernel-other