-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
101 lines (86 loc) · 3.1 KB
/
matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "matrix.h"
using namespace std;
#define INITAL_VALUE 0.2
Matrix::Matrix(std::pair<int, int> dimentions){
this->dimentions = dimentions;
this->x_dimention = dimentions.first;
this->y_dimention = dimentions.second;
}
void Matrix::create_matrix(){
this->matrix = new float*[x_dimention + 1];
for (int i = 0; i < x_dimention + 1; i++)
{
this->matrix[i] = new float[y_dimention + 1];
}
}
void Matrix::fill_ones(){
for(int i = 0; i < x_dimention + 1; i++)
{
for(int j = 0; j < y_dimention + 1; j++)
{
matrix[i][j] = INITAL_VALUE;
}
}
}
void Matrix::fill_matrix(std::vector<array<int, 5>> input_vector){
for(unsigned i = 0; i < input_vector.size(); i++){
this->matrix[get<0>(input_vector[i])][get<1>(input_vector[i])] = (float)get<2>(input_vector[i]);
}
}
int max_in_vector(vector<array<int, 5>> vetor,unsigned const int index){
int max_value = 0;
for(unsigned i = 0; i < vetor.size(); i++){
if(vetor[i][index] > max_value){
max_value = vetor[i][index];
}
}
return max_value;
}
pair<int,int> get_matrix_dimentions(vector<array<int, 5>> vetor){
pair<int,int> max_values;
max_values.first = max_in_vector(vetor, 0);
max_values.second = max_in_vector(vetor, 1);
return max_values;
}
vector<array<int, 5>> process_inputs(string filename){
ifstream file;
file.open(filename);
string line;
std::vector<array<int, 5>> inputs;
while(getline(file,line)){
if (line.find(",") != std::string::npos){
string work_line = line;
string delimiter = ":";
int user = atoi(work_line.substr(1, work_line.find(delimiter)-1).c_str());
work_line = line;
delimiter = ":";
string delimiter2 = ",";
int item = atoi(work_line.substr( work_line.find(delimiter)+2, work_line.find(delimiter2)).c_str());
work_line = line;
delimiter = ",";
work_line = work_line.substr(work_line.find(delimiter)+1, -1);
int rating = atoi(work_line.substr(0, work_line.find(delimiter)).c_str());
work_line = line;
delimiter = ",";
work_line = work_line.substr(work_line.find(delimiter)+1, -1);
int timestamp = atoi(work_line.substr(work_line.find(delimiter)+1, -1).c_str());
if(user == 0 and item == 0) continue;
inputs.push_back({user, item, rating, timestamp, 0});
}
else{
string work_line = line;
string delimiter = ":";
int user = atoi(work_line.substr(1, work_line.find(delimiter)-1).c_str());
work_line = line;
delimiter = ":";
string delimiter2 = ",";
int item = atoi(work_line.substr( work_line.find(delimiter)+2, work_line.find(delimiter2)).c_str());
int rating = 0;
int timestamp = 0;
if(user == 0 and item == 0) continue;
inputs.push_back({user, item, rating, timestamp, 0});
}
}
file.close();
return inputs;
}