Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / ir / GraphIterator.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_GRAPH_ITERATOR_H__
18 #define __ONERT_IR_GRAPH_ITERATOR_H__
19
20 #include <type_traits>
21
22 #include "ir/Index.h"
23
24 namespace onert
25 {
26 namespace compiler
27 {
28 class LoweredGraph;
29 } // namespace compiler
30 } // namespace onert
31
32 namespace onert
33 {
34 namespace ir
35 {
36
37 class Graph;
38 class Operation;
39 class OpSequence;
40
41 template <bool is_const> class Iterator
42 {
43 public:
44   using GraphRef = typename std::conditional<is_const, const Graph &, Graph &>::type;
45   using IndexRef = const OperationIndex &;
46   using NodeRef = typename std::conditional<is_const, const Operation &, Operation &>::type;
47   using IterFn = std::function<void(IndexRef, NodeRef)>;
48
49 public:
50   virtual ~Iterator() = default;
51   virtual void iterate(GraphRef graph, const IterFn &fn) const = 0;
52 };
53
54 template <bool is_const = false> class DefaultIterator final : public Iterator<is_const>
55 {
56 public:
57   using GraphRef = typename Iterator<is_const>::GraphRef;
58   using IndexRef = typename Iterator<is_const>::IndexRef;
59   using NodeRef = typename Iterator<is_const>::NodeRef;
60   using IterFn = typename Iterator<is_const>::IterFn;
61
62 public:
63   void iterate(GraphRef graph, const IterFn &fn) const;
64 };
65 using DefaultConstIterator = DefaultIterator<true>;
66
67 template <bool is_const = false> class PostDfsIterator final : public Iterator<is_const>
68 {
69 public:
70   using GraphRef = typename Iterator<is_const>::GraphRef;
71   using IndexRef = typename Iterator<is_const>::IndexRef;
72   using NodeRef = typename Iterator<is_const>::NodeRef;
73   using IterFn = typename Iterator<is_const>::IterFn;
74   using LoweredGraphRef =
75       typename std::conditional<is_const, const typename compiler::LoweredGraph &,
76                                 typename compiler::LoweredGraph &>::type;
77   using OpSequenceRef = typename std::conditional<is_const, const OpSequence &, OpSequence &>::type;
78   using OpSeqIndexRef = const OpSequenceIndex &;
79   using OpSeqIterFn = std::function<void(OpSeqIndexRef, OpSequenceRef)>;
80
81 public:
82   void iterate(GraphRef graph, const IterFn &fn) const;
83   void iterateOpSeqs(LoweredGraphRef lowered_graph, const OpSeqIterFn &f) const;
84 };
85 using PostDfsConstIterator = PostDfsIterator<true>;
86
87 } // namespace ir
88 } // namespace onert
89
90 #endif // __ONERT_IR_GRAPH_ITERATOR_H__