| 1 | #cxx . . $ `<file>/mlspp/src/tree_math.cpp`/include "mls/tree_math.h"cxx . . $ `<file>/mlspp/include/mls/tree_math.h`/ |
| 2 | #include "mls/common.h"cxx . . $ `<file>/mlspp/include/mls/common.h`/ |
| 3 | |
| 4 | #include <algorithm> |
| 5 | |
| 6 | static const uint32_tcxx . . $ uint32_t# onecxx . . $ one. = 0x01; |
| 7 | |
| 8 | static uint32_tcxx . . $ uint32_t# |
| 9 | log2cxx . . $ log2(2141b656ce2cc063).(uint32_tcxx . . $ uint32_t# x) |
| 10 | { |
| 11 | if (x == 0) { |
| 12 | return 0; |
| 13 | } |
| 14 | |
| 15 | uint32_tcxx . . $ uint32_t# k = 0; |
| 16 | while ((x >> k) > 0) { |
| 17 | k += 1; |
| 18 | } |
| 19 | return k - 1; |
| 20 | } |
| 21 | |
| 22 | namespace mlsppcxx . . $ mlspp/ { |
| 23 | |
| 24 | LeafCountcxx . . $ mlspp/LeafCount#::LeafCountcxx . . $ mlspp/LeafCount#LeafCount(fa60cf8d54654d16).(const NodeCountcxx . . $ mlspp/NodeCount# w) |
| 25 | { |
| 26 | if (w.valcxx . . $ mlspp/UInt32#val. == 0) { |
| 27 | valcxx . . $ mlspp/UInt32#val. = 0; |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | if ((w.valcxx . . $ mlspp/UInt32#val. & onecxx . . $ one.) == 0) { |
| 32 | throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Only odd node counts describe trees"); |
| 33 | } |
| 34 | |
| 35 | valcxx . . $ mlspp/UInt32#val. = (w.valcxx . . $ mlspp/UInt32#val. >> onecxx . . $ one.) + 1; |
| 36 | } |
| 37 | |
| 38 | LeafCountcxx . . $ mlspp/LeafCount# |
| 39 | LeafCountcxx . . $ mlspp/LeafCount#::fullcxx . . $ mlspp/LeafCount#full(1e5797729f614563).(const LeafCountcxx . . $ mlspp/LeafCount# n) |
| 40 | { |
| 41 | auto w = uint32_tcxx . . $ uint32_t#(1); |
| 42 | while (w < n.valcxx . . $ mlspp/UInt32#val.) { |
| 43 | w <<= 1U; |
| 44 | } |
| 45 | return LeafCountcxx . . $ mlspp/LeafCount#{ w }; |
| 46 | } |
| 47 | |
| 48 | NodeCountcxx . . $ mlspp/NodeCount#::NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(const LeafCountcxx . . $ mlspp/LeafCount# n) |
| 49 | : UInt32cxx . . $ mlspp/UInt32#(2 * (n.valcxx . . $ mlspp/UInt32#val. - 1) + 1) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | LeafIndexcxx . . $ mlspp/LeafIndex#::LeafIndexcxx . . $ mlspp/LeafIndex#LeafIndex(29011067b9a1f03).(NodeIndexcxx . . $ mlspp/NodeIndex# x) |
| 54 | : UInt32cxx . . $ mlspp/UInt32#(0) |
| 55 | { |
| 56 | if (x.valcxx . . $ mlspp/UInt32#val. % 2 == 1) { |
| 57 | throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Only even node indices describe leaves"); |
| 58 | } |
| 59 | |
| 60 | valcxx . . $ mlspp/UInt32#val. = x.valcxx . . $ mlspp/UInt32#val. >> 1; // NOLINT(hicpp-signed-bitwise) |
| 61 | } |
| 62 | |
| 63 | NodeIndexcxx . . $ mlspp/NodeIndex# |
| 64 | LeafIndexcxx . . $ mlspp/LeafIndex#::ancestorcxx . . $ mlspp/LeafIndex#ancestor(9a73b556cbb4fcfc).(LeafIndexcxx . . $ mlspp/LeafIndex# other) const |
| 65 | { |
| 66 | auto ln = NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(*this); |
| 67 | auto rn = NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(other); |
| 68 | if (ln ==cxx . . $ mlspp/`operator==`(5401d31d8ac0aee4). rn) { |
| 69 | return ln; |
| 70 | } |
| 71 | |
| 72 | uint8_tcxx . . $ uint8_t# k = 0; |
| 73 | while (ln !=cxx . . $ mlspp/`operator!=`(5401d31d8ac0aee4). rn) { |
| 74 | ln.valcxx . . $ mlspp/UInt32#val. = ln.valcxx . . $ mlspp/UInt32#val. >> 1U; |
| 75 | rn.valcxx . . $ mlspp/UInt32#val. = rn.valcxx . . $ mlspp/UInt32#val. >> 1U; |
| 76 | k += 1; |
| 77 | } |
| 78 | |
| 79 | const uint32_tcxx . . $ uint32_t# prefix = ln.valcxx . . $ mlspp/UInt32#val. << k; |
| 80 | const uint32_tcxx . . $ uint32_t# stop = (1U << uint8_tcxx . . $ uint8_t#(k - 1)); |
| 81 | return NodeIndexcxx . . $ mlspp/NodeIndex#{ prefix + (stop - 1) }; |
| 82 | } |
| 83 | |
| 84 | NodeIndexcxx . . $ mlspp/NodeIndex#::NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(LeafIndexcxx . . $ mlspp/LeafIndex# x) |
| 85 | : UInt32cxx . . $ mlspp/UInt32#(2 * x.valcxx . . $ mlspp/UInt32#val.) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | NodeIndexcxx . . $ mlspp/NodeIndex# |
| 90 | NodeIndexcxx . . $ mlspp/NodeIndex#::rootcxx . . $ mlspp/NodeIndex#root(f0a39db46e675ba1).(LeafCountcxx . . $ mlspp/LeafCount# n) |
| 91 | { |
| 92 | if (n.valcxx . . $ mlspp/UInt32#val. == 0) { |
| 93 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Root for zero-size tree is undefined"); |
| 94 | } |
| 95 | |
| 96 | auto w = NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(n); |
| 97 | return NodeIndexcxx . . $ mlspp/NodeIndex#{ (onecxx . . $ one. << log2cxx . . $ log2(2141b656ce2cc063).(w.valcxx . . $ mlspp/UInt32#val.)) - 1 }; |
| 98 | } |
| 99 | |
| 100 | bool |
| 101 | NodeIndexcxx . . $ mlspp/NodeIndex#::is_leafcxx . . $ mlspp/NodeIndex#is_leaf(50ce9a9e25b4a850).() const |
| 102 | { |
| 103 | return valcxx . . $ mlspp/UInt32#val. % 2 == 0; |
| 104 | } |
| 105 | |
| 106 | bool |
| 107 | NodeIndexcxx . . $ mlspp/NodeIndex#::is_belowcxx . . $ mlspp/NodeIndex#is_below(df9c6d3ab1251738).(NodeIndexcxx . . $ mlspp/NodeIndex# other) const |
| 108 | { |
| 109 | auto lx = levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).(); |
| 110 | auto ly = other.levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).(); |
| 111 | return lx <= ly && (valcxx . . $ mlspp/UInt32#val. >> (ly + 1) == other.valcxx . . $ mlspp/UInt32#val. >> (ly + 1)); |
| 112 | } |
| 113 | |
| 114 | NodeIndexcxx . . $ mlspp/NodeIndex# |
| 115 | NodeIndexcxx . . $ mlspp/NodeIndex#::leftcxx . . $ mlspp/NodeIndex#left(d66767722381bbc7).() const |
| 116 | { |
| 117 | if (is_leafcxx . . $ mlspp/NodeIndex#is_leaf(50ce9a9e25b4a850).()) { |
| 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 NodeIndexcxx . . $ mlspp/NodeIndex#{ valcxx . . $ mlspp/UInt32#val. ^ (onecxx . . $ one. << (levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).() - 1)) }; |
| 124 | } |
| 125 | |
| 126 | NodeIndexcxx . . $ mlspp/NodeIndex# |
| 127 | NodeIndexcxx . . $ mlspp/NodeIndex#::rightcxx . . $ mlspp/NodeIndex#right(d66767722381bbc7).() const |
| 128 | { |
| 129 | if (is_leafcxx . . $ mlspp/NodeIndex#is_leaf(50ce9a9e25b4a850).()) { |
| 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | return NodeIndexcxx . . $ mlspp/NodeIndex#{ valcxx . . $ mlspp/UInt32#val. ^ (uint32_tcxx . . $ uint32_t#(0x03) << (levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).() - 1)) }; |
| 134 | } |
| 135 | |
| 136 | NodeIndexcxx . . $ mlspp/NodeIndex# |
| 137 | NodeIndexcxx . . $ mlspp/NodeIndex#::parentcxx . . $ mlspp/NodeIndex#parent(d66767722381bbc7).() const |
| 138 | { |
| 139 | auto k = levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).(); |
| 140 | return NodeIndexcxx . . $ mlspp/NodeIndex#{ (valcxx . . $ mlspp/UInt32#val. | (onecxx . . $ one. << k)) & ~(onecxx . . $ one. << (k + 1)) }; |
| 141 | } |
| 142 | |
| 143 | NodeIndexcxx . . $ mlspp/NodeIndex# |
| 144 | NodeIndexcxx . . $ mlspp/NodeIndex#::siblingcxx . . $ mlspp/NodeIndex#sibling(d66767722381bbc7).() const |
| 145 | { |
| 146 | return siblingcxx . . $ mlspp/NodeIndex#sibling(6f0fa48a65b17a10).(parentcxx . . $ mlspp/NodeIndex#parent(d66767722381bbc7).()); |
| 147 | } |
| 148 | |
| 149 | NodeIndexcxx . . $ mlspp/NodeIndex# |
| 150 | NodeIndexcxx . . $ mlspp/NodeIndex#::siblingcxx . . $ mlspp/NodeIndex#sibling(6f0fa48a65b17a10).(NodeIndexcxx . . $ mlspp/NodeIndex# ancestor) const |
| 151 | { |
| 152 | if (!is_belowcxx . . $ mlspp/NodeIndex#is_below(df9c6d3ab1251738).(ancestor)) { |
| 153 | throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Node is not below claimed ancestor"); |
| 154 | } |
| 155 | |
| 156 | auto l = ancestor.leftcxx . . $ mlspp/NodeIndex#left(d66767722381bbc7).(); |
| 157 | auto r = ancestor.rightcxx . . $ mlspp/NodeIndex#right(d66767722381bbc7).(); |
| 158 | |
| 159 | if (is_belowcxx . . $ mlspp/NodeIndex#is_below(df9c6d3ab1251738).(l)) { |
| 160 | return r; |
| 161 | } |
| 162 | |
| 163 | return l; |
| 164 | } |
| 165 | |
| 166 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#> |
| 167 | NodeIndexcxx . . $ mlspp/NodeIndex#::dirpathcxx . . $ mlspp/NodeIndex#dirpath(41abe7a05f92e8bb).(LeafCountcxx . . $ mlspp/LeafCount# n) |
| 168 | { |
| 169 | if (valcxx . . $ mlspp/UInt32#val. >= NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(n).valcxx . . $ mlspp/UInt32#val.) { |
| 170 | throw InvalidParameterErrorcxx . . $ mlspp/InvalidParameterError#("Request for dirpath outside of tree"); |
| 171 | } |
| 172 | |
| 173 | auto d = stdcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#>{}; |
| 174 | |
| 175 | auto r = rootcxx . . $ mlspp/NodeIndex#root(f0a39db46e675ba1).(n); |
| 176 | if (*this ==cxx . . $ mlspp/`operator==`(5401d31d8ac0aee4). r) { |
| 177 | return dcxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 178 | } |
| 179 | |
| 180 | auto p = parentcxx . . $ mlspp/NodeIndex#parent(d66767722381bbc7).(); |
| 181 | while (p.valcxx . . $ mlspp/UInt32#val. != r.valcxx . . $ mlspp/UInt32#val.) { |
| 182 | d.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(p); |
| 183 | p =cxx . . $ mlspp/NodeIndex#`operator=`(1805de707aa904d0). p.parentcxx . . $ mlspp/NodeIndex#parent(d66767722381bbc7).(); |
| 184 | } |
| 185 | |
| 186 | // Include the root except in a one-member tree |
| 187 | if (valcxx . . $ mlspp/UInt32#val. != r.valcxx . . $ mlspp/UInt32#val.) { |
| 188 | d.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(p); |
| 189 | } |
| 190 | |
| 191 | return dcxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 192 | } |
| 193 | |
| 194 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#> |
| 195 | NodeIndexcxx . . $ mlspp/NodeIndex#::copathcxx . . $ mlspp/NodeIndex#copath(41abe7a05f92e8bb).(LeafCountcxx . . $ mlspp/LeafCount# n) |
| 196 | { |
| 197 | auto d = dirpathcxx . . $ mlspp/NodeIndex#dirpath(41abe7a05f92e8bb).(n); |
| 198 | if (d.emptycxx . . $ std/vector#empty(3482b152b9333168).()) { |
| 199 | return {cxx . . $ std/vector#vector(49f6e7a06ebc5aa8).}; |
| 200 | } |
| 201 | |
| 202 | // Prepend leaf; omit root |
| 203 | d.insertcxx . . $ std/vector#insert(e54735f7c99f2c9d).(dcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..begincxx . . $ std/vector#begin(55a6de06420d4903).(), *this); |
| 204 | d.pop_backcxx . . $ std/vector#pop_back(ced63f7c635d850d).(); |
| 205 | |
| 206 | return stdxcxx . . $ mlspp/stdx/::transformcxx . . $ mlspp/stdx/transform(895a059819fd6266).<NodeIndexcxx . . $ mlspp/NodeIndex#>(d, [](auto x) { return x.sibling(); }); |
| 207 | } |
| 208 | |
| 209 | uint32_tcxx . . $ uint32_t# |
| 210 | NodeIndexcxx . . $ mlspp/NodeIndex#::levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).() const |
| 211 | { |
| 212 | if ((valcxx . . $ mlspp/UInt32#val. & onecxx . . $ one.) == 0) { |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | uint32_tcxx . . $ uint32_t# k = 0; |
| 217 | while (((valcxx . . $ mlspp/UInt32#val. >> k) & onecxx . . $ one.) == 1) { |
| 218 | k += 1; |
| 219 | } |
| 220 | return k; |
| 221 | } |
| 222 | |
| 223 | } // namespace mlspp |