From a0d8d0727de03c08a8a95c566fbc468ac3791e59 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Senior=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 8 May 2018 10:09:13 +0900 Subject: [PATCH] [core.ADT.tensor] Add 'range' function (#205) This commit adds 'range' function which creates a 'IndexRange' object from a 'Shape' object. Signed-off-by: Jonghyun Park --- libs/core/include/nncc/core/ADT/tensor/IndexRange.h | 2 ++ libs/core/src/nncc/core/ADT/tensor/IndexRange.cpp | 2 ++ .../core/src/nncc/core/ADT/tensor/IndexRange.test.cpp | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/libs/core/include/nncc/core/ADT/tensor/IndexRange.h b/libs/core/include/nncc/core/ADT/tensor/IndexRange.h index df0bf7f..e592cc4 100644 --- a/libs/core/include/nncc/core/ADT/tensor/IndexRange.h +++ b/libs/core/include/nncc/core/ADT/tensor/IndexRange.h @@ -35,6 +35,8 @@ private: const Shape _shape; }; +IndexRange range(const Shape &shape); + } // namespace tensor } // namespace ADT } // namespace core diff --git a/libs/core/src/nncc/core/ADT/tensor/IndexRange.cpp b/libs/core/src/nncc/core/ADT/tensor/IndexRange.cpp index 5636c25..9d10e1b 100644 --- a/libs/core/src/nncc/core/ADT/tensor/IndexRange.cpp +++ b/libs/core/src/nncc/core/ADT/tensor/IndexRange.cpp @@ -86,6 +86,8 @@ void IndexRange::iterate(const std::function &f) const } } +IndexRange range(const Shape &shape) { return IndexRange{shape}; } + } // namespace tensor } // namespace ADT } // namespace core diff --git a/libs/core/src/nncc/core/ADT/tensor/IndexRange.test.cpp b/libs/core/src/nncc/core/ADT/tensor/IndexRange.test.cpp index 5537af4..bcf3cfb 100644 --- a/libs/core/src/nncc/core/ADT/tensor/IndexRange.test.cpp +++ b/libs/core/src/nncc/core/ADT/tensor/IndexRange.test.cpp @@ -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 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; })); +} -- 2.7.4