From aa35f80d60da3e945cd7bb2d1e2b91553cac6104 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: Fri, 4 May 2018 17:37:44 +0900 Subject: [PATCH] [core.ADT.tensor] Add 'TextFormatted' class (#202) This commit adds 'TextFormatted --- .../include/nncc/core/ADT/tensor/TextFormatted.h | 22 +++++++++++++ .../nncc/core/ADT/tensor/TextFormattedIndex.h | 38 ++++++++++++++++++++++ .../nncc/core/ADT/tensor/TextFormattedIndex.cpp | 33 +++++++++++++++++++ .../core/ADT/tensor/TextFormattedIndex.test.cpp | 17 ++++++++++ 4 files changed, 110 insertions(+) create mode 100644 libs/core/include/nncc/core/ADT/tensor/TextFormatted.h create mode 100644 libs/core/include/nncc/core/ADT/tensor/TextFormattedIndex.h create mode 100644 libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.cpp create mode 100644 libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.test.cpp diff --git a/libs/core/include/nncc/core/ADT/tensor/TextFormatted.h b/libs/core/include/nncc/core/ADT/tensor/TextFormatted.h new file mode 100644 index 0000000..261deca --- /dev/null +++ b/libs/core/include/nncc/core/ADT/tensor/TextFormatted.h @@ -0,0 +1,22 @@ +#ifndef __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_H__ +#define __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_H__ + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +template struct TextFormatted; + +template TextFormatted txtfmt(const T &obj) { return TextFormatted{obj}; } + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_INDEX_H__ diff --git a/libs/core/include/nncc/core/ADT/tensor/TextFormattedIndex.h b/libs/core/include/nncc/core/ADT/tensor/TextFormattedIndex.h new file mode 100644 index 0000000..58c3636 --- /dev/null +++ b/libs/core/include/nncc/core/ADT/tensor/TextFormattedIndex.h @@ -0,0 +1,38 @@ +#ifndef __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_INDEX_H__ +#define __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_INDEX_H__ + +#include "nncc/core/ADT/tensor/Index.h" +#include "nncc/core/ADT/tensor/TextFormatted.h" + +#include "nncc/core/ADT/Printable.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +template <> class TextFormatted final : public nncc::core::ADT::Printable +{ +public: + explicit TextFormatted(const Index &index) : _index(index) + { + // DO NOTHING + } + +public: + void print(std::ostream &os) const override; + +private: + const Index &_index; +}; + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc + +#endif // __NNCC_CORE_ADT_TENSOR_TEXT_FORMATTED_INDEX_H__ diff --git a/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.cpp b/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.cpp new file mode 100644 index 0000000..7bb1ba5 --- /dev/null +++ b/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.cpp @@ -0,0 +1,33 @@ +#include "nncc/core/ADT/tensor/TextFormattedIndex.h" + +namespace nncc +{ +namespace core +{ +namespace ADT +{ +namespace tensor +{ + +void TextFormatted::print(std::ostream &os) const +{ + const auto rank = _index.rank(); + + if (rank > 0) + { + os << _index.at(0); + + if (rank > 1) + { + for (uint32_t axis = 1; axis < rank; ++axis) + { + os << ", " << _index.at(axis); + } + } + } +} + +} // namespace tensor +} // namespace ADT +} // namespace core +} // namespace nncc diff --git a/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.test.cpp b/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.test.cpp new file mode 100644 index 0000000..8ebe318 --- /dev/null +++ b/libs/core/src/nncc/core/ADT/tensor/TextFormattedIndex.test.cpp @@ -0,0 +1,17 @@ +#include "nncc/core/ADT/tensor/TextFormattedIndex.h" + +#include +#include + +#include + +TEST(ADT_TENSOR_TEXT_FORMATTED_INDEX, message) +{ + const nncc::core::ADT::tensor::Index index{2, 3}; + + std::stringstream ss; + + ss << nncc::core::ADT::tensor::txtfmt(index); + + ASSERT_EQ(ss.str(), "2, 3"); +} -- 2.7.4