Generated on Thu Apr 11 13:59:15 2019 for Gecode by doxygen 1.6.3

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  *  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 <iostream>
00041 #include <sstream>
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 ~SAO(void);
00067 
00069       T& operator [](int i);
00071       const T& operator [](int i) const;
00072 
00074       int size(void) const;
00075 
00077       T* begin(void);
00079       const T* begin(void) const;
00081       T* end(void);
00083       const T* end(void) const;
00084     };
00085   public:
00087 
00088 
00089     typedef T value_type;
00091     typedef T& reference;
00093     typedef const T& const_reference;
00095     typedef T* pointer;
00097     typedef const T* const_pointer;
00099     typedef T* iterator;
00101     typedef const T* const_iterator;
00103     typedef std::reverse_iterator<T*> reverse_iterator;
00105     typedef std::reverse_iterator<const T*> const_reverse_iterator;
00107 
00115     SharedArray(void);
00117     SharedArray(int n);
00125     void init(int n);
00127     SharedArray(const SharedArray& a);
00129     SharedArray(const ArgArrayBase<T>& a);
00130 
00132     T& operator [](int i);
00134     const T& operator [](int i) const;
00135 
00137     int size(void) const;
00138 
00140     bool operator ==(const SharedArray<T>& sa) const;
00141 
00143 
00144 
00145     iterator begin(void);
00147     const_iterator begin(void) const;
00149     iterator end(void);
00151     const_iterator end(void) const;
00153     reverse_iterator rbegin(void);
00155     const_reverse_iterator rbegin(void) const;
00157     reverse_iterator rend(void);
00159     const_reverse_iterator rend(void) const;
00161   };
00162 
00167   template<class Char, class Traits, class T>
00168   std::basic_ostream<Char,Traits>&
00169   operator <<(std::basic_ostream<Char,Traits>& os,
00170              const SharedArray<T>& x);
00171 
00172 
00173   /*
00174    * Implementation
00175    *
00176    */
00177 
00178   /*
00179    * Shared arrays
00180    *
00181    */
00182   template<class T>
00183   forceinline
00184   SharedArray<T>::SAO::SAO(int n0) : n(n0) {
00185     a = (n>0) ? heap.alloc<T>(n) : NULL;
00186   }
00187 
00188   template<class T>
00189   SharedArray<T>::SAO::~SAO(void) {
00190     if (n>0) {
00191       heap.free<T>(a,n);
00192     }
00193   }
00194 
00195   template<class T>
00196   forceinline T&
00197   SharedArray<T>::SAO::operator [](int i) {
00198     assert((i>=0) && (i<n));
00199     return a[i];
00200   }
00201 
00202   template<class T>
00203   forceinline const T&
00204   SharedArray<T>::SAO::operator [](int i) const {
00205     assert((i>=0) && (i<n));
00206     return a[i];
00207   }
00208 
00209   template<class T>
00210   forceinline int
00211   SharedArray<T>::SAO::size(void) const {
00212     return n;
00213   }
00214 
00215   template<class T>
00216   forceinline T*
00217   SharedArray<T>::SAO::begin(void) {
00218     return a;
00219   }
00220 
00221   template<class T>
00222   forceinline const T*
00223   SharedArray<T>::SAO::begin(void) const {
00224     return a;
00225   }
00226 
00227   template<class T>
00228   forceinline T*
00229   SharedArray<T>::SAO::end(void) {
00230     return a+n;
00231   }
00232 
00233   template<class T>
00234   forceinline const T*
00235   SharedArray<T>::SAO::end(void) const {
00236     return a+n;
00237   }
00238 
00239 
00240   template<class T>
00241   forceinline
00242   SharedArray<T>::SharedArray(void) {}
00243 
00244   template<class T>
00245   forceinline
00246   SharedArray<T>::SharedArray(int n)
00247     : SharedHandle(new SAO(n)) {}
00248 
00249   template<class T>
00250   forceinline
00251   SharedArray<T>::SharedArray(const SharedArray<T>& sa)
00252     : SharedHandle(sa) {}
00253 
00254   template<class T>
00255   forceinline void
00256   SharedArray<T>::init(int n) {
00257     assert(object() == NULL);
00258     object(new SAO(n));
00259   }
00260 
00261   template<class T>
00262   forceinline T&
00263   SharedArray<T>::operator [](int i) {
00264     assert(object() != NULL);
00265     return (*static_cast<SAO*>(object()))[i];
00266   }
00267 
00268   template<class T>
00269   forceinline const T&
00270   SharedArray<T>::operator [](int i) const {
00271     assert(object() != NULL);
00272     return (*static_cast<SAO*>(object()))[i];
00273   }
00274 
00275   template<class T>
00276   inline bool
00277   SharedArray<T>::operator ==(const SharedArray<T>& sa) const {
00278     if (size() != sa.size())
00279       return false;
00280     if (object()==sa.object())
00281       return true;
00282     for (int i=0; i<size(); i++) {
00283       if ((*this)[i] != sa[i])
00284         return false;
00285     }
00286     return true;
00287   }
00288 
00289   template<class T>
00290   forceinline
00291   SharedArray<T>::SharedArray(const ArgArrayBase<T>& a)
00292     : SharedHandle(new SAO(a.size())) {
00293     for (int i=0; i<a.size(); i++)
00294       operator [](i)=a[i];
00295   }
00296 
00297   template<class T>
00298   forceinline int
00299   SharedArray<T>::size(void) const {
00300     assert(object() != NULL);
00301     return static_cast<SAO*>(object())->size();
00302   }
00303 
00304   template<class T>
00305   forceinline typename SharedArray<T>::iterator
00306   SharedArray<T>::begin(void) {
00307     assert(object() != NULL);
00308     return static_cast<SAO*>(object())->begin();
00309   }
00310 
00311   template<class T>
00312   forceinline typename SharedArray<T>::const_iterator
00313   SharedArray<T>::begin(void) const {
00314     assert(object() != NULL);
00315     return static_cast<SAO*>(object())->begin();
00316   }
00317 
00318   template<class T>
00319   forceinline typename SharedArray<T>::iterator
00320   SharedArray<T>::end(void) {
00321     assert(object() != NULL);
00322     return static_cast<SAO*>(object())->end();
00323   }
00324 
00325   template<class T>
00326   forceinline typename SharedArray<T>::const_iterator
00327   SharedArray<T>::end(void) const {
00328     assert(object() != NULL);
00329     return static_cast<SAO*>(object())->end();
00330   }
00331 
00332   template<class T>
00333   forceinline typename SharedArray<T>::reverse_iterator
00334   SharedArray<T>::rbegin(void) {
00335     assert(object() != NULL);
00336     return reverse_iterator(static_cast<SAO*>(object())->end());
00337   }
00338 
00339   template<class T>
00340   forceinline typename SharedArray<T>::const_reverse_iterator
00341   SharedArray<T>::rbegin(void) const {
00342     assert(object() != NULL);
00343     return const_reverse_iterator(static_cast<SAO*>(object())->end());
00344   }
00345 
00346   template<class T>
00347   forceinline typename SharedArray<T>::reverse_iterator
00348   SharedArray<T>::rend(void) {
00349     assert(object() != NULL);
00350     return reverse_iterator(static_cast<SAO*>(object())->begin());
00351   }
00352 
00353   template<class T>
00354   forceinline typename SharedArray<T>::const_reverse_iterator
00355   SharedArray<T>::rend(void) const {
00356     assert(object() != NULL);
00357     return const_reverse_iterator(static_cast<SAO*>(object())->begin());
00358   }
00359 
00360   template<class Char, class Traits, class T>
00361   std::basic_ostream<Char,Traits>&
00362   operator <<(std::basic_ostream<Char,Traits>& os,
00363              const SharedArray<T>& x) {
00364     std::basic_ostringstream<Char,Traits> s;
00365     s.copyfmt(os); s.width(0);
00366     s << '{';
00367     if (x.size() > 0) {
00368       s << x[0];
00369       for (int i=1; i<x.size(); i++)
00370         s << ", " << x[i];
00371     }
00372     s << '}';
00373     return os << s.str();
00374   }
00375 
00376 }
00377 
00378 // STATISTICS: kernel-other