[core.ADT.tensor] Add 'range' function (#205)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 8 May 2018 01:09:13 +0000 (10:09 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 8 May 2018 01:09:13 +0000 (10:09 +0900)
This commit adds 'range' function which creates a 'IndexRange'
object from a 'Shape' object.

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

index df0bf7f..e592cc4 100644 (file)
@@ -35,6 +35,8 @@ private:
   const Shape _shape;
 };
 
+IndexRange range(const Shape &shape);
+
 } // namespace tensor
 } // namespace ADT
 } // namespace core
index 5636c25..9d10e1b 100644 (file)
@@ -86,6 +86,8 @@ void IndexRange::iterate(const std::function<void(const Index &)> &f) const
   }
 }
 
+IndexRange range(const Shape &shape) { return IndexRange{shape}; }
+
 } // namespace tensor
 } // namespace ADT
 } // namespace core
index 5537af4..bcf3cfb 100644 (file)
@@ -61,3 +61,22 @@ TEST(ADT_TENSOR_INDEX_RANGE, iterate_full_range)
 
   ASSERT_TRUE(std::all_of(count.begin(), count.end(), [](uint32_t n) { return n == 1; }));
 }
+
+TEST(ADT_TENSOR_INDEX_RANGE, range)
+{
+  const nncc::core::ADT::tensor::Shape shape{3, 4};
+
+  std::array<uint32_t, 3 * 4> count;
+
+  count.fill(0);
+
+  using nncc::core::ADT::tensor::Index;
+  using nncc::core::ADT::tensor::range;
+
+  range(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; }));
+}