Matrix.cpp 6.37 KB
Newer Older
Mario Bajer's avatar
Mario Bajer committed
1
/** @file Matrix.cpp
Mario Bajer's avatar
Mario Bajer committed
2
3
 *  @brief Enthält alle Funktionen welche im matrix.hpp beschrieben sind.
 * */
4

Mario Bajer's avatar
cleanup  
Mario Bajer committed
5
#include "Matrix.hpp"
Mario Bajer's avatar
Mario Bajer committed
6
#include "notmatrix.hpp"
Mario Bajer's avatar
cleanup  
Mario Bajer committed
7

8
9
std::mutex m;

10
void Matrix::print(std::ostream& a_r0s){ // refactor auf z.B: "renderHTML"
Mario Bajer's avatar
Mario Bajer committed
11
    for(int i = 0; i < this->dimx; i++){
Mario Bajer's avatar
Mario Bajer committed
12
        a_r0s << "<p>";
Mario Bajer's avatar
Mario Bajer committed
13
        for(int j = 0; j < this->dimy; j++){
Mario Bajer's avatar
Mario Bajer committed
14
            a_r0s  << (*this)(i,j) << " ";
15
        }
Mario Bajer's avatar
Mario Bajer committed
16
        a_r0s << "</p>";
Mario Bajer's avatar
cleanup  
Mario Bajer committed
17
18
19
    }
}

Mario Bajer's avatar
Mario Bajer committed
20
void Matrix::fillRND() {
21
22
    static std::mt19937 engine(std::random_device{}());         //static damit nicht bei jedem call neues obj erstellt wird
    static std::uniform_int_distribution<uint64_t> distribution;
Mario Bajer's avatar
Mario Bajer committed
23
24
    for(int i = 0; i < datasize; i++){
        data[i] = distribution(engine);
25
    }
Mario Bajer's avatar
Mario Bajer committed
26
}
Mario Bajer's avatar
cleanup  
Mario Bajer committed
27

Mario Bajer's avatar
Mario Bajer committed
28
29
int Matrix::generateID() {
    std::lock_guard<mutex> lock(m);
30
31
32
33
34
35
36
    json jconfig;
    string configloc = "config.json";

    if(!existsm(configloc)) {
        json jcreate;
        jcreate["counter"] = 0;
        ofstream ocreate;
37
        ocreate.open(configloc);        //abfangen obs auch wirklich aufgegangen is!
38
39
40
        ocreate << jcreate << endl;
        ocreate.close();
        cout << "<p> config.json did not exist. Created config.json with counter 0" << endl;
Mario Bajer's avatar
cleanup  
Mario Bajer committed
41
    }
42
43
44
45

    ifstream in1(configloc);
    in1 >> jconfig;

Mario Bajer's avatar
Mario Bajer committed
46
    ID = jconfig["counter"];
47
48
49
50
51
52
53
    int temp_ID = jconfig["counter"];
    temp_ID++;
    jconfig["counter"] = temp_ID;
    ofstream writecounter;
    writecounter.open(configloc);
    writecounter << jconfig << endl;
    writecounter.close();
Mario Bajer's avatar
Mario Bajer committed
54
55
    return ID;
}
56

57
int Matrix::saveMatrix(){                   // sollte mit parameter "folderpath" aufgerufen werden oder geöffnetem stream von außen
Mario Bajer's avatar
Mario Bajer committed
58
    ID = generateID();
59

Mario Bajer's avatar
Mario Bajer committed
60
61
    string folderpath = "Json/";
    folderpath += to_string(this->ID);
62

63
64
    ofstream o;
    o.open(folderpath);
Mario Bajer's avatar
Mario Bajer committed
65
    o << ID << "," << dimx << "," << dimy << "," << endl;
66
    o.seekp(50, std::ofstream::beg);                        // trennung durch newline
Mario Bajer's avatar
Mario Bajer committed
67
68
69
70
71
72
73
    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;
        }
    }
74
    o.close();
Mario Bajer's avatar
Mario Bajer committed
75
76
    cout << "<p>" << "File wurde mit ID: " << ID << " in Ordner " << folderpath << " erstellt!" << "</p>";
    return ID;
Mario Bajer's avatar
cleanup  
Mario Bajer committed
77
}
78

Mario Bajer's avatar
Mario Bajer committed
79
80
81
82
83
84
85
86
87
88
89
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];
}
90

Mario Bajer's avatar
Mario Bajer committed
91
92
93
94
95
void Matrix::getmatrixfromfile( const string& path) {
    ifstream in(path);
    int i = 0;
    string param;
    vector<string> input;
96
    while(getline(in, param, ',')){                 //sollte static funktion sein die genau diese 7 zeilen ausführt
Mario Bajer's avatar
Mario Bajer committed
97
98
99
100
101
102
103
104
105
        input.push_back(param);
        i++;
        if (i == 3){
            break;
        }
    }
    ID = stoi (input[0]);
    dimx = stoi (input[1]);
    dimy = stoi (input[2]);
106

Mario Bajer's avatar
Mario Bajer committed
107
108
109
110
111
112
    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);
113
114
}

Mario Bajer's avatar
Mario Bajer committed
115
116
117
118
119
120
121
122
123
124
125
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;
}
126

Mario Bajer's avatar
Mario Bajer committed
127
128
129
130
131
132
133
134
135
136
137
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;
}
138

Mario Bajer's avatar
Mario Bajer committed
139
140
Matrix Matrix::operator*(const Matrix& b) {
    Matrix c(b.dimx, this->dimy);
Mario Bajer's avatar
Mario Bajer committed
141
142
143
144
145
    for (int i = 0; i < this->dimx; i++) {
        for (int j = 0; j < b.dimx; j++) {
            bool helper;
            for (int k = 0; k < this->dimy; k++) {
                helper = c(i, j) != ((*this)(i, k) & b(k, j));
Mario Bajer's avatar
Mario Bajer committed
146
            }
Mario Bajer's avatar
Mario Bajer committed
147
148
149
150
151
152
153
            if (helper) {
                c.set(i,j);
            }else{
                c.reset(i,j);
            }
        }
    }
154
155
156
    return c;
}

157
void Matrix::threadedmult(Matrix* a, Matrix* b, std::size_t from, std::size_t to){
Mario Bajer's avatar
Mario Bajer committed
158
    bool helper1, helper2, helper3, helper4;
Mario Bajer's avatar
Mario Bajer committed
159
    for (size_t i = from; i < to; i++) {
160
161
        for (int j = 0; j < (*b).dimx; j++) {
            for (int k = 0; k < (*a).dimy; k++) {
Mario Bajer's avatar
Mario Bajer committed
162
                if (k > 0){
163
                    helper3 = (*a)(i, k) & (*b)(k, j);
Mario Bajer's avatar
Mario Bajer committed
164
165
166
                    helper4 = helper2;
                    helper2 = helper4 ^ helper3;
                }
167
                helper1 = (*a)(i, k) & (*b)(k, j);
Mario Bajer's avatar
Mario Bajer committed
168
169
170
                if (k == 0){
                    helper2 = helper1;
                }
Mario Bajer's avatar
Mario Bajer committed
171
            }
Mario Bajer's avatar
Mario Bajer committed
172
            if (helper2) {
Mario Bajer's avatar
Mario Bajer committed
173
174
175
176
177
178
179
180
                (*this).set(i,j);
            }else{
                (*this).reset(i,j);
            }
        }
    }
}

Mario Bajer's avatar
Mario Bajer committed
181
182
void Matrix::set(std::size_t x, std::size_t y){
    std::size_t matrixPos = dimx*x + y;
Mario Bajer's avatar
Mario Bajer committed
183
184
185
186
187
188
    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);
}

Mario Bajer's avatar
Mario Bajer committed
189
190
void Matrix::reset(std::size_t x, std::size_t y){
    std::size_t matrixPos = dimx*x + y;
Mario Bajer's avatar
Mario Bajer committed
191
192
193
194
195
    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);
}
196

Mario Bajer's avatar
Mario Bajer committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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;
    }
217
}