// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include #include #include "paddle/ap/include/adt/adt.h" #include "paddle/ap/include/graph/node_list.h" #include "paddle/ap/include/graph/tags.h" namespace ap::graph { template class NodeArena; template struct Node { tNodeId node_id_; std::weak_ptr> node_arena_; const tNodeId& node_id() const { return node_id_; } std::weak_ptr> node_arena() const { return node_arena_; } adt::Result>> GetNodeArena() const { auto ptr = node_arena_.lock(); if (!ptr) { return adt::errors::RuntimeError{"NodeArena is delete."}; } return ptr; } adt::Result Get() const; adt::Result> DownstreamNodes() const; adt::Result> UpstreamNodes() const; adt::Result ConnectTo( const Node& dst_node, const ValidListTag& src_downstream_type, const ValidListTag& dst_unstream_type) const; bool operator<(const Node& other) const { if (!(this->node_id_ == other.node_id_)) { return this->node_id_.value() < other.node_id_.value(); } return this->node_arena_.lock() < other.node_arena_.lock(); } bool operator==(const Node& other) const { return other.node_id_.value() == this->node_id_.value() && other.node_arena_.lock() == this->node_arena_.lock(); } bool operator!=(const Node& other) const { return !(*this == other); } std::size_t GetHashValue() const { return adt::hash_combine( this->node_id_.value(), std::hash>>()(this->node_arena_.lock())); } }; } // namespace ap::graph namespace std { template struct hash> { std::size_t operator()(const ap::graph::Node& node) const { return node.GetHashValue(); } }; } // namespace std