Generated on Mon May 10 06:46:44 2010 for Gecode by doxygen 1.6.3

tree.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, 2009
00008  *
00009  *  Last modified:
00010  *     $Date: 2010-02-15 22:02:53 +0100 (Mon, 15 Feb 2010) $ by $Author: schulte $
00011  *     $Revision: 10285 $
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 bool
00074   TaskTree<TaskView,Node>::left(int i) {
00075     assert(!n_root(i));
00076     // A left node has an odd number
00077     return (i & 1) != 0;
00078   }
00079   template<class TaskView, class Node>
00080   forceinline int
00081   TaskTree<TaskView,Node>::n_right(int i) {
00082     return 2*(i+1);
00083   }
00084   template<class TaskView, class Node>
00085   forceinline bool
00086   TaskTree<TaskView,Node>::right(int i) {
00087     assert(!n_root(i));
00088     // A left node has an even number
00089     return (i & 1) == 0;
00090   }
00091   template<class TaskView, class Node>
00092   forceinline int
00093   TaskTree<TaskView,Node>::n_parent(int i) {
00094     return (i+1)/2 - 1;
00095   }
00096 
00097   template<class TaskView, class Node>
00098   forceinline Node&
00099   TaskTree<TaskView,Node>::leaf(int i) {
00100     return node[_leaf[i]];
00101   }
00102 
00103   template<class TaskView, class Node>
00104   forceinline const Node&
00105   TaskTree<TaskView,Node>::root(void) const {
00106     return node[0];
00107   }
00108 
00109   template<class TaskView, class Node>
00110   forceinline void
00111   TaskTree<TaskView,Node>::init(void) {
00112     for (int i=n_inner(); i--; )
00113       node[i].init(node[n_left(i)],node[n_right(i)]);
00114   }
00115 
00116   template<class TaskView, class Node>
00117   forceinline void
00118   TaskTree<TaskView,Node>::update(int i, bool l) {
00119     if (l)
00120       i = _leaf[i];
00121     assert(!n_root(i));
00122     do {
00123       i = n_parent(i);
00124       node[i].update(node[n_left(i)],node[n_right(i)]);
00125     } while (!n_root(i));
00126   }
00127 
00128   template<class TaskView, class Node>
00129   forceinline
00130   TaskTree<TaskView,Node>::TaskTree(Region& r, 
00131                                     const TaskViewArray<TaskView>& t)
00132     : tasks(t), 
00133       node(r.alloc<Node>(n_nodes())),
00134       _leaf(r.alloc<int>(tasks.size())) {
00135     // Compute a sorting map to order by non decreasing est
00136     int* map = r.alloc<int>(tasks.size());
00137     sort<TaskView,STO_EST,true>(map, tasks);
00138     // Compute inverse of sorting map
00139     for (int i=tasks.size(); i--; )
00140       _leaf[map[i]] = i;
00141     r.free<int>(map,tasks.size());
00142     // Compute index of first leaf in tree: the next larger power of two
00143     int fst = 1;
00144     while (fst < tasks.size())
00145       fst <<= 1;
00146     fst--;
00147     // Remap task indices to leaf indices
00148     for (int i=tasks.size(); i--; )
00149       if (_leaf[i] + fst >= n_nodes())
00150         _leaf[i] += fst - tasks.size();
00151       else
00152         _leaf[i] += fst;
00153   }
00154 
00155 }}
00156 
00157 // STATISTICS: scheduling-prop