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

alles neu

parent 21e906d2
...@@ -13,4 +13,5 @@ add_executable(matrix ...@@ -13,4 +13,5 @@ add_executable(matrix
generate.cpp generate.cpp
Matrix.cpp Matrix.cpp
includes.hpp includes.hpp
notmatrix.cpp
Matrix.hpp) Matrix.hpp)
\ No newline at end of file
/** /** @file matrix.cpp
* @file Matrix.cpp * @brief Enthält alle Funktionen welche im matrix.hpp beschrieben sind.
* @date 2020-05-14 * */
* @version 1.0
*/
#include "Matrix.hpp" #include "Matrix.hpp"
#include "notmatrix.hpp"
std::mutex m; std::mutex m;
bool existsm(string filename){ void Matrix::print(std::ostream& a_r0s){
ifstream f(filename); for(int i = 0; i < this->dimy; i++){
return f.good(); a_r0s << "<p>";
} for(int j = 0; j < this->dimx; j++){
a_r0s << (*this)(i,j) << " ";
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) << " ";
} }
cout << "</p>"; a_r0s << "</p>";
} }
} }
int Matrix::generateMatrix(int dimension){ void Matrix::fillRND() const{
dim = dimension; std::mt19937 engine(std::random_device{}());
delete [] data; std::uniform_int_distribution<uint64_t> distribution;
data = new int[dim * dim]; for(int i = 0; i < datasize; i++){
srand(time(NULL)); data[i] = distribution(engine);
for (int i = 0; i < dim * dim; i++)
{
data[i] = rand() % 2;
} }
}
m.lock(); int Matrix::generateID() {
std::lock_guard<mutex> lock(m);
json jconfig; json jconfig;
string configloc = "config.json"; string configloc = "config.json";
...@@ -61,7 +43,7 @@ int Matrix::generateMatrix(int dimension){ ...@@ -61,7 +43,7 @@ int Matrix::generateMatrix(int dimension){
ifstream in1(configloc); ifstream in1(configloc);
in1 >> jconfig; in1 >> jconfig;
this->ID = jconfig["counter"]; ID = jconfig["counter"];
int temp_ID = jconfig["counter"]; int temp_ID = jconfig["counter"];
temp_ID++; temp_ID++;
jconfig["counter"] = temp_ID; jconfig["counter"] = temp_ID;
...@@ -69,53 +51,139 @@ int Matrix::generateMatrix(int dimension){ ...@@ -69,53 +51,139 @@ int Matrix::generateMatrix(int dimension){
writecounter.open(configloc); writecounter.open(configloc);
writecounter << jconfig << endl; writecounter << jconfig << endl;
writecounter.close(); writecounter.close();
return ID;
}
string folderpath = "Json/"; int Matrix::saveMatrix(){
ID = generateID();
folderpath += to_string(this->ID) + ".json"; string folderpath = "Json/";
json j; 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; ofstream o;
o.open(folderpath); 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(); o.close();
cout << "<p>" << "File wurde mit ID: " << this->ID << " in Ordner " << folderpath << " erstellt!" << "</p>"; cout << "<p>" << "File wurde mit ID: " << ID << " in Ordner " << folderpath << " erstellt!" << "</p>";
m.unlock(); return ID;
return this->ID;
} }
Matrix::Matrix(int dim) { Matrix::Matrix(int x, int y) {
int dim2 = 2; dimx = x;
while (dim2 < dim) dimy = y;
dim2 *= 2; int carryover = 0;
this->dim = dim2; 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++) in.seekg(-50, std::ifstream::end);
this->data[i] = 0; 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 Matrix::operator-(const Matrix& b){
Matrix c(b.dim); Matrix c(b.dimx, b.dimy);
for (int i = 0; i < b.dim; i++) for (int i = 0; i < this->dimy; i++)
for (int j = 0; j < b.dim; j++) for (int j = 0; j < this->dimx; j++)
c(i,j) = ((*this)(i,j) + b(i,j)) % 2; 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; return c;
} }
Matrix Matrix::operator-(const Matrix& b) const{ void Matrix::set(std::size_t y, std::size_t x){
Matrix c(b.dim); std::size_t matrixPos = dimx*y + x;
for (int i = 0; i < this->dim; i++) const std::size_t arrayPos = matrixPos / (sizeof(uint64_t)*8);
for (int j = 0; j < this->dim; j++) const std::size_t bitPos = matrixPos % (sizeof(uint64_t)*8);
c(i,j) = ((*this)(i,j) - b(i,j)) % 2; 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
* @file Matrix.hpp * @brief Enthält die Klasse Matrix und alle ihre Funktionen
* @date 2020-05-14 *
* @version 1.0 * Es kann eine Matrix mit einer quadratischen Dimension erstellt werden.
*/ *
* @author Mario Bajer
* @version 2.1
* @date 28.05.2020
* */
#ifndef MATRIX_HPP #ifndef MATRIX_HPP
#define MATRIX_HPP #define MATRIX_HPP
#include "includes.hpp" #include "includes.hpp"
#include <bitset>
using namespace std; using namespace std;
using namespace cgicc; using namespace cgicc;
using json = nlohmann::json; using json = nlohmann::json;
/** /** @class Matrix
* \brief This class contains the information and methods for a Matrix used in this project * @brief Klasse Matrix
* * */
* \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{ class Matrix{
public: 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{}; /** @brief Setzt ein Bit an stelle (y,x) auf 1
int dim; * */
int *data; void set(std::size_t y, std::size_t x);
int generateMatrix(int dimension);
void print();
inline int& operator()(unsigned row, unsigned col) const { /** @brief Setzt ein Bit an stelle (y,x) auf 0
return data[dim*row + col]; * */
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 { /** @brief Addition zweier Matrizen durch Overload des + Operators
// return data[dim*row + col]; *
// } * @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: private:
int ID;
int dimx;
int dimy;
int datasize;
uint64_t *data;
}; };
void htmlHeader(string title);
bool existsm(string filename);
#endif #endif
\ No newline at end of file
/** /** @file delete.cpp
* @file delete.cpp * @brief Löscht Matrizen aus dem Filesystem
* @date 2020-05-14 * */
* @version 1.0
*/
#include "Matrix.hpp" #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(){ int main(){
htmlHeader("delete.cgi"); htmlHeader("delete.cgi");
Cgicc formData; Cgicc formData;
......
/** /** @file generate.cpp
* @file generate.cpp * @brief Generiert eine Matrix nach Eingabe der Dimension und legt diese im Filesystem ab.
* @date 2020-05-14 * */
* @version 1.0
*/
#include "Matrix.hpp" #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(){ int main(){
Matrix matrix(2);
Cgicc formData; Cgicc formData;
htmlHeader("generate.cgi"); htmlHeader("generate.cgi");
cout << "<body>\n"; cout << "<body>\n";
cout << "<div class=\"container\">" << endl; cout << "<div class=\"container\">" << endl;
form_iterator fi = formData.getElement("dimension"); int dimx, dimy = 0;
if( !fi->isEmpty() && fi != (*formData).end()) { auto fi1 = formData.getElement("dimensionx");
//nur vielfache von 2 erlaubt if( !fi1->isEmpty() && fi1 != (*formData).end()) {
int input = stoi(**fi); dimx = stoi(**fi1);
int dim2 = 2; cout << "<p>" << dimx << "</p>" << endl;
while (dim2 < input)
dim2 *= 2;
matrix.generateMatrix(dim2);
} }
else 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 << "</div>";
cout << "<br/>\n"; cout << "<br/>\n";
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>"; cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
......
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <mutex> #include <mutex>
#include <random>
#include <nlohmann/json.hpp> #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
* @file list.cpp * @brief Zeigt alle im Filesystem gespeicherten Matrizen an
* @date 2020-05-14 *
* @version 1.0 * */
*/