// 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/tags.h" namespace ap::graph { template struct Node; template struct UndefinedTag : public std::monostate { using std::monostate::monostate; }; template struct IndexedTag { T data; }; template struct UnindexedTag { T data; }; template using ValidListTagImpl = std::variant, UnindexedTag>; template struct ValidListTag : public ValidListTagImpl { using ValidListTagImpl::ValidListTagImpl; ADT_DEFINE_VARIANT_METHODS(ValidListTagImpl); }; template using ListTagImpl = std::variant, IndexedTag, UnindexedTag>; template struct ListTag : public ListTagImpl { using ListTagImpl::ListTagImpl; ADT_DEFINE_VARIANT_METHODS(ListTagImpl); }; template struct NodeList : public ListTag>> { using list_type = adt::List>; using ListTag::ListTag; ListTag type() const { return this->Match( [](const UndefinedTag&) -> ListTag { return UndefinedTag{}; }, [](const IndexedTag&) -> ListTag { return IndexedTag{}; }, [](const UnindexedTag&) -> ListTag { return UnindexedTag{}; }); } adt::Result> Sole() const { return this->Match( [](const UndefinedTag&) -> adt::Result> { return adt::errors::TypeError{"UndefinedList has no sole data"}; }, [](const auto& l) -> adt::Result> { ADT_CHECK(l.data->size() == 1); return l.data->at(0); }); } std::size_t size() const { return this->Match( [](const UndefinedTag&) -> std::size_t { return 0; }, [](const auto& l) -> std::size_t { return l.data->size(); }); } template adt::Result VisitNodes(const DoEachT& DoEach) const { return this->Match( [](const UndefinedTag&) -> adt::Result { return adt::Ok{}; }, [&](const auto& l) -> adt::Result { for (const auto& data : *l.data) { ADT_RETURN_IF_ERR(DoEach(data)); } return adt::Ok{}; }); } }; } // namespace ap::graph