[core.ADT] Iterator-based kernel overlay (#428)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 2 Jul 2018 23:45:47 +0000 (08:45 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 2 Jul 2018 23:45:47 +0000 (08:45 +0900)
This commit revises 'core::ADT::kernel::Overlay' to use iterator instead
of a pointer.

This change allows us to use kernel overlay over any STL-compatible
sequences that provides random iterator.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
libs/core/include/nncc/core/ADT/kernel/Buffer.h
libs/core/include/nncc/core/ADT/kernel/Overlay.h
libs/core/include/nncc/core/ADT/kernel/View.h
libs/core/include/nncc/core/ADT/kernel/ViewImpl.h [new file with mode: 0644]

index d8993ba..1b5ee90 100644 (file)
@@ -2,6 +2,7 @@
 #define __NNCC_CORE_ADT_KERNEL_BUFFER_H__
 
 #include "nncc/core/ADT/kernel/View.h"
+#include "nncc/core/ADT/kernel/ViewImpl.h"
 
 #include <vector>
 
@@ -17,17 +18,29 @@ namespace kernel
 template <typename T> class Buffer final : public View<T>
 {
 public:
-  explicit Buffer(const Shape &shape, const Layout &layout) : View<T>{shape, layout}
+  explicit Buffer(const Shape &shape, const Layout &layout) : _impl{shape, layout}
   {
     _buffer.resize(num_elements(shape));
   }
 
 public:
-  virtual T *base(void) { return _buffer.data(); }
-  virtual const T *base(void) const { return _buffer.data(); }
+  T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const override
+  {
+    return _impl.at(_buffer.begin(), nth, ch, row, col);
+  }
+
+public:
+  T &at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) override
+  {
+    return _impl.at(_buffer.begin(), nth, ch, row, col);
+  }
+
+public:
+  const Shape &shape(void) const { return _impl.shape(); }
 
 private:
   std::vector<T> _buffer;
+  ViewImpl<T> _impl;
 };
 
 template <typename T, typename LayoutImpl> Buffer<T> make_buffer(const Shape &shape)
index 1de4e7f..19756f2 100644 (file)
@@ -2,6 +2,7 @@
 #define __NNCC_CORE_ADT_KERNEL_OVERLAY_H__
 
 #include "nncc/core/ADT/kernel/View.h"
+#include "nncc/core/ADT/kernel/ViewImpl.h"
 
 #include <vector>
 
@@ -14,26 +15,46 @@ namespace ADT
 namespace kernel
 {
 
-template <typename T> class Overlay final : public View<T>
+template <typename T, typename InputIt> class Overlay final : public View<T>
 {
 public:
-  explicit Overlay(const Shape &shape, const Layout &layout, T *base)
-      : View<T>{shape, layout}, _base{base}
+  explicit Overlay(const Shape &shape, const Layout &layout, InputIt it)
+      : _impl{shape, layout}, _it{it}
   {
     // DO NOTHING
   }
 
 public:
-  virtual T *base(void) { return _base; }
-  virtual const T *base(void) const { return _base; }
+  T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const override
+  {
+    return _impl.at(_it, nth, ch, row, col);
+  }
+
+public:
+  T &at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) override
+  {
+    return _impl.at(_it, nth, ch, row, col);
+  }
+
+public:
+  const Shape &shape(void) const { return _impl.shape(); }
 
 private:
-  T *const _base;
+  InputIt const _it;
+  ViewImpl<T> _impl;
+};
+
+template <typename T, typename LayoutImpl> struct OverlayFactory
+{
+  template <typename InputIt> static Overlay<T, InputIt> make(const Shape &shape, InputIt it)
+  {
+    return Overlay<T, InputIt>{shape, LayoutImpl{}, it};
+  }
 };
 
-template <typename T, typename LayoutImpl> Overlay<T> make_overlay(const Shape &shape, T *base)
+template <typename T, typename LayoutImpl> Overlay<T, T *> make_overlay(const Shape &shape, T *base)
 {
-  return Overlay<T>{shape, LayoutImpl{}, base};
+  return OverlayFactory<T, LayoutImpl>::make(shape, base);
 }
 
 } // namespace kernel
index 9b8dd67..2313af7 100644 (file)
@@ -3,7 +3,6 @@
 
 #include "nncc/core/ADT/kernel/Shape.h"
 #include "nncc/core/ADT/kernel/Reader.h"
-#include "nncc/core/ADT/kernel/Layout.h"
 
 namespace nncc
 {
@@ -14,36 +13,11 @@ namespace ADT
 namespace kernel
 {
 
-template <typename T> class View : public Reader<T>
+template <typename T> struct View : public Reader<T>
 {
-public:
-  explicit View(const Shape &shape, const Layout &layout) : _shape{shape}, _layout{layout}
-  {
-    // DO NOTHING
-  }
+  virtual T &at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) = 0;
 
-public:
-  virtual T *base(void) = 0;
-  virtual const T *base(void) const = 0;
-
-public:
-  T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const override final
-  {
-    return *(base() + _layout.offset(_shape, nth, ch, row, col));
-  }
-
-public:
-  T &at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col)
-  {
-    return *(base() + _layout.offset(_shape, nth, ch, row, col));
-  }
-
-public:
-  const Shape &shape(void) const { return _shape; }
-
-private:
-  const Shape _shape;
-  const Layout _layout;
+  virtual const Shape &shape(void) const = 0;
 };
 
 } // namespace kernel
diff --git a/libs/core/include/nncc/core/ADT/kernel/ViewImpl.h b/libs/core/include/nncc/core/ADT/kernel/ViewImpl.h
new file mode 100644 (file)
index 0000000..5c8bc8d
--- /dev/null
@@ -0,0 +1,51 @@
+#ifndef __NNCC_CORE_ADT_KERNEL_VIEW_IMPL_H__
+#define __NNCC_CORE_ADT_KERNEL_VIEW_IMPL_H__
+
+#include "nncc/core/ADT/kernel/Shape.h"
+#include "nncc/core/ADT/kernel/Layout.h"
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace kernel
+{
+
+template <typename T> class ViewImpl
+{
+public:
+  explicit ViewImpl(const Shape &shape, const Layout &layout) : _shape{shape}, _layout{layout}
+  {
+    // DO NOTHING
+  }
+
+public:
+  template <typename InputIt>
+  T at(InputIt it, uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const
+  {
+    return *(it + _layout.offset(_shape, nth, ch, row, col));
+  }
+
+public:
+  template <typename InputIt>
+  T &at(InputIt it, uint32_t nth, uint32_t ch, uint32_t row, uint32_t col)
+  {
+    return *(it + _layout.offset(_shape, nth, ch, row, col));
+  }
+
+public:
+  const Shape &shape(void) const { return _shape; }
+
+private:
+  const Shape _shape;
+  const Layout _layout;
+};
+
+} // namespace kernel
+} // namespace ADT
+} // namespace core
+} // namespace nncc
+
+#endif // __NNCC_CORE_ADT_KERNEL_VIEW_IMPL_H__