-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.c
More file actions
100 lines (61 loc) · 2.33 KB
/
input.c
File metadata and controls
100 lines (61 loc) · 2.33 KB
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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
/**
* The method assign length and matrix to the corresponding pointers.
* return 0 if the process succeed.
*/
int getInputMatrix(char* binaryInput, int *lengthPointer, int* degreesSum, int** degreesArray, int***matrixPointer) {
/*-----------------------------------declarations-----------------------------------*/
FILE *file ;
int numOfNodes;
int ass = 0; /* this variable is being set to 1 in order to assert if needed */
int **matrix; /* this is the matrix which we generate from the input file */
int* myDegreesArray;
int i, j, q; /* those variables are being used to iterate */
int nodeDegree;
int neighborIndex;
int myDegreesSum = 0;
/*-----------------------------------code-----------------------------------*/
if(lengthPointer == 0){};
if(matrixPointer == 0){};
if(degreesSum == 0){};
if(degreesArray == 0){};
/* "Validating input file. */
assert(binaryInput != 0);
assert(binaryInput != NULL);
file = fopen(binaryInput, "r");
assert(file!=NULL);
/* "The input is valid. */
/*-----------------------------------*/
/* "Start - Creating the matrix... \n" */
ass = fread(&numOfNodes, sizeof(int), 1, file);
assert(ass == 1);
matrix = (int**)calloc(numOfNodes, sizeof(int*));
myDegreesArray = (int*)calloc(numOfNodes, sizeof(int));
/* The following code can be optimized by keeping the next neighbor - it will lead to insert value to each node only once (now can be twice if neighbors).
* another optimization can be done with the face the the matrix is symmetric. */
for(i = 0 ; i < numOfNodes ; i++){ /* i represents the i_th node */
matrix[i] = (int*)calloc(numOfNodes, sizeof(int));
for(j = 0 ; j < numOfNodes; j++){ /* filling 0's as a default value - as the nodes were not neighbors */
matrix[i][j] = 0;
}
ass = fread(&nodeDegree, sizeof(int), 1, file);
assert(ass == 1);
myDegreesSum += nodeDegree;
myDegreesArray[i] = nodeDegree;
for (q = 0; q < nodeDegree; q++){
ass = fread(&neighborIndex, sizeof(int), 1, file);
assert(ass == 1);
matrix[i][neighborIndex] = 1;
}
}
/*free(neighborNodes);*/
/* "End - Creating the matrix. */
/*-----------------------------------*/
*matrixPointer = matrix;
*degreesArray = myDegreesArray;
*lengthPointer = numOfNodes;
*degreesSum = myDegreesSum;
return EXIT_SUCCESS;
}