Debian Codegraph @

mlspp/src/tree_math.cpp

raw symbols SCIP
1 #include "mls/tree_math.h"
2 #include "mls/common.h"
3
4 #include <algorithm>
5
6 static const uint32_t one = 0x01;
7
8 static uint32_t
9 log2(uint32_t x)
10 {
11 if (x == 0) {
12 return 0;
13 }
14
15 uint32_t k = 0;
16 while ((x >> k) > 0) {
17 k += 1;
18 }
19 return k - 1;
20 }
21
22 namespace mlspp {
23
24 LeafCount::LeafCount(const NodeCount w)
25 {
26 if (w.val == 0) {
27 val = 0;
28 return;
29 }
30
31 if ((w.val & one) == 0) {
32 throw InvalidParameterError("Only odd node counts describe trees");
33 }
34
35 val = (w.val >> one) + 1;
36 }
37
38 LeafCount
39 LeafCount::full(const LeafCount n)
40 {
41 auto w = uint32_t(1);
42 while (w < n.val) {
43 w <<= 1U;
44 }
45 return LeafCount{ w };
46 }
47
48 NodeCount::NodeCount(const LeafCount n)
49 : UInt32(2 * (n.val - 1) + 1)
50 {
51 }
52
53 LeafIndex::LeafIndex(NodeIndex x)
54 : UInt32(0)
55 {
56 if (x.val % 2 == 1) {
57 throw InvalidParameterError("Only even node indices describe leaves");
58 }
59
60 val = x.val >> 1; // NOLINT(hicpp-signed-bitwise)
61 }
62
63 NodeIndex
64 LeafIndex::ancestor(LeafIndex other) const
65 {
66 auto ln = NodeIndex(*this);
67 auto rn = NodeIndex(other);
68 if (ln == rn) {
69 return ln;
70 }
71
72 uint8_t k = 0;
73 while (ln != rn) {
74 ln.val = ln.val >> 1U;
75 rn.val = rn.val >> 1U;
76 k += 1;
77 }
78
79 const uint32_t prefix = ln.val << k;
80 const uint32_t stop = (1U << uint8_t(k - 1));
81 return NodeIndex{ prefix + (stop - 1) };
82 }
83
84 NodeIndex::NodeIndex(LeafIndex x)
85 : UInt32(2 * x.val)
86 {
87 }
88
89 NodeIndex
90 NodeIndex::root(LeafCount n)
91 {
92 if (n.val == 0) {
93 throw std::runtime_error("Root for zero-size tree is undefined");
94 }
95
96 auto w = NodeCount(n);
97 return NodeIndex{ (one << log2(w.val)) - 1 };
98 }
99
100 bool
101 NodeIndex::is_leaf() const
102 {
103 return val % 2 == 0;
104 }
105
106 bool
107 NodeIndex::is_below(NodeIndex other) const
108 {
109 auto lx = level();
110 auto ly = other.level();
111 return lx <= ly && (val >> (ly + 1) == other.val >> (ly + 1));
112 }
113
114 NodeIndex
115 NodeIndex::left() const
116 {
117 if (is_leaf()) {
118 return *this;
119 }
120
121 // The clang analyzer doesn't realize that is_leaf() assures that level >= 1
122 // NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult)
123 return NodeIndex{ val ^ (one << (level() - 1)) };
124 }
125
126 NodeIndex
127 NodeIndex::right() const
128 {
129 if (is_leaf()) {
130 return *this;
131 }
132
133 return NodeIndex{ val ^ (uint32_t(0x03) << (level() - 1)) };
134 }
135
136 NodeIndex
137 NodeIndex::parent() const
138 {
139 auto k = level();
140 return NodeIndex{ (val | (one << k)) & ~(one << (k + 1)) };
141 }
142
143 NodeIndex
144 NodeIndex::sibling() const
145 {
146 return sibling(parent());
147 }
148
149 NodeIndex
150 NodeIndex::sibling(NodeIndex ancestor) const
151 {
152 if (!is_below(ancestor)) {
153 throw InvalidParameterError("Node is not below claimed ancestor");
154 }
155
156 auto l = ancestor.left();
157 auto r = ancestor.right();
158
159 if (is_below(l)) {
160 return r;
161 }
162
163 return l;
164 }
165
166 std::vector<NodeIndex>
167 NodeIndex::dirpath(LeafCount n)
168 {
169 if (val >= NodeCount(n).val) {
170 throw InvalidParameterError("Request for dirpath outside of tree");
171 }
172
173 auto d = std::vector<NodeIndex>{};
174
175 auto r = root(n);
176 if (*this == r) {
177 return d;
178 }
179
180 auto p = parent();
181 while (p.val != r.val) {
182 d.push_back(p);
183 p = p.parent();
184 }
185
186 // Include the root except in a one-member tree
187 if (val != r.val) {
188 d.push_back(p);
189 }
190
191 return d;
192 }
193
194 std::vector<NodeIndex>
195 NodeIndex::copath(LeafCount n)
196 {
197 auto d = dirpath(n);
198 if (d.empty()) {
199 return {};
200 }
201
202 // Prepend leaf; omit root
203 d.insert(d.begin(), *this);
204 d.pop_back();
205
206 return stdx::transform<NodeIndex>(d, [](auto x) { return x.sibling(); });
207 }
208
209 uint32_t
210 NodeIndex::level() const
211 {
212 if ((val & one) == 0) {
213 return 0;
214 }
215
216 uint32_t k = 0;
217 while (((val >> k) & one) == 1) {
218 k += 1;
219 }
220 return k;
221 }
222
223 } // namespace mlspp