[nest] Add 'VarContext' class (#682)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 17 Jul 2018 05:05:54 +0000 (14:05 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 17 Jul 2018 05:05:54 +0000 (14:05 +0900)
This commit adds 'VarContext' class which manages information related
with each variable (such as bound).

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

diff --git a/contrib/nest/include/nest/VarContext.h b/contrib/nest/include/nest/VarContext.h
new file mode 100644 (file)
index 0000000..afc6964
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef __NEST_VAR_CONTEXT_H__
+#define __NEST_VAR_CONTEXT_H__
+
+#include "nest/Bound.h"
+#include "nest/Var.h"
+
+#include <vector>
+
+namespace nest
+{
+
+class VarContext
+{
+public:
+  uint32_t count(void) const;
+
+public:
+  Var make(void);
+
+public:
+  Bound &bound(const Var &);
+  const Bound &bound(const Var &) const;
+
+private:
+  std::vector<Bound> _bound;
+};
+
+} // namespace nest
+
+#endif // __NEST_VAR_CONTEXT_H__
diff --git a/contrib/nest/src/VarContext.cpp b/contrib/nest/src/VarContext.cpp
new file mode 100644 (file)
index 0000000..d32a64d
--- /dev/null
@@ -0,0 +1,20 @@
+#include "nest/VarContext.h"
+
+namespace nest
+{
+
+uint32_t VarContext::count(void) const { return _bound.size(); }
+
+Var VarContext::make(void)
+{
+  const VarID vid{count()};
+
+  _bound.emplace_back(0, 0);
+
+  return Var{vid};
+}
+
+Bound &VarContext::bound(const Var &var) { return _bound.at(var.id().value()); }
+const Bound &VarContext::bound(const Var &var) const { return _bound.at(var.id().value()); }
+
+} // namespace nest
diff --git a/contrib/nest/src/VarContext.test.cpp b/contrib/nest/src/VarContext.test.cpp
new file mode 100644 (file)
index 0000000..4b8f841
--- /dev/null
@@ -0,0 +1,66 @@
+#include "nest/VarContext.h"
+
+#include <gtest/gtest.h>
+
+TEST(VAR_CONTEXT, make)
+{
+  nest::VarContext ctx;
+
+  auto var_0 = ctx.make();
+  auto var_1 = ctx.make();
+
+  ASSERT_FALSE(var_0.id() == var_1.id());
+}
+
+TEST(VAR_CONTEXT, count)
+{
+  nest::VarContext ctx;
+
+  ASSERT_EQ(ctx.count(), 0);
+
+  auto var_0 = ctx.make();
+
+  ASSERT_EQ(ctx.count(), 1);
+
+  auto var_1 = ctx.make();
+
+  ASSERT_EQ(ctx.count(), 2);
+}
+
+TEST(VAR_CONTEXT, bound_one)
+{
+  nest::VarContext ctx;
+
+  auto var_0 = ctx.make();
+
+  ASSERT_EQ(ctx.bound(var_0).min(), 0);
+  ASSERT_EQ(ctx.bound(var_0).max(), 0);
+
+  ctx.bound(var_0) = nest::Bound{-3, 5};
+
+  ASSERT_EQ(ctx.bound(var_0).min(), -3);
+  ASSERT_EQ(ctx.bound(var_0).max(), 5);
+}
+
+TEST(VAR_CONTEXT, bound_independent)
+{
+  nest::VarContext ctx;
+
+  auto var_0 = ctx.make();
+
+  ASSERT_EQ(ctx.bound(var_0).min(), 0);
+  ASSERT_EQ(ctx.bound(var_0).max(), 0);
+
+  auto var_1 = ctx.make();
+
+  ASSERT_EQ(ctx.bound(var_1).min(), 0);
+  ASSERT_EQ(ctx.bound(var_1).max(), 0);
+
+  ctx.bound(var_0) = nest::Bound{-3, 5};
+
+  ASSERT_EQ(ctx.bound(var_0).min(), -3);
+  ASSERT_EQ(ctx.bound(var_0).max(), 5);
+
+  ASSERT_EQ(ctx.bound(var_1).min(), 0);
+  ASSERT_EQ(ctx.bound(var_1).max(), 0);
+}