From: 박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 Date: Tue, 17 Jul 2018 05:05:54 +0000 (+0900) Subject: [nest] Add 'VarContext' class (#682) X-Git-Tag: nncc_backup~2404 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fe8f1bd531a1dd26da1e09dbd538183f82e305ec;p=platform%2Fcore%2Fml%2Fnnfw.git [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 --- 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); +}