lasd/librerie/exercise5/matrix/vec/matrixvec.cpp

123 lines
3.0 KiB
C++
Raw Permalink Normal View History

namespace lasd {
template <typename Data>
MatrixVec<Data>::MatrixVec(ulong r, ulong c){
rows = r;
columns = c;
size = r*c;
Elements = new Data[size]{};
}
template <typename Data>
MatrixVec<Data>::MatrixVec(const MatrixVec& toCopy){
rows = toCopy.rows;
columns = toCopy.columns;
2021-05-28 12:43:27 +02:00
size = toCopy.size;
Elements = new Data[size]{};
for(ulong i=0 ; i<size ; ++i){
Elements[i] = toCopy.Elements[i];
}
}
template <typename Data>
2021-05-31 11:26:27 +02:00
MatrixVec<Data>::MatrixVec(MatrixVec<Data>&& toMove) noexcept{
std::swap(rows, toMove.rows);
std::swap(columns, toMove.columns);
std::swap(size, toMove.size);
std::swap(Elements, toMove.Elements);
}
template <typename Data>
MatrixVec<Data>::~MatrixVec(){
Clear();
}
template <typename Data>
2021-05-31 11:26:27 +02:00
MatrixVec<Data>& MatrixVec<Data>::operator=(const MatrixVec& toCopy){
Clear();
rows = toCopy.rows;
columns = toCopy.columns;
2021-05-28 12:43:27 +02:00
size = toCopy.size;
Elements = new Data[size]{};
for(ulong i=0 ; i<size ; ++i){
Elements[i] = toCopy.Elements[i];
}
return *this;
}
template <typename Data>
2021-05-31 11:26:27 +02:00
MatrixVec<Data>& MatrixVec<Data>::operator=(MatrixVec&& toMove) noexcept{
std::swap(rows, toMove.rows);
std::swap(columns, toMove.columns);
std::swap(size, toMove.size);
std::swap(Elements, toMove.Elements);
return *this;
}
template <typename Data>
bool MatrixVec<Data>::operator==(const MatrixVec& toCompare) const noexcept{
if(rows == toCompare.rows && columns == toCompare.columns){
return Vector<Data>::operator==(toCompare);
}else{
return false;
}
}
template <typename Data>
bool MatrixVec<Data>::operator!=(const MatrixVec& toCompare) const noexcept{
return !(*this == toCompare);
}
template <typename Data>
2021-06-05 14:25:22 +02:00
void MatrixVec<Data>::RowResize(const ulong& new_row_dim){
if(new_row_dim != rows){
Vector<Data>::Resize(new_row_dim * columns);
rows = new_row_dim;
size = new_row_dim * columns;
2021-05-31 17:46:48 +02:00
}
}
template <typename Data>
void MatrixVec<Data>::ColumnResize(const ulong& new_column_dim){
2021-06-05 14:25:22 +02:00
if(new_column_dim != columns){
2021-05-28 12:43:27 +02:00
ulong limit = (new_column_dim < columns)? new_column_dim : columns;
Data* tmp = new Data[rows * new_column_dim]{};
2021-05-31 11:26:27 +02:00
for(ulong i=0 ; i<rows ; ++i){
2021-05-28 12:43:27 +02:00
for(ulong j=0 ; j<limit ; ++j){
tmp[(i*new_column_dim)+j] = (*this)(i,j);
}
}
size = rows * new_column_dim;
columns = new_column_dim;
std::swap(Elements, tmp);
delete[] tmp;
}
}
template <typename Data>
2021-05-31 11:26:27 +02:00
bool MatrixVec<Data>::ExistsCell(const ulong& r, const ulong& c) const noexcept{
return (r<rows && c<columns);
}
template <typename Data>
2021-06-02 10:02:21 +02:00
const Data& MatrixVec<Data>::operator()(const ulong r, const ulong c) const{
if(ExistsCell(r,c)) return Elements[(r*columns)+c];
2021-05-31 11:26:27 +02:00
else throw std::out_of_range("Tried to access an invalid position!");
}
template <typename Data>
2021-06-02 10:02:21 +02:00
Data& MatrixVec<Data>::operator()(const ulong r, const ulong c){
if(ExistsCell(r,c)) return Elements[(r*columns)+c];
2021-05-31 11:26:27 +02:00
else throw std::out_of_range("Tried to access an invalid position!");
}
template <typename Data>
void MatrixVec<Data>::Clear(){
Vector<Data>::Clear();
rows = 0;
columns = 0;
}
}