[core.ADT] Simplify tensor view construction (#383)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 27 Jun 2018 02:43:46 +0000 (11:43 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 27 Jun 2018 02:43:46 +0000 (11:43 +0900)
The current implementation of tensor::View<T> takes an owned pointer
of Layout (std::unique_ptr<Layout>) as its argument.

This design was enforced by the constraint that Layout is a pure
virtual class. Recently, this constraint is gone. Layout class is now
a simple class which is copyable and movable.

This commit revises tensor view constructor to take Layout instead of
std::unique_ptr<Layout> to make it easy to build tensor view.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
libs/core/include/nncc/core/ADT/tensor/View.h

index 96ef5ab..c4a1458 100644 (file)
@@ -23,7 +23,7 @@ namespace tensor
 template <typename T> class View final : public Reader<T>, public Accessor<T>
 {
 public:
-  explicit View(const Shape &shape, std::unique_ptr<Layout> &&layout,
+  explicit View(const Shape &shape, const Layout &layout,
                 std::unique_ptr<nncc::foundation::Region<T>> &&region)
       : _shape{shape}, _layout{std::move(layout)}, _region{std::move(region)}
   {
@@ -33,18 +33,18 @@ public:
 public:
   T at(const Index &index) const override
   {
-    return *(_region->base() + _layout->offset(_shape, index));
+    return *(_region->base() + _layout.offset(_shape, index));
   }
 
 public:
-  T &at(const Index &index) override { return *(_region->base() + _layout->offset(_shape, index)); }
+  T &at(const Index &index) override { return *(_region->base() + _layout.offset(_shape, index)); }
 
 public:
   const Shape &shape(void) const { return _shape; }
 
 private:
   const Shape _shape;
-  std::unique_ptr<Layout> _layout;
+  const Layout _layout;
 
 private:
   std::unique_ptr<nncc::foundation::Region<T>> _region;
@@ -53,8 +53,7 @@ private:
 template <typename T, typename LayoutImpl>
 View<T> make_view(const Shape &shape, std::unique_ptr<nncc::foundation::Region<T>> &&region)
 {
-  std::unique_ptr<Layout> layout(new LayoutImpl{});
-  return View<T>{shape, std::move(layout), std::move(region)};
+  return View<T>{shape, LayoutImpl{}, std::move(region)};
 }
 
 } // namespace tensor