|
| 1 | +#ifndef LIB_UTILS_ARITHMETICTREE_H_ |
| 2 | +#define LIB_UTILS_ARITHMETICTREE_H_ |
| 3 | + |
| 4 | +#include <memory> |
| 5 | +#include <variant> |
| 6 | + |
| 7 | +namespace mlir { |
| 8 | +namespace heir { |
| 9 | + |
| 10 | +// This file contains a generic tree structure that can be used for representing |
| 11 | +// arithmetic trees with leaf nodes of various types. |
| 12 | +template <typename T> |
| 13 | +struct ArithmeticTreeNode; |
| 14 | + |
| 15 | +// A leaf node for the tree |
| 16 | +template <typename T> |
| 17 | +struct LeafNode { |
| 18 | + T value; |
| 19 | +}; |
| 20 | + |
| 21 | +using ConstantNode = LeafNode<double>; |
| 22 | + |
| 23 | +template <typename T> |
| 24 | +struct AddNode { |
| 25 | + std::unique_ptr<ArithmeticTreeNode<T>> left; |
| 26 | + std::unique_ptr<ArithmeticTreeNode<T>> right; |
| 27 | +}; |
| 28 | + |
| 29 | +template <typename T> |
| 30 | +struct MultiplyNode { |
| 31 | + std::unique_ptr<ArithmeticTreeNode<T>> left; |
| 32 | + std::unique_ptr<ArithmeticTreeNode<T>> right; |
| 33 | +}; |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +struct PowerNode { |
| 37 | + std::unique_ptr<ArithmeticTreeNode<T>> base; |
| 38 | + size_t exponent; |
| 39 | +}; |
| 40 | + |
| 41 | +template <typename T> |
| 42 | +struct ArithmeticTreeNode { |
| 43 | + public: |
| 44 | + std::variant<ConstantNode, LeafNode<T>, AddNode<T>, MultiplyNode<T>, |
| 45 | + PowerNode<T>> |
| 46 | + node_variant; |
| 47 | + |
| 48 | + explicit ArithmeticTreeNode(double constant) |
| 49 | + : node_variant(ConstantNode{constant}) {} |
| 50 | + explicit ArithmeticTreeNode(const T& value) |
| 51 | + : node_variant(LeafNode<T>{value}) {} |
| 52 | + explicit ArithmeticTreeNode(T&& value) |
| 53 | + : node_variant(LeafNode<T>{std::move(value)}) {} |
| 54 | + |
| 55 | + private: |
| 56 | + ArithmeticTreeNode() = default; |
| 57 | + |
| 58 | + public: |
| 59 | + // Static factory methods |
| 60 | + static std::unique_ptr<ArithmeticTreeNode<T>> constant(double constant) { |
| 61 | + return std::unique_ptr<ArithmeticTreeNode<T>>( |
| 62 | + new ArithmeticTreeNode<T>(constant)); |
| 63 | + } |
| 64 | + |
| 65 | + static std::unique_ptr<ArithmeticTreeNode<T>> leaf(const T& value) { |
| 66 | + return std::unique_ptr<ArithmeticTreeNode<T>>( |
| 67 | + new ArithmeticTreeNode<T>(value)); |
| 68 | + } |
| 69 | + |
| 70 | + static std::unique_ptr<ArithmeticTreeNode<T>> add( |
| 71 | + std::unique_ptr<ArithmeticTreeNode<T>> lhs, |
| 72 | + std::unique_ptr<ArithmeticTreeNode<T>> rhs) { |
| 73 | + assert(lhs && rhs && "invalid add"); |
| 74 | + auto node = |
| 75 | + std::unique_ptr<ArithmeticTreeNode<T>>(new ArithmeticTreeNode<T>()); |
| 76 | + node->node_variant.template emplace<AddNode<T>>(std::move(lhs), |
| 77 | + std::move(rhs)); |
| 78 | + return node; |
| 79 | + } |
| 80 | + |
| 81 | + static std::unique_ptr<ArithmeticTreeNode<T>> mul( |
| 82 | + std::unique_ptr<ArithmeticTreeNode<T>> lhs, |
| 83 | + std::unique_ptr<ArithmeticTreeNode<T>> rhs) { |
| 84 | + assert(lhs && rhs && "invalid mul"); |
| 85 | + auto node = |
| 86 | + std::unique_ptr<ArithmeticTreeNode<T>>(new ArithmeticTreeNode<T>()); |
| 87 | + node->node_variant.template emplace<MultiplyNode<T>>(std::move(lhs), |
| 88 | + std::move(rhs)); |
| 89 | + return node; |
| 90 | + } |
| 91 | + |
| 92 | + static std::unique_ptr<ArithmeticTreeNode<T>> power( |
| 93 | + std::unique_ptr<ArithmeticTreeNode<T>> base, size_t exponent) { |
| 94 | + assert(base && "invalid base for power"); |
| 95 | + auto node = |
| 96 | + std::unique_ptr<ArithmeticTreeNode<T>>(new ArithmeticTreeNode<T>()); |
| 97 | + node->node_variant.template emplace<PowerNode<T>>(std::move(base), |
| 98 | + exponent); |
| 99 | + return node; |
| 100 | + } |
| 101 | + |
| 102 | + // The presence of std::unique_ptr in AddNode, MultiplyNode, and PowerNode |
| 103 | + // makes these types non-copyable. Consequently, ArithmeticTreeNode |
| 104 | + // itself is move-only by default. |
| 105 | + ArithmeticTreeNode(const ArithmeticTreeNode&) = delete; // No copying |
| 106 | + ArithmeticTreeNode& operator=(const ArithmeticTreeNode&) = |
| 107 | + delete; // No copy assignment |
| 108 | + |
| 109 | + ArithmeticTreeNode(ArithmeticTreeNode&& other) noexcept = |
| 110 | + default; // Allow move construction |
| 111 | + ArithmeticTreeNode& operator=(ArithmeticTreeNode&& other) noexcept = |
| 112 | + default; // Allow move assignment |
| 113 | + |
| 114 | + // Visitor pattern |
| 115 | + template <typename VisitorFunc> |
| 116 | + decltype(auto) visit(VisitorFunc&& visitor) { |
| 117 | + return std::visit(std::forward<VisitorFunc>(visitor), node_variant); |
| 118 | + } |
| 119 | + |
| 120 | + template <typename VisitorFunc> |
| 121 | + decltype(auto) visit(VisitorFunc&& visitor) const { |
| 122 | + return std::visit(std::forward<VisitorFunc>(visitor), node_variant); |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +} // namespace heir |
| 127 | +} // namespace mlir |
| 128 | + |
| 129 | +#endif // LIB_UTILS_ARITHMETICTREE_H_ |
0 commit comments