namespace tensor
{
-struct Layout
+class Layout
{
- virtual ~Layout() = default;
+public:
+ using Func = uint32_t (*)(const Shape &, const Index &);
- virtual uint32_t offset(const Shape &, const Index &) const = 0;
+public:
+ explicit Layout(const Func &func);
+
+public:
+ uint32_t offset(const Shape &shape, const Index &index) const { return _func(shape, index); }
+
+private:
+ Func _func;
};
} // namespace tensor
--- /dev/null
+#include "nncc/core/ADT/tensor/Layout.h"
+
+#include <gtest/gtest.h>
+
+using nncc::core::ADT::tensor::Shape;
+using nncc::core::ADT::tensor::Index;
+
+static uint32_t offset_0(const Shape &, const Index &) { return 0; }
+static uint32_t offset_1(const Shape &, const Index &) { return 1; }
+
+TEST(ADT_TENSOR_LAYOUT, ctor)
+{
+ nncc::core::ADT::tensor::Layout l{offset_0};
+
+ ASSERT_EQ(l.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 0);
+}
+
+TEST(ADT_TENSOR_LAYOUT, copy)
+{
+ nncc::core::ADT::tensor::Layout orig{offset_0};
+ nncc::core::ADT::tensor::Layout copy{offset_1};
+
+ ASSERT_EQ(copy.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 1);
+
+ copy = orig;
+
+ ASSERT_EQ(copy.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 0);
+}
+
+TEST(ADT_TENSOR_LAYOUT, move)
+{
+ nncc::core::ADT::tensor::Layout orig{offset_0};
+ nncc::core::ADT::tensor::Layout move{offset_1};
+
+ ASSERT_EQ(move.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 1);
+
+ move = std::move(orig);
+
+ ASSERT_EQ(move.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 0);
+}
#include <cassert>
-namespace nncc
-{
-namespace core
-{
-namespace ADT
-{
-namespace tensor
-{
+using nncc::core::ADT::tensor::Shape;
+using nncc::core::ADT::tensor::Index;
-uint32_t LexicalLayout::offset(const Shape &shape, const Index &index) const
+// NOTE This forward declaration is introduced to minimize code diff
+static uint32_t lexical_offset(const Shape &shape, const Index &index)
{
assert(shape.rank() > 0);
assert(shape.rank() == index.rank());
return res;
}
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace tensor
+{
+
+LexicalLayout::LexicalLayout() : Layout(lexical_offset)
+{
+ // DO NOTHING
+}
+
} // namespace tensor
} // namespace ADT
} // namespace core