| 1 | #cxx . . $ `<file>/mlspp/include/mls/tree_math.h`/pragma once |
| 2 | |
| 3 | #include <cstdint> |
| 4 | #include <tls/tls_syntax.h>cxx . . $ `<file>/mlspp/lib/bytes/include/tls/tls_syntax.h`/ |
| 5 | #include <vector> |
| 6 | |
| 7 | // The below functions provide the index calculus for the tree |
| 8 | // structures used in MLS. They are premised on a "flat" |
| 9 | // representation of a balanced binary tree. Leaf nodes are |
| 10 | // even-numbered nodes, with the n-th leaf at 2*n. Intermediate |
| 11 | // nodes are held in odd-numbered nodes. For example, a 11-element |
| 12 | // tree has the following structure: |
| 13 | // |
| 14 | // X |
| 15 | // X |
| 16 | // X X X |
| 17 | // X X X X X |
| 18 | // X X X X X X X X X X X |
| 19 | // 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 |
| 20 | // |
| 21 | // This allows us to compute relationships between tree nodes simply |
| 22 | // by manipulating indices, rather than having to maintain |
| 23 | // complicated structures in memory, even for partial trees. (The |
| 24 | // storage for a tree can just be a map[int]Node dictionary or an |
| 25 | // array.) The basic rule is that the high-order bits of parent and |
| 26 | // child nodes have the following relation: |
| 27 | // |
| 28 | // 01x = <00x, 10x> |
| 29 | |
| 30 | namespace mlsppcxx . . $ mlspp/ { |
| 31 | |
| 32 | // Index types go in the overall namespace |
| 33 | // XXX([email protected]): Seems like this stuff can probably get |
| 34 | // simplified down a fair bit. |
| 35 | struct UInt32cxx . . $ mlspp/UInt32# |
| 36 | { |
| 37 | uint32_tcxx . . $ uint32_t# valcxx . . $ mlspp/UInt32#val.; |
| 38 | |
| 39 | UInt32cxx . . $ mlspp/UInt32#UInt32(49f6e7a06ebc5aa8).() |
| 40 | : valcxx . . $ mlspp/UInt32#val.(0) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | explicit UInt32cxx . . $ mlspp/UInt32#UInt32(cb2f890c2fdad230).(uint32_tcxx . . $ uint32_t# val_in) |
| 45 | : valcxx . . $ mlspp/UInt32#val.(val_in) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | TLS_SERIALIZABLEcxx . . $ mlspp/UInt32#_tls_fields_w(b4299156a92d78ac).(val) |
| 50 | }; |
| 51 | |
| 52 | struct NodeCountcxx . . $ mlspp/NodeCount#; |
| 53 | |
| 54 | struct LeafCountcxx . . $ mlspp/LeafCount# : public UInt32cxx . . $ mlspp/UInt32# |
| 55 | { |
| 56 | using UInt32cxx . . $ mlspp/UInt32#::UInt32cxx . . $ mlspp/UInt32#UInt32(cb2f890c2fdad230).; |
| 57 | explicit LeafCountcxx . . $ mlspp/LeafCount#LeafCount(fa60cf8d54654d16).(const NodeCountcxx . . $ mlspp/NodeCount# w); |
| 58 | |
| 59 | static LeafCountcxx . . $ mlspp/LeafCount# fullcxx . . $ mlspp/LeafCount#full(1e5797729f614563).(const LeafCountcxx . . $ mlspp/LeafCount# n); |
| 60 | }; |
| 61 | |
| 62 | struct NodeCountcxx . . $ mlspp/NodeCount# : public UInt32cxx . . $ mlspp/UInt32# |
| 63 | { |
| 64 | using UInt32cxx . . $ mlspp/UInt32#::UInt32cxx . . $ mlspp/UInt32#UInt32(7b4453284bafe81d).; |
| 65 | explicit NodeCountcxx . . $ mlspp/NodeCount#NodeCount(a733f6ba51500b8c).(const LeafCountcxx . . $ mlspp/LeafCount# n); |
| 66 | }; |
| 67 | |
| 68 | struct NodeIndexcxx . . $ mlspp/NodeIndex#; |
| 69 | |
| 70 | struct LeafIndexcxx . . $ mlspp/LeafIndex# : public UInt32cxx . . $ mlspp/UInt32# |
| 71 | { |
| 72 | using UInt32cxx . . $ mlspp/UInt32#::UInt32cxx . . $ mlspp/UInt32#; |
| 73 | explicit LeafIndexcxx . . $ mlspp/LeafIndex#LeafIndex(29011067b9a1f03).(const NodeIndexcxx . . $ mlspp/NodeIndex# x); |
| 74 | bool operatorcxx . . $ mlspp/LeafIndex#`operator<`(be225f04b72ff6f7).<(const LeafIndexcxx . . $ mlspp/LeafIndex# other) const { return valcxx . . $ mlspp/UInt32#val. < other.valcxx . . $ mlspp/UInt32#val.; } |
| 75 | bool operatorcxx . . $ mlspp/LeafIndex#`operator<`(6a6f1ee87d9a491e).<(const LeafCountcxx . . $ mlspp/LeafCount# other) const { return valcxx . . $ mlspp/UInt32#val. < other.valcxx . . $ mlspp/UInt32#val.; } |
| 76 | |
| 77 | NodeIndexcxx . . $ mlspp/NodeIndex# ancestorcxx . . $ mlspp/LeafIndex#ancestor(9a73b556cbb4fcfc).(LeafIndexcxx . . $ mlspp/LeafIndex# other) const; |
| 78 | }; |
| 79 | |
| 80 | struct NodeIndexcxx . . $ mlspp/NodeIndex# : public UInt32cxx . . $ mlspp/UInt32# |
| 81 | { |
| 82 | using UInt32cxx . . $ mlspp/UInt32#::UInt32cxx . . $ mlspp/UInt32#UInt32(7b4453284bafe81d).; |
| 83 | explicit NodeIndexcxx . . $ mlspp/NodeIndex#NodeIndex(5a3d3cee3fdf86fc).(const LeafIndexcxx . . $ mlspp/LeafIndex# x); |
| 84 | bool operatorcxx . . $ mlspp/NodeIndex#`operator<`(df9c6d3ab1251738).<(const NodeIndexcxx . . $ mlspp/NodeIndex# other) const { return valcxx . . $ mlspp/UInt32#val. < other.valcxx . . $ mlspp/UInt32#val.; } |
| 85 | bool operatorcxx . . $ mlspp/NodeIndex#`operator<`(41d61865334b0392).<(const NodeCountcxx . . $ mlspp/NodeCount# other) const { return valcxx . . $ mlspp/UInt32#val. < other.valcxx . . $ mlspp/UInt32#val.; } |
| 86 | |
| 87 | static NodeIndexcxx . . $ mlspp/NodeIndex# rootcxx . . $ mlspp/NodeIndex#root(f0a39db46e675ba1).(LeafCountcxx . . $ mlspp/LeafCount# n); |
| 88 | |
| 89 | bool is_leafcxx . . $ mlspp/NodeIndex#is_leaf(50ce9a9e25b4a850).() const; |
| 90 | bool is_belowcxx . . $ mlspp/NodeIndex#is_below(df9c6d3ab1251738).(NodeIndexcxx . . $ mlspp/NodeIndex# other) const; |
| 91 | |
| 92 | NodeIndexcxx . . $ mlspp/NodeIndex# leftcxx . . $ mlspp/NodeIndex#left(d66767722381bbc7).() const; |
| 93 | NodeIndexcxx . . $ mlspp/NodeIndex# rightcxx . . $ mlspp/NodeIndex#right(d66767722381bbc7).() const; |
| 94 | NodeIndexcxx . . $ mlspp/NodeIndex# parentcxx . . $ mlspp/NodeIndex#parent(d66767722381bbc7).() const; |
| 95 | NodeIndexcxx . . $ mlspp/NodeIndex# siblingcxx . . $ mlspp/NodeIndex#sibling(d66767722381bbc7).() const; |
| 96 | |
| 97 | // Returns the sibling of this node "relative to this ancestor" -- the child |
| 98 | // of `ancestor` that is not in the direct path of this node. |
| 99 | NodeIndexcxx . . $ mlspp/NodeIndex# siblingcxx . . $ mlspp/NodeIndex#sibling(6f0fa48a65b17a10).(NodeIndexcxx . . $ mlspp/NodeIndex# ancestor) const; |
| 100 | |
| 101 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#> dirpathcxx . . $ mlspp/NodeIndex#dirpath(41abe7a05f92e8bb).(LeafCountcxx . . $ mlspp/LeafCount# n); |
| 102 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<NodeIndexcxx . . $ mlspp/NodeIndex#> copathcxx . . $ mlspp/NodeIndex#copath(41abe7a05f92e8bb).(LeafCountcxx . . $ mlspp/LeafCount# n); |
| 103 | |
| 104 | uint32_tcxx . . $ uint32_t# levelcxx . . $ mlspp/NodeIndex#level(ddd8539a2fcdd4ce).() const; |
| 105 | }; |
| 106 | |
| 107 | } // namespace mlspp |