Skip to content

Commit

Permalink
optimize sizeof dims_t and raw_matrix types
Browse files Browse the repository at this point in the history
  • Loading branch information
bebuch committed Sep 2, 2016
1 parent f480614 commit 041623a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
5 changes: 5 additions & 0 deletions examples/Jamroot.jam
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ exe rgb2grayscale
rgb2grayscale.cpp
libpng12
;

exe sizeof
:
sizeof.cpp
;
32 changes: 32 additions & 0 deletions examples/sizeof.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <mitrax/matrix.hpp>

#include <iostream>


int main(){
using namespace mitrax;
using namespace mitrax::literals;

auto dcc = dims(2_C, 4_R);
auto drc = dims(2_C_rt, 4_R);
auto dcr = dims(2_C, 4_R_rt);
auto drr = dims(2_C_rt, 4_R_rt);

auto mcc = make_matrix_v< std::uint8_t >(2_C, 4_R);
auto mrc = make_matrix_v< std::uint8_t >(2_C_rt, 4_R);
auto mcr = make_matrix_v< std::uint8_t >(2_C, 4_R_rt);
auto mrr = make_matrix_v< std::uint8_t >(2_C_rt, 4_R_rt);

auto hcc = make_matrix_v(2_C, 4_R, std::uint8_t(), memory_heap);

std::cout << "sizeof(dcc) = " << sizeof(dcc) << std::endl;
std::cout << "sizeof(drc) = " << sizeof(drc) << std::endl;
std::cout << "sizeof(dcr) = " << sizeof(dcr) << std::endl;
std::cout << "sizeof(drr) = " << sizeof(drr) << std::endl;

std::cout << "sizeof(mcc) = " << sizeof(mcc) << std::endl;
std::cout << "sizeof(hcc) = " << sizeof(hcc) << std::endl;
std::cout << "sizeof(mrc) = " << sizeof(mrc) << std::endl;
std::cout << "sizeof(mcr) = " << sizeof(mcr) << std::endl;
std::cout << "sizeof(mrr) = " << sizeof(mrr) << std::endl;
}
22 changes: 12 additions & 10 deletions include/mitrax/dim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ namespace mitrax{
}


template < size_t V >
// the second parameter is a hack to reduce the size of
// dims_t< C, R > with C == R
template < size_t V, typename = void >
struct size_ct{
static constexpr size_t value = V;

Expand Down Expand Up @@ -147,13 +149,13 @@ namespace mitrax{


template < size_t C >
struct col_t< true, C >: size_ct< C >{
struct col_t< true, C >: size_ct< C, col_t< true, C > >{
constexpr auto as_row()const noexcept{ return row_t< true, C >(); }
constexpr auto as_dim()const noexcept{ return dim_t< true, C >(); }
};

template < size_t C >
struct col_t< false, C >: size_ct< C >{
struct col_t< false, C >: size_ct< C, col_t< false, C > >{
constexpr col_t()noexcept = default;

constexpr col_t(col_t< true, C >&&)noexcept{}
Expand Down Expand Up @@ -182,13 +184,13 @@ namespace mitrax{


template < size_t R >
struct row_t< true, R >: size_ct< R >{
struct row_t< true, R >: size_ct< R, row_t< true, R > >{
constexpr auto as_col()const noexcept{ return col_t< true, R >(); }
constexpr auto as_dim()const noexcept{ return dim_t< true, R >(); }
};

template < size_t R >
struct row_t< false, R >: size_ct< R >{
struct row_t< false, R >: size_ct< R, row_t< false, R > >{
constexpr row_t()noexcept = default;

constexpr row_t(row_t< true, R >&&)noexcept{}
Expand Down Expand Up @@ -218,7 +220,7 @@ namespace mitrax{


template < size_t N >
struct dim_t< true, N >: size_ct< N >{
struct dim_t< true, N >: size_ct< N, dim_t< true, N > >{
constexpr auto as_col()const noexcept{
return col_t< true, N >();
}
Expand All @@ -229,7 +231,7 @@ namespace mitrax{
};

template < size_t N >
struct dim_t< false, N >: size_ct< N >{
struct dim_t< false, N >: size_ct< N, dim_t< false, N > >{
constexpr dim_t()noexcept = default;

constexpr dim_t(dim_t< true, N >&&)noexcept{}
Expand Down Expand Up @@ -285,7 +287,7 @@ namespace mitrax{


template < size_t C, size_t R >
class dims_t final: private col_t< C != 0, C >, private row_t< R != 0, R >{
class dims_t: private col_t< C != 0, C >, private row_t< R != 0, R >{
public:
static constexpr size_t ct_cols = C;
static constexpr size_t ct_rows = R;
Expand All @@ -299,11 +301,11 @@ namespace mitrax{
)noexcept: col_t< C != 0, C >(cols), row_t< R != 0, R >(rows) {}


constexpr col_t< C != 0, C > const cols()const noexcept{
constexpr col_t< C != 0, C > cols()const noexcept{
return *this;
}

constexpr row_t< R != 0, R > const rows()const noexcept{
constexpr row_t< R != 0, R > rows()const noexcept{
return *this;
}

Expand Down
37 changes: 8 additions & 29 deletions include/mitrax/raw_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace mitrax{ namespace detail{


template < typename T, size_t C, size_t R >
class raw_matrix_impl{
class raw_matrix_impl: public dims_t< C, R >{
public:
/// \brief Type of the data that administrates the matrix
using value_type = T;
Expand Down Expand Up @@ -59,20 +59,11 @@ namespace mitrax{ namespace detail{


constexpr value_type& operator()(size_t x, size_t y){
return values_[y * cols() + x];
return values_[y * this->cols() + x];
}

constexpr value_type const& operator()(size_t x, size_t y)const{
return values_[y * cols() + x];
}


constexpr col_t< C != 0, C > cols()const noexcept{
return col_t< C != 0, C >();
}

constexpr row_t< R != 0, R > rows()const noexcept{
return row_t< R != 0, R >();
return values_[y * this->cols() + x];
}


Expand Down Expand Up @@ -124,7 +115,7 @@ namespace mitrax{ namespace detail{


template < typename T, size_t C, size_t R >
class raw_matrix_impl_base{
class raw_matrix_impl_base: public dims_t< C, R >{
public:
/// \brief Type of the data that administrates the matrix
using value_type = T;
Expand Down Expand Up @@ -152,9 +143,8 @@ namespace mitrax{ namespace detail{
col_t< C != 0, C > c, row_t< R != 0, R > r,
detail::array_d< value_type >&& values
):
values_(std::move(values)),
cols_(c),
rows_(r)
dims_t< C, R >(c, r),
values_(std::move(values))
{}


Expand All @@ -164,20 +154,11 @@ namespace mitrax{ namespace detail{


value_type& operator()(size_t x, size_t y){
return values_[y * cols() + x];
return values_[y * this->cols() + x];
}

value_type const& operator()(size_t x, size_t y)const{
return values_[y * cols() + x];
}


constexpr col_t< C != 0, C > cols()const noexcept{
return cols_;
}

constexpr row_t< R != 0, R > rows()const noexcept{
return rows_;
return values_[y * this->cols() + x];
}


Expand Down Expand Up @@ -230,8 +211,6 @@ namespace mitrax{ namespace detail{

protected:
detail::array_d< value_type > values_;
col_t< C != 0, C > cols_;
row_t< R != 0, R > rows_;
};


Expand Down

0 comments on commit 041623a

Please sign in to comment.