1#ifndef CYTNX_CONTRACTION_TREE_H_
2#define CYTNX_CONTRACTION_TREE_H_
7#include "utils/utils.hpp"
15 class Node :
public std::enable_shared_from_this<Node> {
19 std::shared_ptr<Node> left;
20 std::shared_ptr<Node> right;
21 std::weak_ptr<Node> root;
24 Node() : is_assigned(false) {}
27 : utensor(rhs.utensor),
28 is_assigned(rhs.is_assigned),
33 if (
auto r = rhs.root.lock()) {
38 Node& operator=(
const Node& rhs) {
40 utensor = rhs.utensor;
41 is_assigned = rhs.is_assigned;
45 if (
auto r = rhs.root.lock()) {
52 Node(std::shared_ptr<Node> in_left, std::shared_ptr<Node> in_right,
53 const UniTensor& in_uten = UniTensor())
54 : is_assigned(false), left(in_left), right(in_right) {
57 name =
"(" + left->name +
"," + right->name +
")";
60 if (in_uten.uten_type() !=
UTenType.Void) {
65 void set_root_ptrs() {
67 auto self = shared_from_this();
71 left->set_root_ptrs();
76 right->set_root_ptrs();
78 }
catch (
const std::bad_weak_ptr& e) {
79 std::cerr <<
"Failed to set root ptrs for node " << name <<
": " << e.what() << std::endl;
84 void clear_utensor() {
86 left->clear_utensor();
90 right->clear_utensor();
94 utensor = UniTensor();
97 void assign_utensor(
const UniTensor& in_uten) {
103 class ContractionTree {
105 std::vector<std::shared_ptr<Node>> nodes_container;
106 std::vector<std::shared_ptr<Node>> base_nodes;
108 ContractionTree() =
default;
109 ContractionTree(
const ContractionTree&) =
default;
110 ContractionTree& operator=(
const ContractionTree&) =
default;
113 nodes_container.clear();
117 void reset_contraction_order() {
119 for (
auto& node : base_nodes) {
120 if (node) node->root.reset();
123 nodes_container.clear();
128 if (!nodes_container.empty() && nodes_container.back()) {
129 nodes_container.back()->clear_utensor();
131 nodes_container.clear();
134 for (
auto& node : base_nodes) {
136 node->is_assigned =
false;
137 node->utensor = UniTensor();
142 void build_default_contraction_tree();
143 void build_contraction_tree_by_tokens(
const std::map<std::string, cytnx_uint64>& name2pos,
144 const std::vector<std::string>& tokens);
Definition Accessor.hpp:12
UniTensorType_class UTenType
UniTensor type.