Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.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.org>
5 * Guido Tack <tack@gecode.org>
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.org
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 int
39 plus(int x, int y) {
40 assert(y != -Limits::infinity);
41 return (x == -Limits::infinity) ? x : x+y;
42 }
43
44 forceinline long long int
45 plus(long long int x, long long int y) {
46 assert(y != -Limits::llinfinity);
47 return (x == -Limits::llinfinity) ? x : x+y;
48 }
49
50 template<class TaskView, class Node>
51 forceinline int
53 return tasks.size()-1;
54 }
55 template<class TaskView, class Node>
56 forceinline int
58 return 2*tasks.size() - 1;
59 }
60
61 template<class TaskView, class Node>
62 forceinline bool
64 return i == 0;
65 }
66 template<class TaskView, class Node>
67 forceinline bool
69 return i >= n_inner();
70 }
71 template<class TaskView, class Node>
72 forceinline int
74 return 2*(i+1) - 1;
75 }
76 template<class TaskView, class Node>
77 forceinline bool
79 assert(!n_root(i));
80 // A left node has an odd number
81 return (i & 1) != 0;
82 }
83 template<class TaskView, class Node>
84 forceinline int
86 return 2*(i+1);
87 }
88 template<class TaskView, class Node>
89 forceinline bool
91 assert(!n_root(i));
92 // A left node has an even number
93 return (i & 1) == 0;
94 }
95 template<class TaskView, class Node>
96 forceinline int
98 return (i+1)/2 - 1;
99 }
100
101 template<class TaskView, class Node>
102 forceinline Node&
104 return node[_leaf[i]];
105 }
106
107 template<class TaskView, class Node>
108 forceinline const Node&
110 return node[0];
111 }
112
113 template<class TaskView, class Node>
114 forceinline void
116 for (int i=n_inner(); i--; )
117 node[i].init(node[n_left(i)],node[n_right(i)]);
118 }
119
120 template<class TaskView, class Node>
121 forceinline void
123 for (int i=n_inner(); i--; )
124 node[i].update(node[n_left(i)],node[n_right(i)]);
125 }
126
127 template<class TaskView, class Node>
128 forceinline void
130 if (l)
131 i = _leaf[i];
132 assert(!n_root(i));
133 do {
134 i = n_parent(i);
135 node[i].update(node[n_left(i)],node[n_right(i)]);
136 } while (!n_root(i));
137 }
138
139 template<class TaskView, class Node>
143 : tasks(t),
144 node(r.alloc<Node>(n_nodes())),
145 _leaf(r.alloc<int>(tasks.size())) {
146 // Compute a sorting map to order by non decreasing est
147 int* map = r.alloc<int>(tasks.size());
149 // Compute inverse of sorting map
150 for (int i=0; i<tasks.size(); i++)
151 _leaf[map[i]] = i;
152 r.free<int>(map,tasks.size());
153 // Compute index of first leaf in tree: the next larger power of two
154 int fst = 1;
155 while (fst < tasks.size())
156 fst <<= 1;
157 fst--;
158 // Remap task indices to leaf indices
159 for (int i=0; i<tasks.size(); i++)
160 if (_leaf[i] + fst >= n_nodes())
161 _leaf[i] += fst - tasks.size();
162 else
163 _leaf[i] += fst;
164 }
165
166 template<class TaskView, class Node> template<class Node2>
170 : tasks(t.tasks),
171 node(r.alloc<Node>(n_nodes())),
172 _leaf(r.alloc<int>(tasks.size())) {
173 for (int i=0; i<tasks.size(); i++)
174 _leaf[i] = t._leaf[i];
175 }
176
177}}
178
179// STATISTICS: int-prop
NNF * l
Left subtree.
NodeType t
Type of node.
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1607
Task trees for task views with node type Node.
Definition task.hh:365
int n_nodes(void) const
Return number of nodes for balanced binary tree.
Definition tree.hpp:57
const TaskViewArray< TaskView > & tasks
The tasks from which the tree is computed.
Definition task.hh:369
static int n_right(int i)
Return index of right child of node i.
Definition tree.hpp:85
int n_inner(void) const
Return number of inner nodes.
Definition tree.hpp:52
static bool right(int i)
Test whether node i is a right child.
Definition tree.hpp:90
static int n_left(int i)
Return index of left child of node i.
Definition tree.hpp:73
static bool left(int i)
Test whether node i is a left child.
Definition tree.hpp:78
void init(void)
Initialize tree after leaves have been initialized.
Definition tree.hpp:115
friend class TaskTree
Definition task.hh:366
bool n_leaf(int i) const
Whether node i is leaf.
Definition tree.hpp:68
const Node & root(void) const
Return root node.
Definition tree.hpp:109
static int n_parent(int i)
Return index of parent of node i.
Definition tree.hpp:97
int * _leaf
Map task number to leaf node number in right order.
Definition task.hh:373
Node & leaf(int i)
Return leaf for task i.
Definition tree.hpp:103
void update(void)
Update all inner nodes of tree after leaves have been initialized.
Definition tree.hpp:122
static bool n_root(int i)
Whether node i is index of root.
Definition tree.hpp:63
Task view array.
Definition task.hh:233
Handle to region.
Definition region.hpp:55
void update(const NoOffset &)
Integer-precision integer scale view.
Definition view.hpp:638
const int infinity
Infinity for integers.
Definition int.hh:120
const long long int llinfinity
Infinity for long long integers.
Definition int.hh:126
void sort(TaskViewArray< TaskView > &t)
Sort task view array t according to sto and inc (increasing or decreasing)
Definition sort.hpp:133
int plus(int x, int y)
Safe addition in case x is -Int::Limits::infinity.
Definition tree.hpp:39
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:767
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:767
Post propagator for SetVar x
Definition set.hh:767
#define forceinline
Definition config.hpp:187