gecode » scheduling » task

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, 2009 00008 * 00009 * Last modified: 00010 * $Date: 2009-08-11 21:20:39 +0200 (Tue, 11 Aug 2009) $ by $Author: schulte $ 00011 * $Revision: 9589 $ 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 Scheduling { 00039 00041 template<class TaskView, bool inc> 00042 class StoEst { 00043 public: 00045 bool operator ()(const TaskView& t1, const TaskView& t2) const; 00046 }; 00047 00049 template<class TaskView, bool inc> 00050 class StoEct { 00051 public: 00053 bool operator ()(const TaskView& t1, const TaskView& t2) const; 00054 }; 00055 00057 template<class TaskView, bool inc> 00058 class StoLst { 00059 public: 00061 bool operator ()(const TaskView& t1, const TaskView& t2) const; 00062 }; 00063 00065 template<class TaskView, bool inc> 00066 class StoLct { 00067 public: 00069 bool operator ()(const TaskView& t1, const TaskView& t2) const; 00070 }; 00071 00073 template<class TaskView, template<class,bool> class STO, bool inc> 00074 class SortMap { 00075 private: 00077 const TaskViewArray<TaskView>& tasks; 00079 STO<TaskView,inc> sto; 00080 public: 00082 SortMap(const TaskViewArray<TaskView>& t); 00084 bool operator ()(int& i, int& j) const; 00085 }; 00086 00087 template<class TaskView, bool inc> 00088 forceinline bool 00089 StoEst<TaskView,inc>::operator () 00090 (const TaskView& t1, const TaskView& t2) const { 00091 return inc ? (t1.est() < t2.est()) : (t2.est() < t1.est()); 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 ? (t1.ect() < t2.ect()) : (t2.ect() < t1.ect()); 00099 } 00100 00101 template<class TaskView, bool inc> 00102 forceinline bool 00103 StoLst<TaskView,inc>::operator () 00104 (const TaskView& t1, const TaskView& t2) const { 00105 return inc ? (t1.lst() < t2.lst()) : (t2.lst() < t1.lst()); 00106 } 00107 00108 template<class TaskView, bool inc> 00109 forceinline bool 00110 StoLct<TaskView,inc>::operator () 00111 (const TaskView& t1, const TaskView& t2) const { 00112 return inc ? (t1.lct() < t2.lct()) : (t2.lct() < t1.lct()); 00113 } 00114 00115 00116 template<class TaskView, template<class,bool> class STO, bool inc> 00117 forceinline 00118 SortMap<TaskView,STO,inc>::SortMap(const TaskViewArray<TaskView>& t) 00119 : tasks(t) {} 00120 template<class TaskView, template<class,bool> class STO, bool inc> 00121 forceinline bool 00122 SortMap<TaskView,STO,inc>::operator ()(int& i, int& j) const { 00123 return sto(tasks[i],tasks[j]); 00124 } 00125 00126 template<class TaskView, SortTaskOrder sto, bool inc> 00127 forceinline void 00128 sort(TaskViewArray<TaskView>& t) { 00129 switch (sto) { 00130 case STO_EST: 00131 { 00132 StoEst<TaskView,inc> o; Support::quicksort(&t[0], t.size(), o); 00133 } 00134 break; 00135 case STO_ECT: 00136 { 00137 StoEct<TaskView,inc> o; Support::quicksort(&t[0], t.size(), o); 00138 } 00139 break; 00140 case STO_LST: 00141 { 00142 StoLst<TaskView,inc> o; Support::quicksort(&t[0], t.size(), o); 00143 } 00144 break; 00145 case STO_LCT: 00146 { 00147 StoLct<TaskView,inc> o; Support::quicksort(&t[0], t.size(), o); 00148 } 00149 break; 00150 default: 00151 GECODE_NEVER; 00152 } 00153 } 00154 00155 template<class TaskView, SortTaskOrder sto, bool inc> 00156 forceinline void 00157 sort(int* map, const TaskViewArray<TaskView>& t) { 00158 for (int i=t.size(); i--; ) 00159 map[i]=i; 00160 switch (sto) { 00161 case STO_EST: 00162 { 00163 SortMap<TaskView,StoEst,inc> o(t); 00164 Support::quicksort(map, t.size(), o); 00165 } 00166 break; 00167 case STO_ECT: 00168 { 00169 SortMap<TaskView,StoEct,inc> o(t); 00170 Support::quicksort(map, t.size(), o); 00171 } 00172 break; 00173 case STO_LST: 00174 { 00175 SortMap<TaskView,StoLst,inc> o(t); 00176 Support::quicksort(map, t.size(), o); 00177 } 00178 break; 00179 case STO_LCT: 00180 { 00181 SortMap<TaskView,StoLct,inc> o(t); 00182 Support::quicksort(map, t.size(), o); 00183 } 00184 break; 00185 default: 00186 GECODE_NEVER; 00187 } 00188 } 00189 00190 template<class TaskView, SortTaskOrder sto, bool inc> 00191 forceinline void 00192 sort(int* map, int n, const TaskViewArray<TaskView>& t) { 00193 switch (sto) { 00194 case STO_EST: 00195 { 00196 SortMap<TaskView,StoEst,inc> o(t); 00197 Support::quicksort(map, n, o); 00198 } 00199 break; 00200 case STO_ECT: 00201 { 00202 SortMap<TaskView,StoEct,inc> o(t); 00203 Support::quicksort(map, n, o); 00204 } 00205 break; 00206 case STO_LST: 00207 { 00208 SortMap<TaskView,StoLst,inc> o(t); 00209 Support::quicksort(map, n, o); 00210 } 00211 break; 00212 case STO_LCT: 00213 { 00214 SortMap<TaskView,StoLct,inc> o(t); 00215 Support::quicksort(map, n, o); 00216 } 00217 break; 00218 default: 00219 GECODE_NEVER; 00220 } 00221 } 00222 00223 }} 00224 00225 // STATISTICS: scheduling-other