namespace feature
{
-struct Layout
+class Layout
{
- virtual ~Layout() = default;
+public:
+ using Func = uint32_t (*)(const Shape &, uint32_t ch, uint32_t row, uint32_t col);
- virtual uint32_t offset(const Shape &, uint32_t ch, uint32_t row, uint32_t col) const = 0;
+public:
+ explicit Layout(const Func &func);
+
+public:
+ uint32_t offset(const Shape &shape, uint32_t ch, uint32_t row, uint32_t col) const
+ {
+ return _func(shape, ch, row, col);
+ }
+
+private:
+ Func _func;
};
} // namespace feature
#include "nncc/core/ADT/feature/CHWLayout.h"
+using nncc::core::ADT::feature::Shape;
+
+static uint32_t CHW_offset(const Shape &shape, uint32_t ch, uint32_t row, uint32_t col)
+{
+ return (ch * shape.height() + row) * shape.width() + col;
+}
+
namespace nncc
{
namespace core
namespace feature
{
-uint32_t CHWLayout::offset(const Shape &shape, uint32_t ch, uint32_t row, uint32_t col) const
+CHWLayout::CHWLayout() : Layout{CHW_offset}
{
- return (ch * shape.height() + row) * shape.width() + col;
+ // DO NOTHING
}
} // namespace feature
--- /dev/null
+#include "nncc/core/ADT/feature/Layout.h"
+
+#include <gtest/gtest.h>
+
+using nncc::core::ADT::feature::Shape;
+using nncc::core::ADT::feature::Layout;
+
+static uint32_t offset_0(const Shape &, uint32_t, uint32_t, uint32_t) { return 0; }
+static uint32_t offset_1(const Shape &, uint32_t, uint32_t, uint32_t) { return 1; }
+
+TEST(ADT_FEATURE_LAYOUT, ctor)
+{
+ Layout l{offset_0};
+
+ ASSERT_EQ(l.offset(Shape{4, 3, 6}, 1, 1, 1), 0);
+}
+
+TEST(ADT_FEATURE_LAYOUT, copy)
+{
+ Layout orig{offset_0};
+ Layout copy{offset_1};
+
+ ASSERT_EQ(copy.offset(Shape{4, 3, 6}, 1, 1, 1), 1);
+
+ copy = orig;
+
+ ASSERT_EQ(copy.offset(Shape{4, 3, 6}, 1, 1, 1), 0);
+}
+
+TEST(ADT_FEATURE_LAYOUT, move)
+{
+ Layout orig{offset_0};
+ Layout move{offset_1};
+
+ ASSERT_EQ(move.offset(Shape{4, 3, 6}, 1, 1, 1), 1);
+
+ move = std::move(orig);
+
+ ASSERT_EQ(move.offset(Shape{4, 3, 6}, 1, 1, 1), 0);
+}