Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / ir / Subgraphs.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __ONERT_IR_SUBGRAPHS_H__
18 #define __ONERT_IR_SUBGRAPHS_H__
19
20 #include <memory>
21 #include <unordered_map>
22
23 #include "ir/Index.h"
24 #include "util/ObjectManager.h"
25
26 namespace onert
27 {
28 namespace ir
29 {
30
31 class Graph;
32
33 class Subgraphs
34 {
35 public:
36   Subgraphs() = default;
37   Subgraphs(const Subgraphs &obj) = default;
38   Subgraphs(Subgraphs &&) = default;
39   Subgraphs &operator=(const Subgraphs &) = default;
40   Subgraphs &operator=(Subgraphs &&) = default;
41   ~Subgraphs() = default;
42
43   /**
44    * @brief Put subgraph in the container with a new Index for that
45    *
46    * @param[in] subg Subgraph to be pushed
47    * @param[in] index Index of subgraph to be pushed
48    * @return Created
49    */
50   void push(SubgraphIndex index, const std::shared_ptr<Graph> &subg) { _subgraphs[index] = subg; }
51
52   /**
53    * @brief Remove the subgraph that is associated with the given index
54    *
55    * @param[in] index Index of the subgraph to be removed
56    * @return N/A
57    */
58   void remove(const SubgraphIndex &index) { _subgraphs.erase(index); }
59
60   /**
61    * @brief Get the subgraph that is associated with the given index
62    *
63    * @param[in] index Index of the subgraph to be returned
64    * @return Graph
65    */
66   const std::shared_ptr<Graph> &at(const SubgraphIndex &index) const
67   {
68     return _subgraphs.at(index);
69   }
70   /**
71    * @brief Get the subgraph that is associated with the given index
72    *
73    * @param[in] index Index of the subgraph to be returned
74    * @return Graph
75    */
76   std::shared_ptr<Graph> &at(const SubgraphIndex &index) { return _subgraphs.at(index); }
77
78   /**
79    * @brief Get the subgraph that is associated with the given index
80    *
81    * @param[in] index Index of the subgraph to be returned
82    * @return true if such entry exists otherwise false
83    */
84   bool exist(const SubgraphIndex &index) const
85   {
86     auto it = _subgraphs.find(index);
87     return it != _subgraphs.end();
88   }
89
90   /**
91    * @brief Iterate over the container with given function
92    *
93    * @param[in] fn Function to be run for every container entry
94    * @return N/A
95    */
96   void iterate(const std::function<void(const SubgraphIndex &, const Graph &)> &fn) const
97   {
98     for (const auto &e : _subgraphs)
99     {
100       fn(e.first, *e.second);
101     }
102   }
103
104   /**
105    * @brief Iterate over the container with given function
106    *
107    * @param[in] fn Function to be run for every container entry
108    * @return N/A
109    */
110   void iterate(const std::function<void(const SubgraphIndex &, Graph &)> &fn)
111   {
112     for (const auto &e : _subgraphs)
113     {
114       fn(e.first, *e.second);
115     }
116   }
117
118   /**
119    * @brief Get count of Subgraphs
120    *
121    * @return count of Subgraphs
122    */
123   size_t count() const { return _subgraphs.size(); }
124
125   /**
126    * @brief Return the primary subgraph
127    *
128    * @return std::shared_ptr<Graph> Primary sugraph
129    */
130   std::shared_ptr<Graph> primary() const { return _subgraphs.at(SubgraphIndex{0}); }
131
132 private:
133   std::unordered_map<SubgraphIndex, std::shared_ptr<Graph>> _subgraphs;
134 };
135
136 } // namespace ir
137 } // namespace onert
138
139 #endif // __ONERT_IR_SUBGRAPHS_H__