-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
455 lines (354 loc) · 11.9 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Element {
int value;
int row;
int column;
struct Element * rowElement; /* -> */
struct Element * colElement; /* \|/ */
};
struct Header {
int index;
struct Header * header;
struct Element * element;
};
struct Matrix {
struct Header * headRowHeader;
struct Header * headColHeader;
int rows;
int cols;
int fillValue;
};
void blue_color() {
printf("\033[0;34m");
}
void white_color() {
printf("\033[0;37m");
}
void green_color() {
printf("\033[0;32m");
}
void red_color() {
printf("\033[1;31m");
}
void yellow_color() {
printf("\033[1;33m");
}
void reset_color() {
printf("\033[0m");
}
int print_header_ver(struct Header ** header) {
struct Header * p;
struct Element * element;
int size = 0;
if (!(*header)) {
red_color();
printf("Row header is empty\n");
reset_color();
return 0;
}
else
for (p = (*header); p != NULL; p = p->header) {
size += sizeof(p);
red_color();
printf("index: %d\n", p->index);
yellow_color();
for (element = p->element; element != NULL; element = element->colElement) {
printf("val: %d\n", element->value);
size += sizeof(element);
}
reset_color();
printf("\n");
}
return size;
}
int print_header_hor(struct Header ** header) {
struct Header * p;
struct Element * element;
int size = 0;
if (!(*header)) {
red_color();
printf("Column header is empty\n");
reset_color();
return 0;
}
else
for (p = (*header); p != NULL; p = p->header) {
size += sizeof(p);
red_color();
printf("index: %d\n", p->index);
yellow_color();
for (element = p->element; element != NULL; element = element->rowElement) {
printf("val: %d\n", element->value);
size += sizeof(element);
}
reset_color();
printf("\n");
}
return size;
}
void add_row_header(struct Header **mtrx_column, struct Header * column_header, struct Element * element) {
struct Header * p;
struct Header * q;
struct Element * p_element;
int header_index = column_header->index;
if (*mtrx_column == NULL) {
*mtrx_column = column_header;
(*mtrx_column)->element = element;
}
else {
/* Edge cases */
/* Prefix */
if ((*mtrx_column)->index > header_index) {
column_header->element = element; /* Added */
column_header->header = (*mtrx_column);
(*mtrx_column) = column_header;
}
/* Infix - Matching - Postfix */
else {
p = (*mtrx_column);
while (p) {
q = p->header;
/* Infix */
if (q && p->index < header_index && q->index > header_index) {
column_header->element = element; /* Added */
p->header = column_header;
column_header->header = q;
break;
}
/* Matching */
else if (p && p->index == header_index) {
p_element = p->element; /* ->rowElement */
while (p_element->colElement)
p_element = p_element->colElement;
p_element->colElement = element;
free(column_header);
break;
}
/* Postfix */
else if (p && p->header == NULL) {
column_header->element = element;
p->header = column_header;
break;
}
p = p->header;
}
}
}
}
/*add_col_header(mtrx.headColHeader, column_header, element);*/
void add_col_header(struct Header **mtrx_column, struct Header * column_header, struct Element * element) {
struct Header * p;
struct Header * q;
struct Element * p_element;
int header_index = column_header->index;
if (*mtrx_column == NULL) {
*mtrx_column = column_header;
(*mtrx_column)->element = element;
}
else {
/* Edge cases */
/* Prefix */
if ((*mtrx_column)->index > header_index) {
column_header->element = element; /* Added */
column_header->header = (*mtrx_column);
(*mtrx_column) = column_header;
}
/* Infix - Matching - Postfix */
else {
p = (*mtrx_column);
while (p) {
q = p->header;
/* Infix */
if (q && p->index < header_index && q->index > header_index) {
column_header->element = element; /* Added */
p->header = column_header;
column_header->header = q;
break;
}
/* Matching */
else if (p && p->index == header_index) {
p_element = p->element; /* ->rowElement */
while (p_element->rowElement)
p_element = p_element->rowElement;
p_element->rowElement = element;
free(column_header);
break;
}
/* Postfix */
else if (p && p->header == NULL) {
column_header->element = element;
p->header = column_header;
break;
}
p = p->header;
}
}
}
}
void mtrx_print(struct Matrix * mtrx) {
long int size = 0;
size += print_header_hor(&(mtrx->headColHeader));
printf("==========================\n\n");
size += print_header_ver(&(mtrx->headRowHeader));
printf("\n");
blue_color();
printf("Size of sparse mtrx: %ld\n", size);
reset_color();
}
struct Matrix mtrx_alloc(char * filename, int fillValue) {
/* Global Var(s) */
FILE *in_file;
struct Matrix mtrx;
char ch; /* Used to be 'int' */
int cols = 0;
int rows = 1;
/*int mtrx_cols = 0;
int mtrx_rows = 0;*/
struct Header * column_header;
struct Header * row_header;
struct Element * element;
in_file = fopen(filename, "r"); /*read only*/
if (in_file == NULL) {
printf("Error! Could not open file\n");
exit(-1);
}
/* Configure Matrix */
mtrx.headColHeader = NULL;
mtrx.headRowHeader = NULL;
while ((ch = fgetc(in_file)) != EOF) {
if (isdigit(ch)) ++cols; /* Increment column */
if (isdigit(ch) && (ch = ch-'0') != fillValue) {
/* printf("%d: col=%d, row=%d\n", ch, col, row); */
/* Configure Element */
element = malloc(sizeof(struct Element));
element->value = ch;
/*element->column = cols;
element->row = rows;*/
element->rowElement = NULL;
element->colElement = NULL;
/* Configure Column */
column_header = malloc(sizeof(struct Header));
column_header->index = cols;
column_header->header = NULL;
column_header->element = NULL;
/* Configure Row */
row_header = malloc(sizeof(struct Header));
row_header->index = rows;
row_header->header = NULL;
row_header->element = NULL;
add_col_header(&mtrx.headColHeader, column_header, element);
add_row_header(&mtrx.headRowHeader, row_header, element);
}
else if (ch == '\n') {
++rows; /* Increment row */
cols = 0;
}
}
/* Configure Matrix */
mtrx.rows = rows;
mtrx.cols = cols;
mtrx.fillValue = fillValue;
fclose(in_file);
return mtrx;
}
/* void mtrx_set(struct Matrix *mtrx, int row, int col, int val) {
int i;
int j;
struct Header *p;
struct Element *e;
p = mtrx->headColHeader;
for (i=0; i<col-1; ++i) p = p->header;
e = p->element;
for (j=0; j<row-1; ++j) e = e->rowElement;
printf("Element value: %d\n", e->value);
}*/
/*struct Element* mtrx_get_element(struct Matrix * mtrx, int row, int col) {
struct Header * colHeader = mtrx->headColHeader;
struct Element * element;
while (colHeader->index != col) {
colHeader = colHeader->header;
}
element = colHeader->element;
return element;
}*/
void mtrx_clear(struct Matrix * mtrx) {
struct Header * colHeader = mtrx->headColHeader;
struct Element * element = NULL;
struct Element * prev_element = NULL;
struct Header * p;
struct Header * prev_p;
while (colHeader) {
element = colHeader->element;
prev_element = element;
/* Go through Elements */
if (element) {
while (element) {
prev_element = element;
element = element->rowElement;
free(prev_element);
}
}
else free(prev_element);
/* Change Header */
colHeader = colHeader->header;
}
p = mtrx->headColHeader;
prev_p = p;
while (p) {
p = p->header;
free(prev_p);
prev_p = p;
}
p = mtrx->headRowHeader;
prev_p = p;
while (p) {
p = p->header;
free(prev_p);
prev_p = p;
}
mtrx->headColHeader = NULL;
mtrx->headRowHeader = NULL;
}
int main() {
/* int argc, char *argv[] */
int arr[16];
struct Matrix mtrx = mtrx_alloc("matrix.txt", 0);
/*green_color();
printf("(1, 5) => %d, p: %p\n", mtrx_get_element(&mtrx, 1, 5)->value, (void *)mtrx_get_element(&mtrx, 1, 5));
printf("(3, 1) => %d, p: %p\n", mtrx_get_element(&mtrx, 3, 1)->value, (void *)mtrx_get_element(&mtrx, 3, 1));
printf("(3, 4) => %d, p: %p\n", mtrx_get_element(&mtrx, 3, 4)->value, (void *)mtrx_get_element(&mtrx, 3, 4));
printf("(4, 3) => %d, p: %p\n", mtrx_get_element(&mtrx, 4, 3)->value, (void *)mtrx_get_element(&mtrx, 4, 3));
printf("(4, 5) => %d, p: %p\n", mtrx_get_element(&mtrx, 4, 5)->value, (void *)mtrx_get_element(&mtrx, 4, 5));
printf("(3, 3) => %d, p: %p\n", mtrx_get_element(&mtrx, 3, 3)->value, (void *)mtrx_get_element(&mtrx, 3, 3));
reset_color();*/
/* Print Mtrx contents */
/*green_color();
mtrx_set(&mtrx, 2, 3, 3);
reset_color();*/
mtrx_print(&mtrx);
blue_color();
arr[0] = 0;
arr[1] = 0;
arr[2] = 0;
arr[3] = 0;
arr[4] = 0;
arr[5] = 0;
arr[6] = 1;
arr[7] = 1;
arr[8] = 0;
arr[9] = 0;
arr[10] = 0;
arr[11] = 0;
arr[12] = 0;
arr[13] = 0;
arr[14] = 1;
arr[15] = 1;
printf("Size of simpler mtrx: %ld\n", sizeof(arr));
reset_color();
/* Clear Mtrx contents */
mtrx_clear(&mtrx);
return 0;
}