Skip to content
Snippets Groups Projects

Added flat matrix and vector used for ElementMatrix and ElementVector

Merged Praetorius, Simon requested to merge feature/flat_mat_vec into master
All threads resolved!
4 files
+ 150
39
Compare changes
  • Side-by-side
  • Inline
Files
4
#pragma once
#include <type_traits>
#include <vector>
namespace AMDiS
{
/// Dense matrix with row wise storage in flat data vector
template <class T>
/// Dense matrix with row-wise storage in flat data vector
template <class T, class Allocator = std::allocator<T>>
class FlatMatrix
: public std::vector<T,Allocator>
{
using Super = std::vector<T,Allocator>;
public:
using value_type = T;
using size_type = std::size_t;
using size_type = typename Super::size_type;
using reference = typename Super::reference;
using const_reference = typename Super::const_reference;
private:
template <class Self>
/// Accessor to a row of the matrix.
template <class Vector>
struct AccessProxy
{
template <class S = Self, std::enable_if_t<not std::is_const<S>::value,int> = 0>
value_type& operator[](size_type col)
/// Access the column entry `col` of the row `row_` of this accessor.
/// @{
template <class V = Vector, std::enable_if_t<not std::is_const<V>::value,int> = 0>
reference operator[](size_type col)
{
return self_->data_[row_*self_->cols_ + col];
return (*vector_)[row_*cols_ + col];
}
value_type const& operator[](size_type col) const
const_reference operator[](size_type col) const
{
return self_->data_[row_*self_->cols_ + col];
return (*vector_)[row_*cols_ + col];
}
// @}
Self* self_;
Vector* vector_;
size_type row_;
size_type cols_;
};
public:
/// Default constructor, creates an empty 0x0 matrix.
FlatMatrix() = default;
/// Construct a new matrix with rows `r` and columns `c` and all values
/// initialized by copies of value `v`.
FlatMatrix(size_type r, size_type c, value_type v = {})
: data_(r*c, v)
: Super(r*c, v)
, rows_(r)
, cols_(c)
{}
/// Assign value `s` to all entries of the matrix
FlatMatrix& operator=(value_type s)
{
data_.assign(data_.size(), s);
Super::assign(Super::size(), s);
return *this;
}
/// Resizes the container to contain r x c elements.
/**
* If the current `rows*cols` is greater than `r*c`, the container is reduced
* to its first `r*c` elements. If the current `rows*cols` is less than `r*c`,
* additional copies of value `v` are appended.
**/
void resize(size_type r, size_type c, value_type v = {})
{
data_.resize(r*c, v);
Super::resize(r*c, v);
rows_ = r;
cols_ = c;
}
size_type rows() const
/// Return the number of rows of the matrix
size_type rows() const noexcept
{
return rows_;
}
size_type cols() const
/// Return the number of columns of the matrix
size_type cols() const noexcept
{
return cols_;
}
size_type size() const
{
return data_.size();
}
/// Return the number of entries in the matrix, i.e. `rows*cols`.
using Super::size;
AccessProxy<FlatMatrix> operator[](size_type row)
/// Return accessor to the `row` of the matrix, see \ref AccessProxy
/// @{
AccessProxy<Super> operator[](size_type row)
{
return AccessProxy<FlatMatrix>{this, row};
return AccessProxy<Super>{static_cast<Super*>(this), row, cols_};
}
AccessProxy<FlatMatrix const> operator[](size_type row) const
AccessProxy<Super const> operator[](size_type row) const
{
return AccessProxy<FlatMatrix const>{this, row};
return AccessProxy<Super const>{static_cast<Super const*>(this), row, cols_};
}
T const* data() const noexcept { return data_.data(); }
T* data() noexcept { return data_.data(); }
auto const begin() const { return data_.begin(); }
auto begin() { return data_.begin(); }
auto const end() const { return data_.end(); }
auto end() { return data_.end(); }
auto cbegin() { return data_.cbegin(); }
auto cend() { return data_.cend(); }
/// @}
private:
std::vector<value_type> data_;
size_type rows_ = 0;
size_type cols_ = 0;
};
Loading