Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / ir / OperandIndexSequence.h
1 /*
2  * Copyright (c) 2018 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_MODEL_OPERAND_INDEX_SEQUENCE_H__
18 #define __ONERT_MODEL_OPERAND_INDEX_SEQUENCE_H__
19
20 #include <initializer_list>
21 #include <vector>
22
23 #include "ir/Index.h"
24
25 namespace onert
26 {
27 namespace ir
28 {
29
30 enum class Remove
31 {
32   DUPLICATED = 1,
33   UNDEFINED = 2
34 };
35
36 class OperandIndexSequence
37 {
38 public:
39   OperandIndexSequence(void) = default;
40   OperandIndexSequence(std::initializer_list<OperandIndex> list);
41   OperandIndexSequence(std::initializer_list<int32_t> list);
42   OperandIndexSequence(std::initializer_list<uint32_t> list);
43
44 public:
45   void append(const OperandIndex &index) { _vec.emplace_back(index); }
46   void append(const OperandIndexSequence &l) { _vec.insert(_vec.end(), l.begin(), l.end()); }
47
48 public:
49   uint32_t size() const { return static_cast<uint32_t>(_vec.size()); }
50   const OperandIndex &at(IOIndex set_index) const { return _vec.at(set_index.value()); }
51   const OperandIndex &at(uint32_t index) const { return _vec.at(index); }
52   bool contains(const OperandIndex &index) const;
53   void replace(const OperandIndex &from, const OperandIndex &to);
54   OperandIndexSequence operator|(ir::Remove filter) const
55   {
56     switch (filter)
57     {
58       case ir::Remove::DUPLICATED:
59       {
60         ir::OperandIndexSequence seq;
61         for (const auto &ind : _vec)
62           if (!seq.contains(ind))
63             seq.append(ind);
64         return seq;
65       }
66       case ir::Remove::UNDEFINED:
67       {
68         ir::OperandIndexSequence seq;
69         for (const auto &ind : _vec)
70           if (!ind.undefined())
71             seq.append(ind);
72         return seq;
73       }
74     }
75     return *this;
76   }
77
78 public:
79   bool operator==(const OperandIndexSequence &other) const;
80   OperandIndexSequence operator+(const OperandIndexSequence &other) const;
81   friend std::ostream &operator<<(std::ostream &o, const OperandIndexSequence &operand_seq);
82
83 public:
84   std::vector<OperandIndex>::const_iterator begin(void) const { return _vec.begin(); }
85   std::vector<OperandIndex>::const_iterator end(void) const { return _vec.end(); }
86   std::vector<OperandIndex>::iterator begin(void) { return _vec.begin(); }
87   std::vector<OperandIndex>::iterator end(void) { return _vec.end(); }
88
89 private:
90   std::vector<OperandIndex> _vec;
91 };
92
93 } // namespace ir
94 } // namespace onert
95
96 #endif // __ONERT_MODEL_OPERAND_INDEX_SET_H__