This commit is contained in:
Leon Wilzer 2022-12-17 21:35:56 +01:00
parent a5e86d1335
commit 32b1e69f55
3 changed files with 148 additions and 2 deletions

View File

@ -1,4 +1,5 @@
CC := clang++ -Wall -std=c++17 -Wall -Wextra -g -fsanitize=address,undefined -fno-omit-frame-pointer
#CC := clang++ -Wall -std=c++17 -Wall -Wextra
setup:
mkdir build
@ -39,6 +40,11 @@ sheet06:
chmod +x build/sheet06
build/sheet06
sheet07:
$(CC) src/sheet07.cpp -o build/sheet07
chmod +x build/sheet07
build/sheet07
pizza:
$(CC) src/pizza.cpp -o build/pizza
chmod +x build/pizza

View File

@ -55,9 +55,10 @@ void bubble_sort(std::vector<T> &v, std::function<bool(T,T)> predicate)
}while(has_swapped);
}
char add()
template<typename A>
A add(A a)
{
return 0;
return a;
}
template<typename A, typename... Args>

139
src/sheet07.cpp Normal file
View File

@ -0,0 +1,139 @@
#include <exception>
#include<iostream>
#include <ostream>
#include <system_error>
#include<vector>
#include<initializer_list>
#include<stdexcept>
template<typename T>
class matrix
{
public:
matrix(size_t rows, size_t columns) : rows(rows), columns(columns)
{
data();
}
matrix(size_t rows, size_t columns, const T &ival) : rows(rows), columns(columns)
{
data = std::vector<T>(rows*columns, ival);
}
matrix(std::initializer_list<std::initializer_list<T>> imat)
{
size_t prev_length;
for(std::initializer_list<T> row : imat)
{
if(prev_length!=row.size())
{
throw std::domain_error("provided initializer list with empty spots.");
}
prev_length = row.size();
for(T t : row)
{
data.push_back(t);
}
}
}
T& operator() (size_t row, size_t column)
{
if(row>=this->num_rows())
{
throw std::length_error("row outside matrix.");
}
if(column>=this->num_columns())
{
throw std::length_error("column outside matrix.");
}
return data.at(row*(this->num_columns())+column);
}
const T& operator() (size_t row, size_t column) const
{
if(row>=this->num_rows())
{
throw std::length_error("row outside matrix.");
}
if(column>=this->num_columns())
{
throw std::length_error("column outside matrix.");
}
return data.at(row*(this->num_columns())+column);
}
size_t num_elements() const noexcept { return columns*rows; }
size_t num_rows() const noexcept { return rows; }
size_t num_columns() const noexcept { return columns; }
friend matrix operator* (const matrix &lhs, const T &scale)
{
matrix out(lhs.num_rows(), lhs.num_columns(), 0);
for(size_t x=0; x<lhs.num_columns(); ++x)
{
for(size_t y=0; y<lhs.num_rows(); ++y)
{
out(y,x) = lhs(y,x)*scale;
}
}
return out;
}
friend matrix operator* (const matrix &lhs, const matrix &rhs)
{
//TODO
throw std::logic_error("unimplemented");
}
friend bool operator== (const matrix &lhs, const matrix &rhs)
{
if(lhs.num_columns() != rhs.num_columns() || lhs.num_rows() != rhs.num_rows())
{
return false;
}
for(size_t x=0; x<lhs.num_columns(); ++x)
{
for(size_t y=0; y<lhs.num_rows(); ++y)
{
if(lhs(y,x)!=rhs(y,x))
{
return false;
}
}
}
return true;
}
friend bool operator!=(const matrix &lhs, const matrix &rhs)
{
return !(lhs==rhs);
}
friend std::ostream& operator<< (std::ostream &os, const matrix &m)
{
for(size_t x=0; x<m.num_columns(); ++x)
{
for(size_t y=0; y<m.num_rows(); ++y)
{
os << m(y,x) << '\t';
}
os << '\n';
}
return os;
}
private:
size_t rows;
size_t columns;
std::vector<T> data;
};
int main()
{
matrix<int> m(5,5,5);
std::cout << m;
std::cout << m*5;
std::cout << m*m;
}