[nest] Add 'DomainContext' (#700)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 18 Jul 2018 23:25:23 +0000 (08:25 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 18 Jul 2018 23:25:23 +0000 (08:25 +0900)
This commit adds 'DomainContext' class which creates a domain, and
manages its information.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/nest/include/nest/DomainContext.h [new file with mode: 0644]
contrib/nest/src/DomainContext.cpp [new file with mode: 0644]
contrib/nest/src/DomainContext.test.cpp [new file with mode: 0644]

diff --git a/contrib/nest/include/nest/DomainContext.h b/contrib/nest/include/nest/DomainContext.h
new file mode 100644 (file)
index 0000000..74f3da4
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef __NEST_DOMAIN_CONTEXT_H__
+#define __NEST_DOMAIN_CONTEXT_H__
+
+#include "nest/DomainInfo.h"
+#include "nest/Domain.h"
+
+#include <vector>
+
+namespace nest
+{
+
+class DomainContext
+{
+public:
+  uint32_t count(void) const;
+
+public:
+  Domain make(std::initializer_list<uint32_t> dims);
+
+public:
+  const DomainInfo &info(const Domain &) const;
+
+private:
+  std::vector<DomainInfo> _info;
+};
+
+} // namespace nest
+
+#endif // __NEST_DOMAIN_CONTEXT_H__
diff --git a/contrib/nest/src/DomainContext.cpp b/contrib/nest/src/DomainContext.cpp
new file mode 100644 (file)
index 0000000..c430809
--- /dev/null
@@ -0,0 +1,22 @@
+#include "nest/DomainContext.h"
+
+namespace nest
+{
+
+uint32_t DomainContext::count(void) const { return _info.size(); }
+
+Domain DomainContext::make(std::initializer_list<uint32_t> dims)
+{
+  const DomainID domain_id{count()};
+
+  _info.emplace_back(dims);
+
+  return Domain{domain_id};
+}
+
+const DomainInfo &DomainContext::info(const Domain &dom) const
+{
+  return _info.at(dom.id().value());
+}
+
+} // namespace nest
diff --git a/contrib/nest/src/DomainContext.test.cpp b/contrib/nest/src/DomainContext.test.cpp
new file mode 100644 (file)
index 0000000..64df588
--- /dev/null
@@ -0,0 +1,40 @@
+#include "nest/DomainContext.h"
+
+#include <gtest/gtest.h>
+
+TEST(DOMAIN_CONTEXT, usecase)
+{
+  nest::DomainContext ctx;
+
+  auto dom_0 = ctx.make({1, 3, 4});
+
+  ASSERT_EQ(ctx.count(), 1);
+
+  auto check_dom_0 = [&](void) {
+    ASSERT_EQ(ctx.info(dom_0).rank(), 3);
+    ASSERT_EQ(ctx.info(dom_0).dim(0), 1);
+    ASSERT_EQ(ctx.info(dom_0).dim(1), 3);
+    ASSERT_EQ(ctx.info(dom_0).dim(2), 4);
+  };
+
+  check_dom_0();
+
+  auto dom_1 = ctx.make({7, 6, 2, 1});
+
+  ASSERT_EQ(ctx.count(), 2);
+
+  // Domain ID should be unique for each domain
+  ASSERT_FALSE(dom_0.id() == dom_1.id());
+
+  auto check_dom_1 = [&](void) {
+    ASSERT_EQ(ctx.info(dom_1).rank(), 4);
+    ASSERT_EQ(ctx.info(dom_1).dim(0), 7);
+    ASSERT_EQ(ctx.info(dom_1).dim(1), 6);
+    ASSERT_EQ(ctx.info(dom_1).dim(2), 2);
+    ASSERT_EQ(ctx.info(dom_1).dim(3), 1);
+  };
+
+  // make() SHOULD NOT affect the existing domain information
+  check_dom_0();
+  check_dom_1();
+}