Commit 53c5a6d6 authored by Mario Bajer's avatar Mario Bajer
Browse files

alles neu

parent 21e906d2
......@@ -13,4 +13,5 @@ add_executable(matrix
generate.cpp
Matrix.cpp
includes.hpp
notmatrix.cpp
Matrix.hpp)
\ No newline at end of file
/**
* @file Matrix.cpp
* @date 2020-05-14
* @version 1.0
*/
/** @file matrix.cpp
* @brief Enthält alle Funktionen welche im matrix.hpp beschrieben sind.
* */
#include "Matrix.hpp"
#include "notmatrix.hpp"
std::mutex m;
bool existsm(string filename){
ifstream f(filename);
return f.good();
}
void htmlHeader(string title){
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>" << endl;
cout << "<head>" << endl;
cout << "<meta charset=\"utf-8\">" << endl;
cout << "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">" << endl;
cout << "<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css\" integrity=\"sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh\" crossorigin=\"anonymous\">";
cout << "<title>" << title << "</title>" << endl;
cout << "</head>" << endl;
}
void Matrix::print(){
for(int i = 0; i < this->dim; i++){
cout << "<p>";
for(int j = 0; j < this->dim; j++){
cout << (*this)(i,j) << " ";
void Matrix::print(std::ostream& a_r0s){
for(int i = 0; i < this->dimy; i++){
a_r0s << "<p>";
for(int j = 0; j < this->dimx; j++){
a_r0s << (*this)(i,j) << " ";
}
cout << "</p>";
a_r0s << "</p>";
}
}
int Matrix::generateMatrix(int dimension){
dim = dimension;
delete [] data;
data = new int[dim * dim];
srand(time(NULL));
for (int i = 0; i < dim * dim; i++)
{
data[i] = rand() % 2;
void Matrix::fillRND() const{
std::mt19937 engine(std::random_device{}());
std::uniform_int_distribution<uint64_t> distribution;
for(int i = 0; i < datasize; i++){
data[i] = distribution(engine);
}
}
m.lock();
int Matrix::generateID() {
std::lock_guard<mutex> lock(m);
json jconfig;
string configloc = "config.json";
......@@ -61,7 +43,7 @@ int Matrix::generateMatrix(int dimension){
ifstream in1(configloc);
in1 >> jconfig;
this->ID = jconfig["counter"];
ID = jconfig["counter"];
int temp_ID = jconfig["counter"];
temp_ID++;
jconfig["counter"] = temp_ID;
......@@ -69,53 +51,139 @@ int Matrix::generateMatrix(int dimension){
writecounter.open(configloc);
writecounter << jconfig << endl;
writecounter.close();
return ID;
}
string folderpath = "Json/";
int Matrix::saveMatrix(){
ID = generateID();
folderpath += to_string(this->ID) + ".json";
json j;
string folderpath = "Json/";
folderpath += to_string(this->ID);
j["id"] = this->ID;
j["dimension"] = this->dim;
int size = this->dim * this->dim;
vector<int> v(this->data, this->data + size);
j["matrix"] = v;
ofstream o;
o.open(folderpath);
o << j << endl;
o << ID << "," << dimx << "," << dimy << "," << endl;
o.seekp(50, std::ofstream::beg);
for(int i = 0; i < datasize; i++){
unsigned char val[sizeof(uint64_t)];
memcpy(val, &data[i], sizeof(uint64_t));
for (unsigned char j : val){
o << j;
}
}
o.close();
cout << "<p>" << "File wurde mit ID: " << this->ID << " in Ordner " << folderpath << " erstellt!" << "</p>";
m.unlock();
return this->ID;
cout << "<p>" << "File wurde mit ID: " << ID << " in Ordner " << folderpath << " erstellt!" << "</p>";
return ID;
}
Matrix::Matrix(int dim) {
int dim2 = 2;
while (dim2 < dim)
dim2 *= 2;
this->dim = dim2;
Matrix::Matrix(int x, int y) {
dimx = x;
dimy = y;
int carryover = 0;
if ((64 - ((dimx * dimy) % 64)) != 0 && (64 - ((dimx * dimy) % 64)) != 64){
carryover = 64 - ((dimx * dimy) % 64);
}
datasize = ((dimx * dimy) + carryover) / 64;
const int fillsize = datasize;
data = new uint64_t[fillsize];
}
this->data = new int[dim * dim];
void Matrix::getmatrixfromfile( const string& path) {
ifstream in(path);
int i = 0;
string param;
vector<string> input;
while(getline(in, param, ',')){
input.push_back(param);
i++;
if (i == 3){
break;
}
}
ID = stoi (input[0]);
dimx = stoi (input[1]);
dimy = stoi (input[2]);
for (int i = 0; i < dim * dim; i++)
this->data[i] = 0;
in.seekg(-50, std::ifstream::end);
size_t length = in.tellg();
in.seekg(50, std::ifstream::beg);
delete [] data;
this->data = new uint64_t[length];
in.read((char*)data, length);
}
Matrix Matrix::operator+(const Matrix& b){
Matrix c(b.dimx, b.dimy);
for (int i = 0; i < b.dimy; i++)
for (int j = 0; j < b.dimx; j++)
if ((*this)(i,j) != b(i,j)){
c.set(i,j);
}else{
c.reset(i,j);
}
return c;
}
Matrix Matrix::operator+(const Matrix& b) const{
Matrix c(b.dim);
for (int i = 0; i < b.dim; i++)
for (int j = 0; j < b.dim; j++)
c(i,j) = ((*this)(i,j) + b(i,j)) % 2;
Matrix Matrix::operator-(const Matrix& b){
Matrix c(b.dimx, b.dimy);
for (int i = 0; i < this->dimy; i++)
for (int j = 0; j < this->dimx; j++)
if ((*this)(i,j) != b(i,j)){
c.set(i,j);
}else{
c.reset(i,j);
}
return c;
}
Matrix Matrix::operator*(const Matrix& b) {
Matrix c(b.dimx, this->dimy);
for (int i = 0; i < this->dimx; i++)
for (int j = 0; j < b.dimx; j++)
for (int k = 0; k < this->dimy; k++){
if (c(i,j) != ((*this)(i,k) & b(k,j))) {
c.set(i, j);
} else {
c.reset(i, j);
}
}
return c;
}
Matrix Matrix::operator-(const Matrix& b) const{
Matrix c(b.dim);
for (int i = 0; i < this->dim; i++)
for (int j = 0; j < this->dim; j++)
c(i,j) = ((*this)(i,j) - b(i,j)) % 2;
void Matrix::set(std::size_t y, std::size_t x){
std::size_t matrixPos = dimx*y + x;
const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8);
const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8);
const uint64_t pos = 1;
data[arrayPos] |= (pos << bitPos);
}
void Matrix::reset(std::size_t y, std::size_t x){
std::size_t matrixPos = dimx*y + x;
const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8);
const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8);
const uint64_t pos = 1;
data[arrayPos] &= ~(pos << bitPos);
}
return c;
bool Matrix::checkAddSub(const Matrix& b) const{
if(this->dimx == b.dimx && this->dimy == b.dimy)
return true;
else{
cout << "<p>Matrix A: DimX: " << this->dimx << " DimY: " << this->dimy << "</p>" << endl;
cout << "<p>Matrix B: DimX: " << b.dimx << " DimY: " << b.dimy << "</p>" << endl;
cout << "<P>Matrix A & B müssen gleiche DimX und DimY besitzen!</p>" << endl;
return false;
}
}
bool Matrix::checkMult(const Matrix& b){
if(this->dimx == b.dimy)
return true;
else{
cout << "<p>Matrix A: DimX: " << this->dimx << " DimY: " << this->dimy << "</p>" << endl;
cout << "<p>Matrix B: DimX: " << b.dimx << " DimY: " << b.dimy << "</p>" << endl;
cout << "<P>DimX von Matrix A muss gleich DimY von Matrix B sein!</p>" << endl;
return false;
}
}
\ No newline at end of file
/**
* @file Matrix.hpp
* @date 2020-05-14
* @version 1.0
*/
/** @file matrix.hpp
* @brief Enthält die Klasse Matrix und alle ihre Funktionen
*
* Es kann eine Matrix mit einer quadratischen Dimension erstellt werden.
*
* @author Mario Bajer
* @version 2.1
* @date 28.05.2020
* */
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include "includes.hpp"
#include <bitset>
using namespace std;
using namespace cgicc;
using json = nlohmann::json;
/**
* \brief This class contains the information and methods for a Matrix used in this project
*
* \data ID = ID of the Matrix
* \data dim = Dimension of the Matrix
* \data *data = Pointer to the Data of the Matrix
* \methods generateMatrix(int dimension) = Method to generate the Matrix with the dimension used as parameter
* \methods print() = Method to output the Matrix
*/
/** @class Matrix
* @brief Klasse Matrix
* */
class Matrix{
public:
explicit Matrix(int dim);
/** @brief Konstruktor für die Matrix Klasse
*
* Initialisiert eine Matrix und speichert diese im Filesystem.
* @param x - Spaltenanzahl
* @param y - Zeilenanzahl
* @return liefert einen über x und y definierte Matrix zurück.
* */
Matrix(int x, int y);
/** @brief Printen der Matrix
*
* @param std::ostream& - Ausgabestream für die Matrix
* */
void print(std::ostream& a_r0s);
/** @brief Füllt eine Matrix mit gleichverteilten Zufallswerten
* */
void fillRND() const;
/** @brief Generiert für eine Matrix eine ID auf Basis eines Files welches einen Counter speichert
* */
int generateID();
/** @brief Speichert die Matrix im lokalen Filesystem
* */
int saveMatrix();
/** @brief Liest Daten und Metadaten einer Matrix aus einem File und füllt das Matrix-Objekt
* */
void getmatrixfromfile( const string& path);
int ID{};
int dim;
int *data;
int generateMatrix(int dimension);
void print();
/** @brief Setzt ein Bit an stelle (y,x) auf 1
* */
void set(std::size_t y, std::size_t x);
inline int& operator()(unsigned row, unsigned col) const {
return data[dim*row + col];
/** @brief Setzt ein Bit an stelle (y,x) auf 0
* */
void reset(std::size_t y, std::size_t x);
/** @brief Prüft ob eine Addition möglich ist.
*
* */
bool checkAddSub(const Matrix& b) const;
/** @brief Prüft ob eine Multiplikation möglich ist.
*
* @return bool checkMult - true wenn erlaubt, false falls nicht.
* */
bool checkMult(const Matrix& b);
/** @brief Overload des * Operators
*
* @param unsigned y - Zeile der Matrix
* @param unsigned x - Spalte der Matrix
* @return gibt den Wert der Matrix an der Stelle zurück
* */
inline bool operator()(unsigned y, unsigned x) const {
std::size_t matrixPos = dimx*y + x;
const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8);
const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8);
const uint64_t pos = 1;
return data[arrayPos] & (pos << bitPos);
}
// inline int operator()(unsigned row, unsigned col) const {
// return data[dim*row + col];
// }
/** @brief Addition zweier Matrizen durch Overload des + Operators
*
* @param const Matrix& b const
* @return Gibt eine Matrix c, das Ergebnis der Addition zweier Matrizen zurück.
* */
Matrix operator+(const Matrix& b);
Matrix operator+(const Matrix& b) const ;
/** @brief Subtraktion zweier Matrizen durch Overload des - Operators
*
* @param const Matrix& b
* @return Gibt eine Matrix c, das Ergebnis der Subtraktion zweier Matrizen zurück.
* */
Matrix operator-(const Matrix& b);
Matrix operator-(const Matrix& b) const ;
/** @brief Multiplikation zweier Matrizen durch Overload des * Operators
*
* @param const Matrix& b
* @return Gibt eine Matrix c, das Ergebnis der Multiplikation zweier Matrizen zurück.
* */
Matrix operator*(const Matrix& b);
private:
int ID;
int dimx;
int dimy;
int datasize;
uint64_t *data;
};
void htmlHeader(string title);
bool existsm(string filename);
#endif
\ No newline at end of file
/**
* @file delete.cpp
* @date 2020-05-14
* @version 1.0
*/
/** @file delete.cpp
* @brief Löscht Matrizen aus dem Filesystem
* */
#include "Matrix.hpp"
#include "notmatrix.hpp"
/**
* \brief This method is used to delete a matrix
* \param no params
* \return null or error
*
* -- Detailed description --
*
*/
int main(){
htmlHeader("delete.cgi");
Cgicc formData;
......
/**
* @file generate.cpp
* @date 2020-05-14
* @version 1.0
*/
/** @file generate.cpp
* @brief Generiert eine Matrix nach Eingabe der Dimension und legt diese im Filesystem ab.
* */
#include "Matrix.hpp"
#include "notmatrix.hpp"
/**
* \brief This method is used to generate a matrix
* \param no params
* \return null or error
*
* -- Detailed description --
*
*/
int main(){
Matrix matrix(2);
Cgicc formData;
htmlHeader("generate.cgi");
cout << "<body>\n";
cout << "<div class=\"container\">" << endl;
form_iterator fi = formData.getElement("dimension");
if( !fi->isEmpty() && fi != (*formData).end()) {
//nur vielfache von 2 erlaubt
int input = stoi(**fi);
int dim2 = 2;
while (dim2 < input)
dim2 *= 2;
matrix.generateMatrix(dim2);
int dimx, dimy = 0;
auto fi1 = formData.getElement("dimensionx");
if( !fi1->isEmpty() && fi1 != (*formData).end()) {
dimx = stoi(**fi1);
cout << "<p>" << dimx << "</p>" << endl;
}
else
{
cout << "No Dimension entered" << endl;
cout << "No valid dimension for X entered!" << endl;
}
auto fi2 = formData.getElement("dimensiony");
if( !fi2->isEmpty() && fi2 != (*formData).end()) {
dimy = stoi(**fi2);
cout << "<p>" << dimy << "</p>" << endl;
}
else
{
cout << "No valid dimension for X entered!" << endl;
}
if((dimx != 0) && (dimy != 0)){
Matrix matrix(dimx, dimy);
matrix.fillRND();
matrix.saveMatrix();
}
cout << "</div>";
cout << "<br/>\n";
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
......
......@@ -3,9 +3,11 @@
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <random>
#include <nlohmann/json.hpp>
......
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>Matrix Vergleich</title>
</head>
<body>
<div class = "container">
<div class = "row">
<div class = "col-5">
<h3>Generate a Matrix</h3>
<form action="/cgi-bin/generate.cgi" method="post">
<input type="text" class="form-control" name="dimensionx" placeholder="1000 <--- dimx" required>
<br>
<input type="text" class="form-control" name="dimensiony" placeholder="1000 <--- dimy" required>
<br>
<button type="submit" class="btn btn-primary">
generate matrix
</button>
</form>
</div>
<div class = "col-5">
<br>
<h3>List all Matrices</h3>
<form action="/cgi-bin/list.cgi" method="get">
<!-- <input type="text" class="form-control" name="dimension" value="1000" required>-->
<button type="submit" class="btn btn-success">
List
</button>
</form>
</div>
</div>
</div>
<br>
</body>
</body>
</html>
/**
* @file list.cpp
* @date 2020-05-14
* @version 1.0
*/
/** @file list.cpp
* @brief Zeigt alle im Filesystem gespeicherten Matrizen an
*
* */
#include "Matrix.hpp"
#include "notmatrix.hpp"
namespace fs = filesystem;
/**
* \brief This method is used to list all existing matrices
* \param no params
* \return null or error
*
* -- Detailed description --
*
*/
int main(){
htmlHeader("list.cgi");
cout << "<body>"
"<br>"
"<div class=\"container\">"
"<table class=\"table\">"
"<thead class=\"thead-dark\">"
"<tr>"
"<th>ID</th>"
"<th>Dimension</th>"
"<th>File Location</th>"
"<th></th>"
"</tr>"
"</thead>"
"<tbody>" << endl;
cout << "<div class = \"container\">";
cout << R"(<form action="/cgi-bin/multi.cgi" method="POST">)" << endl;
cout << "<div class=\"form-row\">" << endl;
cout << "<div class=\"form-group col-lg-2 mr-2\">" << endl;
cout << "<label>Matrix A</label>" << endl;
cout << R"(<input name="matrixA" class="form-control" type="number"/>)" << endl;
cout << "</div>" << endl;
cout << "<div class=\"form-group col-lg-2 mr-2\">" << endl;
cout << "<label>Matrix B</label>" << endl;
cout << R"(<input name="matrixB" class="form-control" type="number"/>)" << endl;
cout << "</div>" << endl;
cout << "<div class=\"form-group col-lg-2 mr-2\">" << endl;
cout << "<label>Rechenoperation</label>" << endl;
cout << R"(<select class="form-control" name="rechenop">)" << endl;
cout << "<option>+</option>" << endl;
cout << "<option>-</option>" << endl;
cout << "<option>*</option>" << endl;
cout << "</select>" << endl;
cout << "</div>" << endl;
cout << R"(<button type="submit" class="btn btn-primary my-auto">Berechnen</button>)" << endl;
cout << "</div>" << endl;
cout << "</form>" << endl;
cout << "<br>" << endl;
cout << "<table class=\"table\">" << endl;
cout << "<thead class=\"thead-dark\">" << endl;
cout << "<tr>" << endl;
cout << "<th>ID</th>" << endl;
cout << "<th>Dimension X</th>" << endl;