2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONERT_IR_GRAPH_ITERATOR_H__
18 #define __ONERT_IR_GRAPH_ITERATOR_H__
20 #include <type_traits>
29 } // namespace compiler
41 template <bool is_const> class Iterator
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)>;
50 virtual ~Iterator() = default;
51 virtual void iterate(GraphRef graph, const IterFn &fn) const = 0;
54 template <bool is_const = false> class DefaultIterator final : public Iterator<is_const>
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;
63 void iterate(GraphRef graph, const IterFn &fn) const;
65 using DefaultConstIterator = DefaultIterator<true>;
67 template <bool is_const = false> class PostDfsIterator final : public Iterator<is_const>
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)>;
82 void iterate(GraphRef graph, const IterFn &fn) const;
83 void iterateOpSeqs(LoweredGraphRef lowered_graph, const OpSeqIterFn &f) const;
85 using PostDfsConstIterator = PostDfsIterator<true>;
90 #endif // __ONERT_IR_GRAPH_ITERATOR_H__