-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateAllList.h
210 lines (183 loc) · 6.69 KB
/
createAllList.h
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// Created by Sercan Eser on 27.12.2022.
//
#ifndef CMP2003_PROJECT_CREATEALLLIST_H
#define CMP2003_PROJECT_CREATEALLLIST_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <chrono>
#include <stdexcept>
using namespace std;
bool compareVectorPairFirst(pair<float,float> pair1,pair<float,float> pair2){
return (pair1.first<pair2.first);
}
pair<float,float> vectorPairBinarySearch(vector<pair<float,float> > vector1, int idBeingSearched, int low, int high){
while(high>low){
int mid = (high+low)/2;
if(idBeingSearched == vector1[mid].first)
return vector1[mid];
else if(idBeingSearched > vector1[mid].first){
low = mid + 1;
}
else{
high = mid - 1;
}
}
return make_pair(-1,-1);
}
template<typename T>
void pairInsertionSort(pair<T, T> *pair_list, int length, int mode, int order) {
/*
* mode = 0; Sorts the pair array with regard to the first element of pair.
* mode = 1; Sorts the pair array with regard to the second element of pair.
* order = 0; Sorts the pair in ascending order.
* order = 1; Sorts the pair in descending order.
*/
int i, j;
pair<T, T> key;
if (order == 0) {
switch (mode) {
case 0:
for (i = 1; i < length; i++) {
key = pair_list[i];
j = i - 1;
while (j >= 0 && pair_list[j].first > key.first) {
pair_list[j + 1] = pair_list[j];
j = j - 1;
}
pair_list[j + 1] = key;
}
break;
case 1:
for (i = 1; i < length; i++) {
key = pair_list[i];
j = i - 1;
while (j >= 0 && pair_list[j].second > key.second) {
pair_list[j + 1] = pair_list[j];
j = j - 1;
}
pair_list[j + 1] = key;
/*
// For printing the lists current order
for(int q = 0; q < length;q++){
cout << pair_list[q].second <<" ";
}
cout<<endl;
*/
}
break;
default:
throw invalid_argument("Mode can only be 0 (for sorting with regard to first element) "
"or 1 (for sorting with regard to second element).");
}
} else if (order == 1) {
switch (mode) {
case 0:
for (i = 1; i < length; i++) {
key = pair_list[i];
j = i - 1;
while (j >= 0 && pair_list[j].first < key.first) {
pair_list[j + 1] = pair_list[j];
j = j - 1;
}
pair_list[j + 1] = key;
}
break;
case 1:
for (i = 1; i < length; i++) {
key = pair_list[i];
j = i - 1;
while (j >= 0 && pair_list[j].second < key.second) {
pair_list[j + 1] = pair_list[j];
j = j - 1;
}
pair_list[j + 1] = key;
/*
// For printing the lists current order
for(int q = 0; q < length;q++){
cout << pair_list[q].second <<" ";
}
cout<<endl;
*/
}
break;
default:
throw invalid_argument("Mode can only be 0 (for sorting with regard to first element) "
"or 1 (for sorting with regard to second element).");
}
} else {
throw invalid_argument("Order can only be 0 (for sorting in ascending order) "
"or 1 (for sorting in descending order).");
}
for (i = 1; i < length; i++) {
key = pair_list[i];
j = i - 1;
while (j >= 0 && pair_list[j].second > key.second) {
pair_list[j + 1] = pair_list[j];
j = j - 1;
}
pair_list[j + 1] = key;
}
}
vector<vector<pair<float, float> > > createAllList(string const file_name, int mode) {
//mode = 0; is to create a user based vector (indices of outer vector will correspond to ID's of users.).
//mode = 1; is to create an item based vector (indices of outer vector will correspond to ID's of items.).
//Any other mode is undefined and will give an error if used.
//Vector that is being used to quickly access all the data in csv file.
vector<vector<pair<float, float> > > all;
//Variables used for reading the csv file.
vector<vector<string> > content;
vector<string> row;
string line, word;
// Largest User ID and Item ID in the given list.
int max_user_id = 0, max_item_id = 0;
fstream file(file_name, ios::in);
if (file.is_open()) {
while (getline(file, line)) {
row.clear();
stringstream str(line);
while (getline(str, word, ','))
row.push_back(word);
content.push_back(row);
}
} else
cout << "Could not open the file\n";
for (int i = 1; i < content.size(); i++) {
if (stoi(content[i][0]) > max_user_id) {
max_user_id = stoi(content[i][0]);
}
if (stoi(content[i][1]) > max_item_id) {
max_item_id = stoi(content[i][1]);
}
}
switch (mode) {
case 0:
for (int i = 0; i < max_user_id + 1; i++) {
vector<pair<float, float> > temp;
all.push_back(temp);
}
for (int i = 1; i < content.size(); i++) {
all[stoi(content[i][0])].push_back(make_pair(stof(content[i][1]), stof(content[i][2])));
}
break;
case 1:
for (int i = 0; i < max_item_id + 1; i++) {
vector<pair<float, float> > temp;
all.push_back(temp);
}
for (int i = 1; i < content.size(); i++) {
all[stoi(content[i][1])].push_back(make_pair(stof(content[i][0]), stof(content[i][2])));
}
break;
default:
throw invalid_argument("Mode can only be 0 (for user based list) or 1 (for item based list).");
}
for(int i = 0;i< all.size();i++) {
sort(all[i].begin(), all[i].end(), compareVectorPairFirst);
}
return all;
}
#endif //CMP2003_PROJECT_CREATEALLLIST_H