Created
February 25, 2017 01:52
-
-
Save thai-ng/767a99f9fccb3d3dfca7819041a19a0d to your computer and use it in GitHub Desktop.
Compile time matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <array> | |
template <int width, int height, typename T> | |
class Matrix | |
{ | |
using row_t = std::array<T, width>; | |
using col_t = std::array<T, height>; | |
public: | |
template <typename... T> | |
Matrix(T... values) | |
{ | |
_data = {values...}; | |
} | |
template <std::size_t i> | |
auto getRow() | |
{ | |
return make_array<width>(std::next(_data.begin(), width*i)); | |
} | |
template <std::size_t x, std::size_t y> | |
auto getElement() | |
{ | |
return _data[x*width + y]; | |
} | |
private: | |
std::array<T, width*height> _data; | |
template <typename Iterator> | |
using ValueType = typename std::iterator_traits<Iterator>::value_type; | |
template <std::size_t... I, typename RandomAccessIterator, typename Array = std::array<ValueType<RandomAccessIterator>, sizeof...(I)>> | |
Array make_array(RandomAccessIterator first, std::index_sequence<I...>) | |
{ | |
return Array{ { first[I]... } }; | |
} | |
template <std::size_t N, typename RandomAccessIterator> | |
std::array<ValueType<RandomAccessIterator>, N> make_array(RandomAccessIterator first) | |
{ | |
return make_array(first, std::make_index_sequence<N>()); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment