[neurun] Graph : Introduce operation `Set` (#2215)
author이한종/동작제어Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Wed, 8 Aug 2018 11:07:23 +0000 (20:07 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 8 Aug 2018 11:07:23 +0000 (20:07 +0900)
`neurun::graph::operation::Set` holds operations and also has
ownership of them.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/neurun/src/graph/operation/Set.cc [new file with mode: 0644]
runtimes/neurun/src/graph/operation/Set.h [new file with mode: 0644]
runtimes/neurun/test/operation/Set.cc [new file with mode: 0644]

diff --git a/runtimes/neurun/src/graph/operation/Set.cc b/runtimes/neurun/src/graph/operation/Set.cc
new file mode 100644 (file)
index 0000000..5e1a2b0
--- /dev/null
@@ -0,0 +1,35 @@
+#include "Set.h"
+
+namespace neurun
+{
+namespace graph
+{
+namespace operation
+{
+
+Index Set::append(std::unique_ptr<Node> &&node)
+{
+  uint32_t index = _nodes.size();
+
+  _nodes.emplace_back(std::move(node));
+
+  return Index{index};
+}
+
+const Node &Set::at(const Index &index) const { return *(_nodes.at(index.asInt())); }
+
+Node &Set::at(const Index &index) { return *(_nodes.at(index.asInt())); }
+
+bool Set::exist(const Index &index) const { return index.asInt() < _nodes.size(); }
+
+void Set::iterate(const std::function<void(const Index &, const Node &)> &fn) const
+{
+  for (uint32_t index = 0; index < _nodes.size(); index++)
+  {
+    fn(Index{index}, *_nodes[index]);
+  }
+}
+
+} // namespace operation
+} // namespace graph
+} // namespace neurun
diff --git a/runtimes/neurun/src/graph/operation/Set.h b/runtimes/neurun/src/graph/operation/Set.h
new file mode 100644 (file)
index 0000000..d0bc536
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef __NEURUN_GRAPH_OPERATION_SET_H__
+#define __NEURUN_GRAPH_OPERATION_SET_H__
+
+#include <memory>
+
+#include "graph/operation/Index.h"
+#include "Node.h"
+#include "internal/Model.h"
+
+namespace neurun
+{
+namespace graph
+{
+namespace operation
+{
+
+class Set
+{
+public:
+  Set() = default;
+
+public:
+  Index append(std::unique_ptr<Node> &&node);
+
+public:
+  const Node &at(const Index &) const;
+  Node &at(const Index &);
+  bool exist(const Index &) const;
+  uint32_t size() const { return _nodes.size(); }
+  void iterate(const std::function<void(const Index &, const Node &)> &fn) const;
+
+private:
+  std::vector<std::unique_ptr<Node>> _nodes;
+};
+
+} // namespace operation
+} // namespace graph
+} // namespace neurun
+
+#endif // __NEURUN_GRAPH_OPERATION_SET_H__
diff --git a/runtimes/neurun/test/operation/Set.cc b/runtimes/neurun/test/operation/Set.cc
new file mode 100644 (file)
index 0000000..759618e
--- /dev/null
@@ -0,0 +1,27 @@
+#include <gtest/gtest.h>
+
+#include "graph/operation/Set.h"
+
+using neurun::graph::operation::Set;
+using neurun::graph::operation::Node;
+using neurun::graph::operation::Index;
+
+class TestNode : public Node
+{
+public:
+  TestNode() = default;
+
+public:
+  virtual neurun::graph::operand::IndexSet inputs() const { return {1, 2, 3, 4}; }
+  virtual neurun::graph::operand::IndexSet outputs() const { return {1, 2, 3}; }
+  virtual const ::internal::tflite::op::Node *op() const { return nullptr; }
+};
+
+TEST(graph_operation_Set, operation_test)
+{
+  Set set;
+  set.append(std::unique_ptr<Node>(new TestNode()));
+  Index idx{0u};
+  ASSERT_EQ(set.at(idx).inputs().size(), 4);
+  ASSERT_EQ(set.at(idx).outputs().size(), 3);
+}