Generated on Thu Mar 22 10:39:42 2012 for Gecode by doxygen 1.6.3

ranges-append.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  *
00006  *  Copyright:
00007  *     Christian Schulte, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2010-07-28 17:35:33 +0200 (Wed, 28 Jul 2010) $ by $Author: schulte $
00011  *     $Revision: 11294 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 namespace Gecode { namespace Iter { namespace Ranges {
00039 
00049   template<class I, class J>
00050   class Append : public MinMax {
00051   protected:
00053     I i;
00055     J j;
00056   public:
00058 
00059 
00060     Append(void);
00062     Append(I& i, J& j);
00064     void init(I& i, J& j);
00066 
00068 
00069 
00070     void operator ++(void);
00072   };
00073 
00074 
00084   template<class I>
00085   class NaryAppend  :  public MinMax {
00086   protected:
00088     I* r;
00090     int n;
00092     int active;
00093   public:
00095 
00096 
00097     NaryAppend(void);
00099     NaryAppend(I* i, int n);
00101     void init(I* i, int n);
00103 
00105 
00106 
00107     void operator ++(void);
00109   };
00110 
00111 
00112   /*
00113    * Binary Append
00114    *
00115    */
00116 
00117   template<class I, class J>
00118   inline void
00119   Append<I,J>::operator ++(void) {
00120     if (i()) {
00121       mi = i.min(); ma = i.max();
00122       ++i;
00123       if (!i() && j() && (j.min() == ma+1)) {
00124         ma = j.max();
00125         ++j;
00126       }
00127     } else if (j()) {
00128       mi = j.min();  ma = j.max();
00129       ++j;
00130     } else {
00131       finish();
00132     }
00133   }
00134 
00135 
00136   template<class I, class J>
00137   forceinline
00138   Append<I,J>::Append(void) {}
00139 
00140   template<class I, class J>
00141   forceinline
00142   Append<I,J>::Append(I& i0, J& j0)
00143     : i(i0), j(j0) {
00144     if (i() || j())
00145       operator ++();
00146     else
00147       finish();
00148   }
00149 
00150   template<class I, class J>
00151   forceinline void
00152   Append<I,J>::init(I& i0, J& j0) {
00153     i = i0; j = j0;
00154     if (i() || j())
00155       operator ++();
00156     else
00157       finish();
00158   }
00159 
00160 
00161   /*
00162    * Nary Append
00163    *
00164    */
00165 
00166   template<class I>
00167   inline void
00168   NaryAppend<I>::operator ++(void) {
00169     mi = r[active].min();
00170     ma = r[active].max();
00171     ++r[active];
00172     while (!r[active]()) {
00173       //Skip empty iterators:
00174       do {
00175         active++;
00176         if (active >= n) {
00177           finish(); return;
00178         }
00179       } while (!r[active]());
00180       if (r[active].min() == ma+1){
00181         ma = r[active].max();
00182         ++r[active];
00183       } else {
00184         return;
00185       }
00186     }
00187   }
00188 
00189   template<class I>
00190   forceinline
00191   NaryAppend<I>::NaryAppend(void) {}
00192 
00193   template<class I>
00194   inline
00195   NaryAppend<I>::NaryAppend(I* r0, int n0)
00196     : r(r0), n(n0), active(0) {
00197     while (active < n && !r[active]())
00198       active++;
00199     if (active < n){
00200       operator ++();
00201     } else {
00202       finish();
00203     }
00204   }
00205 
00206   template<class I>
00207   inline void
00208   NaryAppend<I>::init(I* r0, int n0) {
00209     r = r0; n = n0; active = 0;
00210     while (active < n && !r[active]())
00211       active++;
00212     if (active < n){
00213       operator ++();
00214     } else {
00215       finish();
00216     }
00217   }
00218 
00219 }}}
00220 
00221 // STATISTICS: iter-any
00222