1#ifndef CYTNX_CONTRACTION_TREE_H_
2#define CYTNX_CONTRACTION_TREE_H_
7#include "utils/utils.hpp"
17 class Node :
public std::enable_shared_from_this<Node> {
21 std::shared_ptr<Node> left;
22 std::shared_ptr<Node> right;
23 std::weak_ptr<Node> root;
26 Node() : is_assigned(false) {}
29 : utensor(rhs.utensor),
30 is_assigned(rhs.is_assigned),
35 if (
auto r = rhs.root.lock()) {
40 Node& operator=(
const Node& rhs) {
42 utensor = rhs.utensor;
43 is_assigned = rhs.is_assigned;
47 if (
auto r = rhs.root.lock()) {
54 Node(std::shared_ptr<Node> in_left, std::shared_ptr<Node> in_right,
55 const UniTensor& in_uten = UniTensor())
56 : is_assigned(false), left(in_left), right(in_right) {
59 name =
"(" + left->name +
"," + right->name +
")";
62 if (in_uten.uten_type() !=
UTenType.Void) {
67 void set_root_ptrs() {
69 auto self = shared_from_this();
73 left->set_root_ptrs();
78 right->set_root_ptrs();
80 }
catch (
const std::bad_weak_ptr& e) {
81 std::cerr <<
"Failed to set root ptrs for node " << name <<
": " << e.what() << std::endl;
86 void clear_utensor() {
88 left->clear_utensor();
92 right->clear_utensor();
96 utensor = UniTensor();
99 void assign_utensor(
const UniTensor& in_uten) {
105 class ContractionTree {
107 std::vector<std::shared_ptr<Node>> nodes_container;
108 std::vector<std::shared_ptr<Node>> base_nodes;
110 ContractionTree() =
default;
111 ContractionTree(
const ContractionTree&) =
default;
112 ContractionTree& operator=(
const ContractionTree&) =
default;
115 nodes_container.clear();
119 void reset_contraction_order() {
121 for (
auto& node : base_nodes) {
122 if (node) node->root.reset();
125 nodes_container.clear();
130 if (!nodes_container.empty() && nodes_container.back()) {
131 nodes_container.back()->clear_utensor();
133 nodes_container.clear();
136 for (
auto& node : base_nodes) {
138 node->is_assigned =
false;
139 node->utensor = UniTensor();
144 void build_default_contraction_tree();
145 void build_contraction_tree_by_tokens(
const std::map<std::string, cytnx_uint64>& name2pos,
146 const std::vector<std::string>& tokens);
Definition Accessor.hpp:12
UniTensorType_class UTenType
UniTensor type.