Generated on Tue May 22 09:40:05 2018 for Gecode by doxygen 1.6.3

sort.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, 2009
00009  *     Guido Tack, 2010
00010  *
00011  *  This file is part of Gecode, the generic constraint
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *  Permission is hereby granted, free of charge, to any person obtaining
00016  *  a copy of this software and associated documentation files (the
00017  *  "Software"), to deal in the Software without restriction, including
00018  *  without limitation the rights to use, copy, modify, merge, publish,
00019  *  distribute, sublicense, and/or sell copies of the Software, and to
00020  *  permit persons to whom the Software is furnished to do so, subject to
00021  *  the following conditions:
00022  *
00023  *  The above copyright notice and this permission notice shall be
00024  *  included in all copies or substantial portions of the Software.
00025  *
00026  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00030  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00031  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00032  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  */
00035 
00036 namespace Gecode { namespace Int {
00037 
00039   template<class TaskView, bool inc>
00040   class StoEst {
00041   public:
00043     bool operator ()(const TaskView& t1, const TaskView& t2) const;
00044   };
00045 
00047   template<class TaskView, bool inc>
00048   class StoEct {
00049   public:
00051     bool operator ()(const TaskView& t1, const TaskView& t2) const;
00052   };
00053 
00055   template<class TaskView, bool inc>
00056   class StoLst {
00057   public:
00059     bool operator ()(const TaskView& t1, const TaskView& t2) const;
00060   };
00061 
00063   template<class TaskView, bool inc>
00064   class StoLct {
00065   public:
00067     bool operator ()(const TaskView& t1, const TaskView& t2) const;
00068   };
00069 
00071   template<class TaskView, template<class,bool> class STO, bool inc>
00072   class SortMap {
00073   private:
00075     const TaskViewArray<TaskView>& tasks;
00077     STO<TaskView,inc> sto;
00078   public:
00080     SortMap(const TaskViewArray<TaskView>& t);
00082     bool operator ()(int& i, int& j) const;
00083   };
00084 
00085   template<class TaskView, bool inc>
00086   forceinline bool
00087   StoEst<TaskView,inc>::operator ()
00088     (const TaskView& t1, const TaskView& t2) const {
00089     return inc ?
00090       (t1.est() < t2.est() || (t1.est()==t2.est() && t1.lct() < t2.lct()))
00091     : (t2.est() < t1.est() || (t2.est()==t1.est() && t2.lct() < t1.lct()));
00092   }
00093 
00094   template<class TaskView, bool inc>
00095   forceinline bool
00096   StoEct<TaskView,inc>::operator ()
00097     (const TaskView& t1, const TaskView& t2) const {
00098     return inc ?
00099       (t1.ect() < t2.ect() || (t1.ect()==t2.ect() && t1.lst() < t2.lst()))
00100     : (t2.ect() < t1.ect() || (t2.ect()==t1.ect() && t2.lst() < t1.lst()));
00101   }
00102 
00103   template<class TaskView, bool inc>
00104   forceinline bool
00105   StoLst<TaskView,inc>::operator ()
00106     (const TaskView& t1, const TaskView& t2) const {
00107     return inc ?
00108       (t1.lst() < t2.lst() || (t1.lst()==t2.lst() && t1.ect() < t2.ect()))
00109     : (t2.lst() < t1.lst() || (t2.lst()==t1.lst() && t2.ect() < t1.ect()));
00110   }
00111 
00112   template<class TaskView, bool inc>
00113   forceinline bool
00114   StoLct<TaskView,inc>::operator ()
00115     (const TaskView& t1, const TaskView& t2) const {
00116     return inc ?
00117       (t1.lct() < t2.lct() || (t1.lct()==t2.lct() && t1.est() < t2.est()))
00118     : (t2.lct() < t1.lct() || (t2.lct()==t1.lct() && t2.est() < t1.est()));
00119   }
00120 
00121   template<class TaskView, template<class,bool> class STO, bool inc>
00122   forceinline
00123   SortMap<TaskView,STO,inc>::SortMap(const TaskViewArray<TaskView>& t)
00124     : tasks(t) {}
00125   template<class TaskView, template<class,bool> class STO, bool inc>
00126   forceinline bool
00127   SortMap<TaskView,STO,inc>::operator ()(int& i, int& j) const {
00128     return sto(tasks[i],tasks[j]);
00129   }
00130 
00131   template<class TaskView, SortTaskOrder sto, bool inc>
00132   forceinline void
00133   sort(TaskViewArray<TaskView>& t) {
00134     switch (sto) {
00135     case STO_EST:
00136       {
00137         StoEst<TaskView,inc> o; Support::quicksort(&t[0], t.size(), o);
00138       }
00139       break;
00140     case STO_ECT:
00141       {
00142         StoEct<TaskView,inc> o; Support::quicksort(&t[0], t.size(), o);
00143       }
00144       break;
00145     case STO_LST:
00146       {
00147         StoLst<TaskView,inc> o; Support::quicksort(&t[0], t.size(), o);
00148       }
00149       break;
00150     case STO_LCT:
00151       {
00152         StoLct<TaskView,inc> o; Support::quicksort(&t[0], t.size(), o);
00153       }
00154       break;
00155     default:
00156       GECODE_NEVER;
00157     }
00158   }
00159 
00160   template<class TaskView, SortTaskOrder sto, bool inc>
00161   forceinline void
00162   sort(int* map, const TaskViewArray<TaskView>& t) {
00163     for (int i=t.size(); i--; )
00164       map[i]=i;
00165     switch (sto) {
00166     case STO_EST:
00167       {
00168         SortMap<TaskView,StoEst,inc> o(t);
00169         Support::quicksort(map, t.size(), o);
00170       }
00171       break;
00172     case STO_ECT:
00173       {
00174         SortMap<TaskView,StoEct,inc> o(t);
00175         Support::quicksort(map, t.size(), o);
00176       }
00177       break;
00178     case STO_LST:
00179       {
00180         SortMap<TaskView,StoLst,inc> o(t);
00181         Support::quicksort(map, t.size(), o);
00182       }
00183       break;
00184     case STO_LCT:
00185       {
00186         SortMap<TaskView,StoLct,inc> o(t);
00187         Support::quicksort(map, t.size(), o);
00188       }
00189       break;
00190     default:
00191       GECODE_NEVER;
00192     }
00193   }
00194 
00195   template<class TaskView, SortTaskOrder sto, bool inc>
00196   forceinline void
00197   sort(int* map, int n, const TaskViewArray<TaskView>& t) {
00198     switch (sto) {
00199     case STO_EST:
00200       {
00201         SortMap<TaskView,StoEst,inc> o(t);
00202         Support::quicksort(map, n, o);
00203       }
00204       break;
00205     case STO_ECT:
00206       {
00207         SortMap<TaskView,StoEct,inc> o(t);
00208         Support::quicksort(map, n, o);
00209       }
00210       break;
00211     case STO_LST:
00212       {
00213         SortMap<TaskView,StoLst,inc> o(t);
00214         Support::quicksort(map, n, o);
00215       }
00216       break;
00217     case STO_LCT:
00218       {
00219         SortMap<TaskView,StoLct,inc> o(t);
00220         Support::quicksort(map, n, o);
00221       }
00222       break;
00223     default:
00224       GECODE_NEVER;
00225     }
00226   }
00227 
00228 }}
00229 
00230 // STATISTICS: int-other