-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode.hpp
163 lines (140 loc) · 4.08 KB
/
node.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <[email protected]>
*
* Copyright:
* Guido Tack, 2006
*
* Last modified:
* $Date$ by $Author$
* $Revision$
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef NODE_HPP
#define NODE_HPP
inline unsigned int
Node::getTag(void) const {
return static_cast<unsigned int>
(reinterpret_cast<ptrdiff_t>(childrenOrFirstChild) & 3);
}
inline void
Node::setTag(unsigned int tag) {
assert(getTag() == UNDET);
resetTag(tag);
}
inline void
Node::resetTag(unsigned int tag) {
assert(tag <= 3);
childrenOrFirstChild = reinterpret_cast<void*>
( (reinterpret_cast<ptrdiff_t>(childrenOrFirstChild) & ~(3)) | tag);
}
inline void*
Node::getPtr(void) const {
return reinterpret_cast<void*>
(reinterpret_cast<ptrdiff_t>(childrenOrFirstChild) & ~(3));
}
inline int
Node::getFirstChild(void) const {
return static_cast<int>
((reinterpret_cast<ptrdiff_t>(childrenOrFirstChild) & ~(3)) >> 2);
}
inline void
Node::setFirstChild(int n) {
assert(getTag() == TWO_CHILDREN);
childrenOrFirstChild = reinterpret_cast<void*>(n << 2 | getTag());
}
inline
Node::Node(int p, bool failed) : parent(p) {
childrenOrFirstChild = nullptr;
noOfChildren = 0;
setTag(failed ? LEAF : UNDET);
#ifdef MAXIM_DEBUG
debug_id = Node::debug_instance_counter++;
#endif
}
inline int
Node::getParent(void) const {
return parent;
}
inline VisualNode*
Node::getParent(const NodeAllocator& na) const {
return parent < 0 ? nullptr : na[parent];
}
inline bool
Node::isUndetermined(void) const { return getTag() == UNDET; }
inline void
Node::replaceChild(NodeAllocator& na, int new_node_gid, int alt) {
/// NOTE(maxim): only handling binary trees for now
if (getTag() != TWO_CHILDREN) return;
if (alt == 0) {
childrenOrFirstChild =
reinterpret_cast<void*>(new_node_gid << 2);
setTag(TWO_CHILDREN);
} else if (alt == 1) {
noOfChildren = -new_node_gid;
}
na[new_node_gid]->parent = getIndex(na);
}
inline int
Node::getChild(int n) const {
assert(getTag() != UNDET && getTag() != LEAF);
if (getTag() == TWO_CHILDREN) {
assert(n != 1 || noOfChildren <= 0);
return n == 0 ? getFirstChild() : -noOfChildren;
}
assert(n < noOfChildren);
return static_cast<int*>(getPtr())[n];
}
inline VisualNode*
Node::getChild(const NodeAllocator& na, int n) const {
return na[getChild(n)];
}
inline bool
Node::isRoot(void) const { return parent == -1; }
inline unsigned int
Node::getNumberOfChildren(void) const {
switch (getTag()) {
case UNDET: return 0;
case LEAF: return 0;
case TWO_CHILDREN: return 1+(noOfChildren <= 0);
default: return noOfChildren;
}
}
inline int
Node::getIndex(const NodeAllocator& na) const {
if (parent==-1)
return 0;
Node* p = na[parent];
for (int i=p->getNumberOfChildren(); i--;) {
if (p->getChild(na,i) == this) {
return p->getChild(i);
}
}
assert(false);
return -1;
}
#endif