our_dick/include/detail/matrix.tpp

148 lines
4.7 KiB
C++

/**
This file is a part of the rexy/r0nk/atlas project
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_DETAIL_MATRIX_TPP
#define REXY_DETAIL_MATRIX_TPP
#include <cstdlib> //size_t
#include <utility> //integer_sequence
namespace math::detail{
template<typename T, size_t R>
constexpr mat_ref_obj<T,R>::mat_ref_obj(T* d, size_type i):
m_data(d+i){}
template<typename T, size_t R>
constexpr T& mat_ref_obj<T,R>::operator[](size_type i){
return m_data[i*R];
}
template<typename T, size_t R>
constexpr const T& mat_ref_obj<T,R>::operator[](size_type i)const{
return m_data[i*R];
}
template<typename T, size_t W, size_t H>
template<typename matrix_base<T,W,H>::size_type... Ss>
constexpr matrix_base<T,W,H>::matrix_base(std::integer_sequence<size_type,Ss...>):
m_data{Ss...}{}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::matrix_base():
matrix_base(typename detail::default_initialization_matrix<Columns,Rows>::tuple{}){}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::matrix_base(detail::zero_initialize_t):
m_data{}{}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::matrix_base(detail::no_initialize_t){}
template<typename T, size_t W, size_t H>
template<typename U>
constexpr matrix_base<T,W,H>::matrix_base(detail::id_initialize_t):
matrix_base()
{
static_assert(Columns == Rows, "Identity initialization only supported on square matrices");
}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::matrix_base(value_type v){
for(size_type i = 0;i < Columns*Rows;++i)
m_data[i] = v;
}
template<typename T, size_t W, size_t H>
template<typename... Args>
constexpr matrix_base<T,W,H>::matrix_base(Args&&... args):
m_data{std::forward<Args>(args)...}{}
template<typename T, size_t W, size_t H>
template<typename U>
constexpr matrix_base<T,W,H>::matrix_base(const matrix_base<U,Columns,Rows>& m){
using mat = decltype(m);
for(typename mat::size_type i = 0;i < mat::Columns*mat::Rows;++i)
m_data[i] = m.get(i);
}
template<typename T, size_t W, size_t H>
template<typename U>
constexpr matrix_base<T,W,H>& matrix_base<T,W,H>::operator=(const matrix_base<U,Columns,Rows>& m){
using mat = decltype(m);
for(typename mat::size_type i = 0;i < mat::Columns*mat::Rows;++i)
m_data[i] = m.get(i);
return *this;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::operator[](size_type x){
return detail::mat_ref_obj<value_type,Rows>{m_data, x};
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::operator[](size_type x)const{
return detail::mat_ref_obj<const value_type,Rows>{m_data, x};
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::get(size_type x, size_type y) -> reference{
return m_data[x+(y*Rows)];
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::get(size_type x, size_type y)const -> const_reference{
return m_data[x+(y*Rows)];
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::get(size_type i) -> reference{
return m_data[i];
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::get(size_type i)const -> const_reference{
return m_data[i];
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::columns()const -> size_type{
return Columns;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::rows()const -> size_type{
return Rows;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::size()const -> size_type{
return Columns*Rows;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::raw() -> pointer{
return m_data;
}
template<typename T, size_t W, size_t H>
constexpr auto matrix_base<T,W,H>::raw()const -> const_pointer{
return m_data;
}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::operator pointer(){
return m_data;
}
template<typename T, size_t W, size_t H>
constexpr matrix_base<T,W,H>::operator const_pointer()const{
return m_data;
}
}
#endif