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>
34 template <bool is_const> class Iterator
37 using GraphRef = typename std::conditional<is_const, const Graph &, Graph &>::type;
38 using IndexRef = const OperationIndex &;
39 using NodeRef = typename std::conditional<is_const, const Operation &, Operation &>::type;
40 using IterFn = std::function<void(IndexRef, NodeRef)>;
43 virtual ~Iterator() = default;
44 virtual void iterate(GraphRef graph, const IterFn &fn) const = 0;
47 template <bool is_const = false> class DefaultIterator final : public Iterator<is_const>
50 using GraphRef = typename Iterator<is_const>::GraphRef;
51 using IndexRef = typename Iterator<is_const>::IndexRef;
52 using NodeRef = typename Iterator<is_const>::NodeRef;
53 using IterFn = typename Iterator<is_const>::IterFn;
56 void iterate(GraphRef graph, const IterFn &fn) const;
58 using DefaultConstIterator = DefaultIterator<true>;
60 template <bool is_const = false> class PostDfsIterator final : public Iterator<is_const>
63 using GraphRef = typename Iterator<is_const>::GraphRef;
64 using IndexRef = typename Iterator<is_const>::IndexRef;
65 using NodeRef = typename Iterator<is_const>::NodeRef;
66 using IterFn = typename Iterator<is_const>::IterFn;
67 using LoweredGraphRef =
68 typename std::conditional<is_const, const LoweredGraph &, LoweredGraph &>::type;
69 using OpSequenceRef = typename std::conditional<is_const, const OpSequence &, OpSequence &>::type;
70 using OpSeqIndexRef = const OpSequenceIndex &;
71 using OpSeqIterFn = std::function<void(OpSeqIndexRef, OpSequenceRef)>;
74 void iterate(GraphRef graph, const IterFn &fn) const;
75 void iterateOpSeqs(LoweredGraphRef lowered_graph, const OpSeqIterFn &f) const;
77 using PostDfsConstIterator = PostDfsIterator<true>;
82 #endif // __ONERT_IR_GRAPH_ITERATOR_H__