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-10-05 14:07:04 +0200 (Mon, 05 Oct 2009) $ by $Author: schulte $ 00011 * $Revision: 9815 $ 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 00040 forceinline int 00041 plus(int x, int y) { 00042 assert(y != -Int::Limits::infinity); 00043 return (x == -Int::Limits::infinity) ? x : x+y; 00044 } 00045 00046 template<class TaskView, class Node> 00047 forceinline int 00048 TaskTree<TaskView,Node>::n_inner(void) const { 00049 return tasks.size()-1; 00050 } 00051 template<class TaskView, class Node> 00052 forceinline int 00053 TaskTree<TaskView,Node>::n_nodes(void) const { 00054 return 2*tasks.size() - 1; 00055 } 00056 00057 template<class TaskView, class Node> 00058 forceinline bool 00059 TaskTree<TaskView,Node>::n_root(int i) { 00060 return i == 0; 00061 } 00062 template<class TaskView, class Node> 00063 forceinline bool 00064 TaskTree<TaskView,Node>::n_leaf(int i) const { 00065 return i > n_inner(); 00066 } 00067 template<class TaskView, class Node> 00068 forceinline int 00069 TaskTree<TaskView,Node>::n_left(int i) { 00070 return 2*(i+1) - 1; 00071 } 00072 template<class TaskView, class Node> 00073 forceinline int 00074 TaskTree<TaskView,Node>::n_right(int i) { 00075 return 2*(i+1); 00076 } 00077 template<class TaskView, class Node> 00078 forceinline int 00079 TaskTree<TaskView,Node>::n_parent(int i) { 00080 return (i+1)/2 - 1; 00081 } 00082 00083 template<class TaskView, class Node> 00084 forceinline Node& 00085 TaskTree<TaskView,Node>::leaf(int i) { 00086 return node[_leaf[i]]; 00087 } 00088 00089 template<class TaskView, class Node> 00090 forceinline const Node& 00091 TaskTree<TaskView,Node>::root(void) const { 00092 return node[0]; 00093 } 00094 00095 template<class TaskView, class Node> 00096 forceinline void 00097 TaskTree<TaskView,Node>::init(void) { 00098 for (int i=n_inner(); i--; ) 00099 node[i].init(node[n_left(i)],node[n_right(i)]); 00100 } 00101 00102 template<class TaskView, class Node> 00103 forceinline void 00104 TaskTree<TaskView,Node>::update(int i, bool l) { 00105 if (l) 00106 i = _leaf[i]; 00107 assert(!n_root(i)); 00108 do { 00109 i = n_parent(i); 00110 node[i].update(node[n_left(i)],node[n_right(i)]); 00111 } while (!n_root(i)); 00112 } 00113 00114 template<class TaskView, class Node> 00115 forceinline 00116 TaskTree<TaskView,Node>::TaskTree(Region& r, 00117 const TaskViewArray<TaskView>& t) 00118 : tasks(t), 00119 node(r.alloc<Node>(n_nodes())), 00120 _leaf(r.alloc<int>(tasks.size())) { 00121 // Compute a sorting map to order by non decreasing est 00122 int* map = r.alloc<int>(tasks.size()); 00123 sort<TaskView,STO_EST,true>(map, tasks); 00124 // Compute inverse of sorting map 00125 for (int i=tasks.size(); i--; ) 00126 _leaf[map[i]] = i; 00127 r.free<int>(map,tasks.size()); 00128 // Compute index of first leaf in tree: the next larger power of two 00129 int fst = 1; 00130 while (fst < tasks.size()) 00131 fst <<= 1; 00132 fst--; 00133 // Remap task indices to leaf indices 00134 for (int i=tasks.size(); i--; ) 00135 if (_leaf[i] + fst >= n_nodes()) 00136 _leaf[i] += fst - tasks.size(); 00137 else 00138 _leaf[i] += fst; 00139 } 00140 00141 }} 00142 00143 // STATISTICS: scheduling-prop