-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.c
196 lines (175 loc) · 5.41 KB
/
dataset.c
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
#include "dataset.h"
#include <stdlib.h>
#include <string.h>
#include "constants.h"
#include "utils.h"
void destroy_on_error(DATASET* dataset, int allocated_pointers) {
int i = 0;
for (i = 0; i < allocated_pointers; i++) {
free(dataset->DATA[i]);
}
free(dataset->DATA);
dataset->DATA = NULL;
dataset->n_edges = 0;
dataset->n_nodes = 0;
}
void destroy_dataset(DATASET* dataset) {
int i = 0;
for (i = 0; i < dataset->n_edges; i++) {
free(dataset->DATA[i]);
}
free(dataset->DATA);
free(dataset->name);
dataset->name = NULL;
dataset->DATA = NULL;
dataset->n_edges = 0;
dataset->n_nodes = 0;
}
int compare_dataset_entries(const void *a, const void *b) {
const int *_a = *(const int **)a;
const int *_b = *(const int **)b;
if(_a[0] == _b[0]) {
return _a[1] - _b[1];
} else {
return _a[0] - _b[0];
}
}
void sort_dataset(DATASET* dataset) {
qsort(dataset->DATA, dataset->n_edges, sizeof(*(dataset->DATA)), compare_dataset_entries);
}
int get_dataset_size(FILE *file, DATASET* dataset) {
char *readed_line = NULL;
ssize_t n_char_readed = 0;
size_t len = 0;
int status = STATUS_ERR;
while((n_char_readed = getline(&readed_line, &len, file)) != EOF) {
if (n_char_readed > 0) {
if (readed_line[0] != '#') {
break;
} else {
if (starts_with("# Nodes:", readed_line)) {
if (sscanf(readed_line, "# Nodes: %d Edges: %d", &(dataset->n_nodes), &(dataset->n_edges)) != EOF) {
free(readed_line);
readed_line = NULL;
len = 0;
if ((n_char_readed = getline(&readed_line, &len, file)) != EOF) {
status = STATUS_OK;
}
break;
}
}
}
}
free(readed_line);
readed_line = NULL;
len = 0;
}
return status;
}
int get_dataset_entry(FILE *file, int *to_node_id, int *from_node_id) {
char *readed_line = NULL;
ssize_t n_char_readed = 0;
size_t len = 0;
if ((n_char_readed = getline(&readed_line, &len, file) != EOF)) {
if (sscanf( readed_line, "%d %d", from_node_id, to_node_id) != EOF) {
return STATUS_OK;
} else {
return STATUS_ERR;
}
}
return EOF;
}
int get_dataset_entries(FILE *file, DATASET* dataset, int order) {
int to_node_id;
int from_node_id;
int status;
int i;
int **DATA;
i = 0;
DATA = dataset->DATA;
while((status = get_dataset_entry(file, &(to_node_id), &from_node_id)) == STATUS_OK || i < dataset->n_edges) {
DATA[i] = (int*)malloc(sizeof(int)*2);
if (DATA[i] != NULL) {
if (order == TO_NODE_ID_FIRST) {
DATA[i][0] = to_node_id;
DATA[i][1] = from_node_id;
} else {
DATA[i][0] = from_node_id;
DATA[i][1] = to_node_id;
}
} else {
destroy_on_error(dataset, i+1);
return STATUS_ERR;
}
i++;
}
if (dataset->n_edges != i) {
printf("[ERR] Number of edges mismatch: Header says %d but in file there are %d edges\n", dataset->n_edges, i);
return STATUS_ERR;
}
if (status != EOF) {
return STATUS_ERR;
}
if (order == TO_NODE_ID_FIRST) {
sort_dataset(dataset);
}
return STATUS_OK;
}
int set_dataset_name(char *file_path, DATASET* dataset, int order) {
int file_path_len = strlen(file_path);
dataset->name =(char*) malloc(file_path_len + 2);
if (dataset->name == NULL) {
printf("[ERR] Fail allocating memory for dataset name.\n");
return STATUS_ERR;
}
strcpy(dataset->name, file_path);
if (order == TO_NODE_ID_FIRST) {
dataset->name[file_path_len] = 'S';
} else {
dataset->name[file_path_len] = 'T';
}
// adjust the \0
dataset->name[file_path_len + 1] = '\0';
return STATUS_OK;
}
int read_dataset_from_file(char *file_path, DATASET* dataset, int order) {
FILE *f;
int status;
f = fopen(file_path,"r");
if (f == NULL) {
printf("[ERR] Fail opening the file.\n");
return STATUS_ERR;
}
if (get_dataset_size(f, dataset) == NOT_FOUND) {
printf("[ERR] Fail getting dataset size.\n");
return STATUS_ERR;
}
if (set_dataset_name(file_path, dataset, order) == STATUS_ERR) {
printf("[ERR] Error setting the dataset name.\n");
return STATUS_ERR;
}
dataset->DATA = (int**)malloc(sizeof(int*)*dataset->n_edges);
if (dataset->DATA == NULL) {
printf("[ERR] Error allocating memory for dataset.\n");
free(dataset->name);
return STATUS_ERR;
}
if ((status = get_dataset_entries(f, dataset, order)) == STATUS_ERR) {
printf("[ERR] Get dataset entries error.");
free(dataset->name);
return STATUS_ERR;
}
if (fclose(f) == EOF) {
printf("[ERR] Error closing the file.\n");
return STATUS_ERR;
}
return STATUS_OK;
}
void print_dataset(DATASET dataset) {
int i;
printf("***** DATASET *****\n");
printf("N_NODES: %d\tN_EDGES=%d\n", dataset.n_nodes, dataset.n_edges);
for (i = 0; i < dataset.n_edges; i++) {
printf("%d %d\n", dataset.DATA[i][0], dataset.DATA[i][1]);
}
}