diff --git a/DbCoreLib.pro b/DbCoreLib.pro new file mode 100644 index 0000000..5129a26 --- /dev/null +++ b/DbCoreLib.pro @@ -0,0 +1,21 @@ +QT += core +QT -= gui widgets + +PROJECT_ROOT = ../.. + +TARGET = dbcore + +DEFINES += DBCORELIB_LIBRARY + +SOURCES += + +HEADERS += dbcorelib_global.h \ + fixedsizematrix.h + +FORMS += + +RESOURCES += + +TRANSLATIONS += + +include($${PROJECT_ROOT}/lib.pri) diff --git a/dbcorelib_global.h b/dbcorelib_global.h new file mode 100644 index 0000000..4c1dfa8 --- /dev/null +++ b/dbcorelib_global.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +#if defined(DBCORELIB_LIBRARY) +# define DBCORELIB_EXPORT Q_DECL_EXPORT +#else +# define DBCORELIB_EXPORT Q_DECL_IMPORT +#endif diff --git a/fixedsizematrix.h b/fixedsizematrix.h new file mode 100644 index 0000000..1353f87 --- /dev/null +++ b/fixedsizematrix.h @@ -0,0 +1,198 @@ +#pragma once + +#include +#include +#include + +template +class FixedSizeMatrix +{ +public: + explicit FixedSizeMatrix(); + FixedSizeMatrix(const std::array, height>& data); + FixedSizeMatrix(const FixedSizeMatrix &other); + + auto& operator=(const FixedSizeMatrix& other); + auto& operator=(const std::array, height>& data); + + [[nodiscard]] auto operator==(const FixedSizeMatrix& other) const; + [[nodiscard]] auto operator!=(const FixedSizeMatrix& other) const; + + [[nodiscard]] auto& operator()(unsigned int x, unsigned int y); + [[nodiscard]] const auto& operator()(unsigned int x, unsigned int y) const; + + void rotateLeft(); + void rotateRight(); + void rotate180(); + void mirrorHorizontally(); + void mirrorVertically(); + + [[nodiscard]] auto asRotatedLeft() const; + [[nodiscard]] auto asRotatedRight() const; + [[nodiscard]] auto asRotated180() const; + [[nodiscard]] auto asMirroredHorizontally() const; + [[nodiscard]] auto asMirroredVertically() const; + +private: + std::array, height> m_data; +}; + +template +FixedSizeMatrix::FixedSizeMatrix() {} + +template +FixedSizeMatrix::FixedSizeMatrix(const std::array, height>& data) : m_data(data) {} + +template +FixedSizeMatrix::FixedSizeMatrix(const FixedSizeMatrix& other) : m_data(other.m_data) {} + +template +auto& FixedSizeMatrix::operator=(const FixedSizeMatrix& other) +{ + if(&other != this) + m_data = other.m_data; + return *this; +} + +template +auto& FixedSizeMatrix::operator=(const std::array, height>& data) +{ + m_data = data; + return *this; +} + +template +auto FixedSizeMatrix::operator==(const FixedSizeMatrix& other) const +{ + return m_data == other.m_data; +} + +template +auto FixedSizeMatrix::operator!=(const FixedSizeMatrix& other) const +{ + return m_data != other.m_data; +} + +template +auto& FixedSizeMatrix::operator()(unsigned int x, unsigned int y) +{ + assert(x < width); + assert(y < height); + return m_data[y][x]; +} + +template +const auto& FixedSizeMatrix::operator()(unsigned int x, unsigned int y) const +{ + assert(x < width); + assert(y < height); + return m_data[y][x]; +} + +template +void FixedSizeMatrix::rotateLeft() +{ + static_assert(width == height, "Rotating in place only works when width matches height"); + + //TODO +} + +template +void FixedSizeMatrix::rotateRight() +{ + static_assert(width == height, "Rotating in place only works when width matches height"); + + //TODO +} + +template +void FixedSizeMatrix::rotate180() +{ + //for(unsigned int y = 0; y < height / 2; y++) + // for(unsigned int x = 0; x < width; x++) + // std::swap(m_data[height-y-1][width-x-1], m_data[y][x]); + + mirrorHorizontally(); + mirrorVertically(); +} + +template +void FixedSizeMatrix::mirrorHorizontally() +{ + //for(unsigned int y = 0; y < height / 2; y++) + // for(unsigned int x = 0; x < width; x++) + // std::swap(m_data[height-y-1][x], m_data[y][x]); + + std::reverse(std::begin(m_data), std::end(m_data)); +} + +template +void FixedSizeMatrix::mirrorVertically() +{ + //for(unsigned int y = 0; y < height; y++) + // for(unsigned int x = 0; x < width / 2; x++) + // std::swap(m_data[y][width-x-1], m_data[y][x]); + + for(unsigned int y = 0; y < height; y++) + std::reverse(std::begin(m_data[y]), std::end(m_data[y])); +} + +template +auto FixedSizeMatrix::asRotatedLeft() const +{ + std::array, width> data; + + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + data[width-x-1][y] = m_data[y][x]; + + return FixedSizeMatrix(data); +} + +template +auto FixedSizeMatrix::asRotatedRight() const +{ + std::array, width> data; + + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + data[x][height-y-1] = m_data[y][x]; + + return FixedSizeMatrix(data); +} + +template +auto FixedSizeMatrix::asRotated180() const +{ + std::array, height> data; + + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + data[height-y-1][width-x-1] = m_data[y][x]; + + return FixedSizeMatrix(data); +} + +template +auto FixedSizeMatrix::asMirroredHorizontally() const +{ + std::array, height> data; + + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + data[height-y-1][x] = m_data[y][x]; + + return FixedSizeMatrix(data); +} + +template +auto FixedSizeMatrix::asMirroredVertically() const +{ + std::array, height> data; + + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + data[y][width-x-1] = m_data[y][x]; + + return FixedSizeMatrix(data); +}