ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ObjectManager.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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_UTIL_OBJECT_MANAGER_H__
18#define __ONERT_UTIL_OBJECT_MANAGER_H__
19
20#include "util/logging.h"
21
22#include <cassert>
23#include <functional>
24#include <list>
25#include <memory>
26#include <unordered_map>
27
28namespace onert
29{
30namespace util
31{
32
37template <typename Index, typename Object> class ObjectManager
38{
39public:
41
42public:
49 template <class... Args> Index emplace(Args &&...args)
50 {
51 auto index = generateIndex();
52 if (!index.valid())
53 return index;
54 _objects.emplace(index, std::make_unique<Object>(std::forward<Args>(args)...));
55 return index;
56 }
57
67 Index push(std::unique_ptr<Object> &&object, Index index)
68 {
69 auto gen_index = tryIndex(index);
70 if (gen_index.valid())
71 _objects.emplace(gen_index, std::move(object));
72 return gen_index;
73 }
82 Index push(std::unique_ptr<Object> &&object)
83 {
84 auto gen_index = generateIndex();
85 if (gen_index.valid())
86 _objects.emplace(gen_index, std::move(object));
87 return gen_index;
88 }
99 Index set(Index index, std::unique_ptr<Object> &&object)
100 {
101 if (index.valid())
102 _objects[index] = std::move(object);
103 return index;
104 }
111 void remove(const Index &index) { _objects.erase(index); }
112
121 const Object &at(const Index &index) const { return *(_objects.at(index)); }
130 Object &at(const Index &index) { return *(_objects.at(index)); }
139 const Object *getRawPtr(const Index &index) const
140 {
141 auto itr = _objects.find(index);
142 if (itr == _objects.end())
143 return nullptr;
144 else
145 {
146 assert(itr->second != nullptr);
147 return itr->second.get();
148 }
149 }
158 Object *getRawPtr(const Index &index)
159 {
160 return const_cast<Object *>(
161 const_cast<const ObjectManager<Index, Object> *>(this)->getRawPtr(index));
162 }
169 bool exist(const Index &index) const
170 {
171 auto it = _objects.find(index);
172 return it != _objects.end();
173 }
179 size_t size() const { return _objects.size(); }
186 void iterate(const std::function<void(const Index &, const Object &)> &fn) const
187 {
188 for (const auto &[index, obj] : _objects)
189 {
190 fn(index, *obj);
191 }
192 }
199 void iterate(const std::function<void(const Index &, Object &)> &fn)
200 {
201 // TODO Remove this workaround
202 // This implementation is a workaround in case of adding operands while iteration
203 std::list<Index> l;
204
205 for (const auto &e : _objects)
206 {
207 l.push_back(e.first);
208 }
209
210 for (const auto &index : l)
211 {
212 fn(index, *_objects[index]);
213 }
214 }
215
216private:
217 // Try assigning the given index
218 Index tryIndex(Index index)
219 {
220 if (!index.valid())
221 return index;
222 if (_objects.find(index) == _objects.end())
223 {
224 // If the given index does not exist, update the next index and return the index
225 if (index.value() >= _next_index)
226 _next_index = index.value() + 1;
227 return index;
228 }
229 else
230 {
231 // If the given index exists already, return a non-valid index
232 return Index{};
233 }
234 }
235
236 // Generate a new index with `_next_index`
237 Index generateIndex()
238 {
239 // No need to check if there is an entry with _next_index since
240 // _next_index is always ("the highest index in the object map" + 1)
241 if (Index{_next_index}.valid())
242 return Index{_next_index++};
243 else
244 return Index{};
245 }
246
247protected:
248 std::unordered_map<Index, std::unique_ptr<Object>> _objects;
249 uint32_t _next_index;
250};
251
252} // namespace util
253} // namespace onert
254
255#endif // __ONERT_UTIL_OBJECT_MANAGER_H__
A wrapper class for unsigned integral Index NOTE : Max value of the underlying type is used as the in...
Definition Index.h:39
Class that owns objects and maps them with indices as a handle for them.
Index set(Index index, std::unique_ptr< Object > &&object)
Set the object in the container with given index.
Object & at(const Index &index)
Get the object that is associated with the given index.
size_t size() const
Return the number of objects that the manager contains.
Index emplace(Args &&...args)
Create an object with args and put it in the container with a newly assigned Index.
void remove(const Index &index)
Remove the object that is associated with the given index.
Index push(std::unique_ptr< Object > &&object, Index index)
Put the object in the container with given index.
Index push(std::unique_ptr< Object > &&object)
Put the object in the container with a newly assigned index.
Object * getRawPtr(const Index &index)
Get the object that is associated with the given index.
void iterate(const std::function< void(const Index &, const Object &)> &fn) const
Iterate over the container with given function.
std::unordered_map< Index, std::unique_ptr< Object > > _objects
const Object & at(const Index &index) const
Get the object that is associated with the given index.
const Object * getRawPtr(const Index &index) const
Get the object that is associated with the given index.
void iterate(const std::function< void(const Index &, Object &)> &fn)
Iterate over the container with given function.
bool exist(const Index &index) const
Get the object that is associated with the given index.