From fe8f1bd531a1dd26da1e09dbd538183f82e305ec Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 17 Jul 2018 14:05:54 +0900 Subject: [PATCH] [nest] Add 'VarContext' class (#682) This commit adds 'VarContext' class which manages information related with each variable (such as bound). Signed-off-by: Jonghyun Park --- contrib/nest/include/nest/VarContext.h | 30 ++++++++++++++++ contrib/nest/src/VarContext.cpp | 20 +++++++++++ contrib/nest/src/VarContext.test.cpp | 66 ++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 contrib/nest/include/nest/VarContext.h create mode 100644 contrib/nest/src/VarContext.cpp create mode 100644 contrib/nest/src/VarContext.test.cpp diff --git a/contrib/nest/include/nest/VarContext.h b/contrib/nest/include/nest/VarContext.h new file mode 100644 index 0000000..afc6964 --- /dev/null +++ b/contrib/nest/include/nest/VarContext.h @@ -0,0 +1,30 @@ +#ifndef __NEST_VAR_CONTEXT_H__ +#define __NEST_VAR_CONTEXT_H__ + +#include "nest/Bound.h" +#include "nest/Var.h" + +#include + +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; +}; + +} // 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 index 0000000..d32a64d --- /dev/null +++ b/contrib/nest/src/VarContext.cpp @@ -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 index 0000000..4b8f841 --- /dev/null +++ b/contrib/nest/src/VarContext.test.cpp @@ -0,0 +1,66 @@ +#include "nest/VarContext.h" + +#include + +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); +} -- 2.7.4