Generated on for Gecode by doxygen 1.18.0
tree.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.dev>
5 * Guido Tack <tack@gecode.dev>
6 *
7 * Copyright:
8 * Christian Schulte, 2009
9 * Guido Tack, 2010
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.dev
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
36namespace Gecode { namespace Int {
37
38 forceinline long long int
39 plus(long long int x, long long int y) {
40 assert(y != -Limits::llinfinity);
41 return (x == -Limits::llinfinity) ? x : x+y;
42 }
43
44 template<class TaskView, class Node>
45 forceinline int
47 return tasks.size()-1;
48 }
49 template<class TaskView, class Node>
50 forceinline int
52 return 2*tasks.size() - 1;
53 }
54
55 template<class TaskView, class Node>
56 forceinline bool
58 return i == 0;
59 }
60 template<class TaskView, class Node>
61 forceinline bool
63 return i >= n_inner();
64 }
65 template<class TaskView, class Node>
66 forceinline int
68 return 2*(i+1) - 1;
69 }
70 template<class TaskView, class Node>
71 forceinline bool
73 assert(!n_root(i));
74 // A left node has an odd number
75 return (i & 1) != 0;
76 }
77 template<class TaskView, class Node>
78 forceinline int
80 return 2*(i+1);
81 }
82 template<class TaskView, class Node>
83 forceinline bool
85 assert(!n_root(i));
86 // A left node has an even number
87 return (i & 1) == 0;
88 }
89 template<class TaskView, class Node>
90 forceinline int
92 return (i+1)/2 - 1;
93 }
94
95 template<class TaskView, class Node>
96 forceinline Node&
98 return node[_leaf[i]];
99 }
100
101 template<class TaskView, class Node>
102 forceinline const Node&
104 return node[0];
105 }
106
107 template<class TaskView, class Node>
108 forceinline void
110 for (int i=n_inner(); i--; )
111 node[i].init(node[n_left(i)],node[n_right(i)]);
112 }
113
114 template<class TaskView, class Node>
115 forceinline void
117 for (int i=n_inner(); i--; )
118 node[i].update(node[n_left(i)],node[n_right(i)]);
119 }
120
121 template<class TaskView, class Node>
122 forceinline void
124 if (l)
125 i = _leaf[i];
126 assert(!n_root(i));
127 do {
128 i = n_parent(i);
129 node[i].update(node[n_left(i)],node[n_right(i)]);
130 } while (!n_root(i));
131 }
132
133 template<class TaskView, class Node>
134 forceinline
137 : tasks(t),
138 node(r.alloc<Node>(n_nodes())),
139 _leaf(r.alloc<int>(tasks.size())) {
140 // Compute a sorting map to order by non decreasing est
141 int* map = r.alloc<int>(tasks.size());
143 // Compute inverse of sorting map
144 for (int i=0; i<tasks.size(); i++)
145 _leaf[map[i]] = i;
146 r.free<int>(map,tasks.size());
147 // Compute index of first leaf in tree: the next larger power of two
148 int fst = 1;
149 while (fst < tasks.size())
150 fst <<= 1;
151 fst--;
152 // Remap task indices to leaf indices
153 for (int i=0; i<tasks.size(); i++)
154 if (_leaf[i] + fst >= n_nodes())
155 _leaf[i] += fst - tasks.size();
156 else
157 _leaf[i] += fst;
158 }
159
160 template<class TaskView, class Node> template<class Node2>
161 forceinline
164 : tasks(t.tasks),
165 node(r.alloc<Node>(n_nodes())),
166 _leaf(r.alloc<int>(tasks.size())) {
167 for (int i=0; i<tasks.size(); i++)
168 _leaf[i] = t._leaf[i];
169 }
170
171}}
172
173// STATISTICS: int-prop
int n_nodes(void) const
Return number of nodes for balanced binary tree.
Definition tree.hpp:51
const TaskViewArray< TaskView > & tasks
The tasks from which the tree is computed.
Definition task.hh:366
static int n_right(int i)
Return index of right child of node i.
Definition tree.hpp:79
int n_inner(void) const
Return number of inner nodes.
Definition tree.hpp:46
static bool right(int i)
Test whether node i is a right child.
Definition tree.hpp:84
static int n_left(int i)
Return index of left child of node i.
Definition tree.hpp:67
static bool left(int i)
Test whether node i is a left child.
Definition tree.hpp:72
void init(void)
Initialize tree after leaves have been initialized.
Definition tree.hpp:109
void update(int i, bool l=true)
Update tree after leaf for task i has changed (l whether i refers to a leaf).
Definition tree.hpp:123
Node * node
Task nodes.
Definition task.hh:368
friend class TaskTree
Definition task.hh:363
bool n_leaf(int i) const
Whether node i is leaf.
Definition tree.hpp:62
const Node & root(void) const
Return root node.
Definition tree.hpp:103
static int n_parent(int i)
Return index of parent of node i.
Definition tree.hpp:91
int * _leaf
Map task number to leaf node number in right order.
Definition task.hh:370
Node & leaf(int i)
Return leaf for task i.
Definition tree.hpp:97
void update(void)
Update all inner nodes of tree after leaves have been initialized.
Definition tree.hpp:116
static bool n_root(int i)
Whether node i is index of root.
Definition tree.hpp:57
Task view array.
Definition task.hh:233
Handle to region.
Definition region.hpp:55
void free(void)
Free allocate memory.
Definition region.hpp:356
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition region.hpp:386
const long long int llinfinity
Infinity for long long integers.
Definition int.hh:131
Finite domain integers.
Definition lastval.hh:52
void sort(TaskViewArray< TaskView > &t)
Sort task view array t according to sto and inc (increasing or decreasing).
Definition sort.hpp:133
long long int plus(long long int x, long long int y)
Safe addition in case x is -Int::Limits::llinfinity.
Definition tree.hpp:39
Gecode toplevel namespace