[nncc.core] Add 'Iterable' class (#189)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 2 May 2018 10:50:38 +0000 (19:50 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 2 May 2018 10:50:38 +0000 (19:50 +0900)
This commit adds 'Iterable' class which faciliates the use of
'iterate' method based on lambda.

This commit also revises 'tensor::IndexRange' class using newly
introduced 'Iterable' class.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
libs/core/include/nncc/core/ADT/Iterable.h [new file with mode: 0644]
libs/core/include/nncc/core/ADT/tensor/IndexRange.h
libs/core/src/nncc/core/ADT/tensor/IndexRange.test.cpp

diff --git a/libs/core/include/nncc/core/ADT/Iterable.h b/libs/core/include/nncc/core/ADT/Iterable.h
new file mode 100644 (file)
index 0000000..2fcc541
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef __NNCC_CORE_ADT_ITERABLE_H__
+#define __NNCC_CORE_ADT_ITERABLE_H__
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+
+template <typename T> class Iterable
+{
+public:
+  Iterable(const T *ptr) : _ptr{ptr}
+  {
+    // DO NOTHING
+  }
+
+public:
+  const T &get(void) const { return *_ptr; }
+
+private:
+  const T *const _ptr;
+};
+
+template <typename T, typename Callable>
+const Iterable<T> &operator<<(const Iterable<T> &it, Callable cb)
+{
+  it.get().iterate(cb);
+  return it;
+}
+
+} // namespace ADT
+} // namespace core
+} // namespace nncc
+
+#endif // __NNCC_CORE_ADT_ITERABLE_H__
index f28f3d0..df0bf7f 100644 (file)
@@ -4,6 +4,8 @@
 #include "nncc/core/ADT/tensor/Index.h"
 #include "nncc/core/ADT/tensor/Shape.h"
 
+#include "nncc/core/ADT/Iterable.h"
+
 #include <functional>
 
 namespace nncc
@@ -26,6 +28,9 @@ public:
 public:
   void iterate(const std::function<void(const Index &)> &) const;
 
+public:
+  Iterable<IndexRange> iterate(void) const { return Iterable<IndexRange>{this}; }
+
 private:
   const Shape _shape;
 };
index 556d64c..f15efe7 100644 (file)
@@ -60,9 +60,10 @@ TEST(ADT_TENSOR_INDEX_RANGE, iterate_empty_range)
 
   uint32_t count = 0;
 
-  auto f = [&count](const nncc::core::ADT::tensor::Index &) { ++count; };
+  using nncc::core::ADT::tensor::Index;
+  using nncc::core::ADT::tensor::IndexRange;
 
-  nncc::core::ADT::tensor::IndexRange{shape}.iterate(f);
+  nncc::core::ADT::tensor::IndexRange{shape}.iterate() << [&count](const Index &) { ++count; };
 
   ASSERT_EQ(count, 0);
 }
@@ -79,11 +80,13 @@ TEST(ADT_TENSOR_INDEX_RANGE, iterate_full_range)
 
   count.fill(0);
 
-  nncc::core::ADT::tensor::IndexRange{shape}.iterate(
-      [&count](const nncc::core::ADT::tensor::Index &i) {
-        ASSERT_EQ(i.rank(), 2);
-        count.at(i.at(0) * 4 + i.at(1)) += 1;
-      });
+  using nncc::core::ADT::tensor::Index;
+  using nncc::core::ADT::tensor::IndexRange;
+
+  IndexRange{shape}.iterate() << [&count](const Index &i) {
+    ASSERT_EQ(i.rank(), 2);
+    count.at(i.at(0) * 4 + i.at(1)) += 1;
+  };
 
   ASSERT_TRUE(std::all_of(count.begin(), count.end(), [](uint32_t n) { return n == 1; }));
 }