-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi21.c
More file actions
804 lines (587 loc) · 23.3 KB
/
api21.c
File metadata and controls
804 lines (587 loc) · 23.3 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//
// Created by mirko on 07/09/21.
//
//Li Veli Mirko
//------------------------------------------- LIBRERIE -------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
//--------------------------------------------- TEST ---------------------------------------------//
//#define DEBUG "input_1"
//#define DEBUG "input_2"
//#define DEBUG "input_3"
//#define DEBUG "input_4"
//#define DEBUG "input_5"
//#define DEBUG "input_6"
//#define STAMPA
//------------------------------------------- GLOBALI -------------------------------------------//
#define TXT 8
#define MAX UINT64_MAX
//static u_int64_t tmp_count = 0;
static char *stdin_buffer;
static int stdin_buffer_size;
static char *stdout_buffer;
static int stdout_buffer_size;
//------------------------------------------- DEFINIZIONI -------------------------------------------//
//
//typedef struct {
// u_int64_t id;
// u_int64_t peso;
// u_int64_t txt;
// unsigned char txt_len;
//} heap_element;
typedef struct {
u_int64_t id;
u_int64_t peso;
u_int64_t txt;
unsigned char txt_len;
} heap_element;
typedef struct {
heap_element *data;
u_int64_t capacita;
u_int64_t dimensione_attuale;
uint64_t volume;
} heap;
void Stampa_Classifica(heap *classifica);
void copy_classifica(heap_element *dacopiare, heap_element *dovecopiare, u_int64_t startdacopiare,
u_int64_t startdovecopiare, u_int64_t quanticopiarne);
void MERGE(heap_element *classifica, heap_element *temp, u_int64_t start, u_int64_t centro,
u_int64_t final);
void MERGESORT(heap_element *classifica, heap_element *temp, u_int64_t start, u_int64_t final);
void Acquisisci_Matrice(u_int64_t **matrice, int dimensione);
static inline void copia_heap_element(heap_element *from, heap_element *to);
static inline void stdin_parserow(int size, u_int64_t *ptr);
static inline void stdin_loadrow();
static inline int stdin_getfch();
static inline void stdin_init(int length_graph, int lenght_charts);
static inline void Replace_Element_End(heap *mio_heap, heap_element *da_inserire);
static inline void Replace_Element_Start(heap *mio_heap, heap_element *da_inserire);
static inline void Rebalance_Heap_MinHeap(heap *mio_heap);
u_int64_t
Ricerca_Binaria(heap_element *classifica, heap_element da_trovare, u_int64_t partenza, u_int64_t num_elem);
unsigned char convert(u_int64_t data, u_int64_t *buff);
static inline void _Copia_Elemento_Heap_(heap_element *from, heap_element *to);
void Crea_Heap(heap *mio_heap, u_int64_t dimensione, bool big);
static inline void Copia_Elemento_Heap(heap *mio_heap, u_int64_t from, u_int64_t to);
static inline u_int64_t Heap_Left(u_int64_t index);
static inline u_int64_t Heap_Right(u_int64_t index);
static inline void Swap_Heap_Element(heap *mio_heap, u_int64_t indexA, u_int64_t indexB);
static inline void Min_Heapify(heap *mio_heap, u_int64_t index);
static inline bool Estrai_Minimo_Heap(heap *mio_heap, heap_element *dove_salvare);
static inline u_int64_t padre(u_int64_t index);
static inline void Insert_Element_MinHeap(heap *mio_heap, heap_element *da_inserire);
static inline void Add_To_Heap(heap *mio_heap, u_int64_t id, u_int64_t peso);
inline static void inizializza_elemento_classifica(heap_element *ptr, u_int64_t id, u_int64_t peso);
void Inizializza_Grafo(u_int64_t *peso, bool *esaminato, int dimensione);
u_int64_t
CalcoloPunteggio(u_int64_t **matrice, u_int64_t *peso_array, heap *peso_heap, bool *esaminato, int dimensione);
bool Find_Next(heap *peso_heap, const bool *esaminato, u_int64_t *result);
static inline void Rebalance_Heap_MaxHeap(heap *mio_heap);
static inline void Insert_Element_MaxHeap(heap *mio_heap, heap_element *da_inserire);
static inline void Max_Heapify(heap *mio_heap, u_int64_t index);
//----------------------------------------------------- IN PROVA -----------------------------------------------------//
//----------------------------------------------------- MAIN ----------------------------------------------------//
int main() {
#ifdef DEBUG
FILE *input = freopen("/home/mirko/CLionProjects/API2021/open_tests/"DEBUG, "r", stdin);
#endif
int n_node;
u_int64_t n_elementi_classifica;
u_int64_t i = 0;
// u_int64_t migliore = MAX;
// u_int64_t peggiore = MAX;
u_int64_t contatoregrafi = 0;
//Prendo il numero di nodi e il numero di elementi della classifica
if (scanf("%d %lu\n", &n_node, &n_elementi_classifica) != 2) {
return -1;
}
stdin_init(n_node, n_elementi_classifica);
// printf("%lu %lu\n", n_node, n_elementi_classifica);
//Creo la classifica con n_elementi_classifica+1 elementi
//L'elemento in più lo usero per aggiungere gli elementi partendo dalla coda
// heap_element *classifica = (heap_element *) malloc(
// (n_elementi_classifica + 1) * sizeof(heap_element));
//
// //Alloco la classifica temporanea per riordinare quella presente
// heap_element *temp = (heap_element *) malloc((n_elementi_classifica + 1) * sizeof(heap_element));
//Creo la matrice di adiacenza contenente i costi delle transizioni
u_int64_t **matr_costi = (u_int64_t **) malloc(n_node * sizeof(u_int64_t *));
while (i < n_node) {
matr_costi[i] = (u_int64_t *) malloc(n_node * sizeof(u_int64_t));
i++;
}
//NUOVO NODOYUPPI
heap peso_heap;
Crea_Heap(&peso_heap, n_node, true);
heap classifica;
Crea_Heap(&classifica, n_elementi_classifica, false);
for (int j = 0; j < classifica.capacita; j++) {
classifica.data[j].peso = MAX;
}
u_int64_t *peso_array = (u_int64_t *) malloc(n_node * sizeof(u_int64_t));
bool *esaminato = (bool *) malloc(n_node * sizeof(bool));
while (1) {
stdin_loadrow();
int cmd = stdin_getfch();
switch (cmd) {
case 'A':
// printf("Identificato AggiungiGrafo\n");
//
Inizializza_Grafo(peso_array, esaminato, n_node);
Acquisisci_Matrice(matr_costi, n_node);
//
// if (contatoregrafi == n_elementi_classifica) {
// MERGESORT(classifica, temp, 0, n_elementi_classifica - 1);
// }
//QUESTO IF VERIFICA CHE CI SIANO SUFFICIENTI GRAFI PER RIEMPIRE LA CLASSIFICA.
//SE IF SODDISFATTO, CLASSIFICA NON PIENA, VA QUINDI AGGIUNTO IL NUOVO ELEMENTO IN CLASSIFICA
heap_element tmp;
inizializza_elemento_classifica(&tmp, contatoregrafi,
CalcoloPunteggio(matr_costi, peso_array, &peso_heap,
esaminato, n_node));
if (classifica.dimensione_attuale == classifica.volume) {
if (tmp.peso < classifica.data[0].peso) {
Replace_Element_Start(&classifica, &tmp);
Max_Heapify(&classifica, 0);
}
} else {
Insert_Element_MaxHeap(&classifica, &tmp);
}
// }
// if (classifica.dimensione_attuale == classifica.volume) {
// classifica.dimensione_attuale--;
//// classifica.dimensione_attuale+=2;
// }
//// else {
////// copia_heap_element(&tmp, &classifica.data[classifica.dimensione_attuale]);
////// Rebalance_Heap_MinHeap(&classifica);
////// classifica.dimensione_attuale++;
//// }
////
//// copia_heap_element(&tmp, &classifica.data[classifica.dimensione_attuale]);
////// classifica.dimensione_attuale-=2;
//
// Insert_Element_MaxHeap(&classifica, &tmp);
// Rebalance_Heap_MaxHeap(&classifica);
// classifica.dimensione_attuale++;
//
// if (contatoregrafi < n_elementi_classifica) {
//
//
// inizializza_elemento_classifica(&(classifica[contatoregrafi]), contatoregrafi,
// CalcoloPunteggio(matr_costi, peso_array, &peso_heap,
// esaminato, n_node));
//
//
// if (peggiore < classifica[contatoregrafi].peso) {
// peggiore = classifica[contatoregrafi].peso;
// }
// if (classifica[contatoregrafi].peso < migliore) {
// migliore = classifica[contatoregrafi].peso;
// }
// }
//ENTRO IN QUESTO ELSE SE LA CLASSIFICA È PIENA
// else {
//
// inizializza_elemento_classifica(&(classifica[n_elementi_classifica]), contatoregrafi,
// CalcoloPunteggio(matr_costi, peso_array, &peso_heap,
// esaminato, n_node));
//QUESTA CONDIZIONE VERIFICA CHE IL MIO NUOVO PUNTEGGIO SIA MIGLIORE DEL PUNTEGGIO PEGGIORE IN CLASSIFICA
//SE L'IF È SODDISFATTO, ALLORA IL NUOVO ELEMENTO DEVE ESSERE AGGIUNTO ALLA CLASSIFICA
// if (classifica[n_elementi_classifica].peso < migliore) {
// Inserimento_In_Testa(classifica, classifica[n_elementi_classifica], n_elementi_classifica);
// migliore = classifica[0].peso;
// } else {
// if (peggiore > classifica[n_elementi_classifica].peso) {
// Inserimento_Ordinato(classifica, n_elementi_classifica, classifica[n_elementi_classifica]);
// peggiore = classifica[n_elementi_classifica].peso;
// }
// }
// }
// Stampa_Classifica(classifica, n_elementi_classifica);
// printf("%lu, %lu\n", tmp.id, tmp.peso);
#ifdef STAMPA
Stampa_Classifica(&classifica);
#endif
contatoregrafi++;
break;
case 'T':
// printf("Identificato TopK\n");
if (classifica.dimensione_attuale > 0) {
Stampa_Classifica(&classifica);
} else {
printf("\n");
}
// if (contatoregrafi > 0) {
// if (contatoregrafi < n_elementi_classifica) {
// Stampa_Classifica(classifica, contatoregrafi);
// } else {
// Stampa_Classifica(classifica, n_elementi_classifica);
// }
// } else {
// printf("\n");
// }
break;
case EOF:
// if (tmp_count > 10000000){
// return -1;
// }
// printf("Identificato END\n");
free(stdout_buffer);
free(stdin_buffer);
free(classifica.data);
i = 0;
while (i < n_node){
free(matr_costi[i]);
i++;
}
free(matr_costi);
free(esaminato);
free(peso_array);
free(peso_heap.data);
// free(temp);
#ifdef DEBUG
fclose(input);
#endif
return 0;
default:
printf("Error in 'identifica comando @ %lu (%d)'\n", contatoregrafi, cmd);
return -1;
}
}
}
//-------------------------------------- NOT MAIN --------------------------------------//
static inline void print_to_buff(char **buff, u_int64_t *txt_, int size, char term) {
char *txt = (char *) txt_;
for (int i = 0; i < size; i++) {
**buff = *txt;
(*buff)++;
txt++;
}
**buff = term;
(*buff)++;
}
static inline void copia_heap_element(heap_element *from, heap_element *to) {
to->id = from->id;
to->peso = from->peso;
to->txt = from->txt;
to->txt_len = from->txt_len;
}
void Stampa_Classifica(heap *classifica) {
// tmp_count += n_elementi_da_stampare;
u_int64_t i;
//---------STAMPA DI DEBUG----------//
#ifdef STAMPA
// for (i = 0; i < n_elementi_da_stampare; i++) {
// printf("%lu, %lu\n", classifica[i].id, classifica[i].peso);
// }
// printf("\n");
for (int i = 0; i < classifica->dimensione_attuale;i++){
printf("%lu %lu\n", classifica->data[i].id, classifica->data[i].peso);
}
printf("--------\n");
return;
#endif
//------STAMPA PER PROGETTO------//
stdout_buffer[0] = '\0';
char *curr = stdout_buffer;
for (i = 0; i < classifica->dimensione_attuale - 1; i++) {
print_to_buff(&curr, &(classifica->data[i].txt), classifica->data[i].txt_len, ' ');
}
print_to_buff(&curr, &(classifica->data[i].txt), classifica->data[i].txt_len, '\n');
*curr = '\0';
fputs(stdout_buffer, stdout);
}
unsigned char convert(u_int64_t data, u_int64_t *buff_) {
int i = 2;
char *buff = (char *) buff_;
buff[TXT - 1] = '\0';
if (data == 0) {
buff[TXT - i] = '0';
i++;
} else {
while (data > 0) {
char temp = data % 10;
buff[TXT - i] = temp + 48;
data /= 10;
i++;
}
}
int shift = (TXT - i + 1) * 8;
u_int64_t test = (*buff_) >> shift;
(*buff_) = test;
return (i - 2);
}
void copy_classifica(heap_element *dacopiare, heap_element *dovecopiare, u_int64_t startdacopiare,
u_int64_t startdovecopiare, u_int64_t quanticopiarne) {
dovecopiare += startdovecopiare;
dacopiare += startdacopiare;
for (int i = 0; i < quanticopiarne; i++) {
copia_heap_element(dacopiare, dovecopiare);
dacopiare++;
dovecopiare++;
}
// memcpy(&(dovecopiare[startdovecopiare]), &(dacopiare[startdacopiare]), quanticopiarne * sizeof(heap_element));
}
void MERGE(heap_element *classifica, heap_element *temp, u_int64_t start, u_int64_t centro,
u_int64_t final) {
u_int64_t i = start;
u_int64_t j = centro + 1;
u_int64_t k = 0;
// size_t stazza = sizeof(heap_element);
while (i <= centro && j <= final) {
//SE i < j, inserisco i in temp
if (classifica[i].peso <= classifica[j].peso) {
copia_heap_element(&(classifica[i]), &(temp[k]));
i++;
//altrimenti inserisco j
} else {
copia_heap_element(&(classifica[j]), &(temp[k]));
j++;
}
k++;
}
//se ho preso più elementi da left rispetto righ, finisco di inserire i right in temp
if (i > centro) {
while (j <= final) {
copia_heap_element(&(classifica[j]), &(temp[k]));
j++;
k++;
}
}
//entro nell'else se ho preso più elemtni di right rispetto a left
//finisco l'inserimento degli elemtni di left in temp
else {
while (i <= centro) {
copia_heap_element(&(classifica[i]), &(temp[k]));
i++;
k++;
}
}
copy_classifica(temp, classifica, 0, start, k);
}
void MERGESORT(heap_element *classifica, heap_element *temp, u_int64_t start, u_int64_t final) {
// start: indice iniziale dell'array
// final: indice finale dell'array
// centro: indice intermedio dell'array
// Divido l'array in 2 sottoarray; Se l'array ha un elemento, è già ordinato e non faccio nulla.
if (start < final) {
u_int64_t centro = ((start + final) >> 1);
MERGESORT(classifica, temp, start, centro);
MERGESORT(classifica, temp, centro + 1, final);
MERGE(classifica, temp, start, centro, final);
}
}
void Acquisisci_Matrice(u_int64_t **matrice, int dimensione) {
int i;
for (i = 0; i < dimensione; i++) {
stdin_loadrow();
stdin_parserow(dimensione, matrice[i]);
}
}
u_int64_t
CalcoloPunteggio(u_int64_t **matrice, u_int64_t *peso_array, heap *peso_heap, bool *esaminato, int dimensione) {
u_int64_t i;
int j;
u_int64_t tot = 0;
u_int64_t nuovo_peso;
Add_To_Heap(peso_heap, 0, 0);
// esaminato[0] = true;
while (Find_Next(peso_heap, esaminato, &i)) {
for (j = 1; j < dimensione; j++) {
// if(i != j && matrice[i][j] != 0){
nuovo_peso = matrice[i][j] + peso_array[i];
if (matrice[i][j] != 0 && nuovo_peso < peso_array[j]) {
peso_array[j] = nuovo_peso;
Add_To_Heap(peso_heap, j, nuovo_peso);
}
}
esaminato[i] = 1;
tot += peso_array[i];
}
return tot;
}
void Inizializza_Grafo(u_int64_t *peso, bool *esaminato, int dimensione) {
peso[0] = 0;
esaminato[0] = 0;
for (int i = 1; i < dimensione; ++i) {
peso[i] = MAX;
esaminato[i] = 0;
}
}
bool Find_Next(heap *peso_heap, const bool *esaminato, u_int64_t *result) {
heap_element temp;
do {
if (!Estrai_Minimo_Heap(peso_heap, &temp)) {
return false;
}
} while (esaminato[temp.id]);
*result = temp.id;
return true;
}
static inline void stdin_parserow(int size, u_int64_t *ptr) {
char *tmp_ptr = stdin_buffer;
for (int i = 0; i < size; i++) {
u_int64_t toReturn = 0;
while (*tmp_ptr != ',' && *tmp_ptr != '\n') {
toReturn *= 10;
toReturn += (*tmp_ptr) - 48;
tmp_ptr++;
}
tmp_ptr++;
*ptr = toReturn;
ptr++;
}
}
static inline void stdin_loadrow() {
if (fgets(stdin_buffer, stdin_buffer_size, stdin) == NULL && feof(stdin)) {}
}
static inline int stdin_getfch() {
if (feof(stdin)) {
return EOF;
} else {
return stdin_buffer[0];
}
}
static inline void stdin_init(int length_graph, int length_charts) {
stdin_buffer_size = length_graph * 10 + length_graph + 10;
stdin_buffer = malloc(stdin_buffer_size);
stdout_buffer_size = (length_charts + 1) * 10 + length_charts + 10;
stdout_buffer = malloc(stdout_buffer_size);
}
u_int64_t
Ricerca_Binaria(heap_element *classifica, heap_element da_trovare, u_int64_t partenza, u_int64_t num_elem) {
u_int64_t mezzo = partenza + ((num_elem - partenza) >> 1);
if (da_trovare.peso < classifica[partenza].peso) {
return partenza;
}
if (classifica[mezzo].peso == da_trovare.peso) {
return (mezzo + 1);
}
if (da_trovare.peso > classifica[mezzo].peso) {
return Ricerca_Binaria(classifica, da_trovare, mezzo + 1, num_elem);
}
return Ricerca_Binaria(classifica, da_trovare, partenza, mezzo - 1);
}
static inline void _Copia_Elemento_Heap_(heap_element *from, heap_element *to) {
to->peso = from->peso;
to->id = from->id;
to->txt_len = from->txt_len;
to->txt = from->txt;
}
static inline void Swap_Heap_Element(heap *mio_heap, u_int64_t indexA, u_int64_t indexB) {
heap_element temp;
_Copia_Elemento_Heap_(&(mio_heap->data[indexA]), &temp);
_Copia_Elemento_Heap_(&(mio_heap->data[indexB]), &(mio_heap->data[indexA]));
_Copia_Elemento_Heap_(&temp, &(mio_heap->data[indexB]));
}
static inline u_int64_t Heap_Right(u_int64_t index) {
return (2 * index + 2);
}
static inline void Min_Heapify(heap *mio_heap, u_int64_t index) {
u_int64_t left = Heap_Left(index);
u_int64_t right = Heap_Right(index);
u_int64_t piccolo = index;
if (left < (mio_heap->dimensione_attuale + 1) && mio_heap->data[left].peso < mio_heap->data[index].peso) {
piccolo = left;
}
if (right < (mio_heap->dimensione_attuale + 1) && mio_heap->data[right].peso < mio_heap->data[piccolo].peso) {
piccolo = right;
}
if (piccolo != index) {
Swap_Heap_Element(mio_heap, index, piccolo);
Min_Heapify(mio_heap, piccolo);
}
}
static inline void Max_Heapify(heap *mio_heap, u_int64_t index) {
u_int64_t left = Heap_Left(index);
u_int64_t right = Heap_Right(index);
u_int64_t piccolo = index;
if (left < (mio_heap->dimensione_attuale) && mio_heap->data[left].peso > mio_heap->data[index].peso) {
piccolo = left;
}
if (right < (mio_heap->dimensione_attuale) && mio_heap->data[right].peso > mio_heap->data[piccolo].peso) {
piccolo = right;
}
if (piccolo != index) {
Swap_Heap_Element(mio_heap, index, piccolo);
Max_Heapify(mio_heap, piccolo);
}
}
static inline bool Estrai_Minimo_Heap(heap *mio_heap, heap_element *dove_salvare) {
if (mio_heap->dimensione_attuale == 0) {
return false;
}
_Copia_Elemento_Heap_(&(mio_heap->data[0]), dove_salvare);
mio_heap->dimensione_attuale--;
if (mio_heap->dimensione_attuale > 0) {
Copia_Elemento_Heap(mio_heap, mio_heap->dimensione_attuale, 0);
Min_Heapify(mio_heap, 0);
}
// printf("Extract Heap %lu %lu\n", dove_salvare->id, dove_salvare->peso);
return true;
}
static inline u_int64_t padre(u_int64_t index) {
return ((index - 1) >> 1);
}
static inline void Replace_Element_End(heap *mio_heap, heap_element *da_inserire) {
_Copia_Elemento_Heap_(da_inserire, &(mio_heap->data[mio_heap->dimensione_attuale]));
}
static inline void Replace_Element_Start(heap *mio_heap, heap_element *da_inserire) {
_Copia_Elemento_Heap_(da_inserire, &(mio_heap->data[0]));
}
static inline void Rebalance_Heap_MinHeap(heap *mio_heap) {
u_int64_t i = mio_heap->dimensione_attuale;
// heap_element padre = mio_heap->data[padre(i)];
while (i != 0 && mio_heap->data[padre(i)].peso > mio_heap->data[i].peso) {
Swap_Heap_Element(mio_heap, i, padre(i));
i = padre(i);
}
}
static inline void Rebalance_Heap_MaxHeap(heap *mio_heap) {
u_int64_t i = mio_heap->dimensione_attuale;
// heap_element padre = mio_heap->data[padre(i)];
while (i != 0 && mio_heap->data[padre(i)].peso < mio_heap->data[i].peso) {
Swap_Heap_Element(mio_heap, i, padre(i));
i = padre(i);
}
}
static inline void Insert_Element_MinHeap(heap *mio_heap, heap_element *da_inserire) {
Replace_Element_End(mio_heap, da_inserire);
// assert(mio_heap->dimensione_attuale < mio_heap->capacita);
Rebalance_Heap_MinHeap(mio_heap);
mio_heap->dimensione_attuale++;
}
static inline void Insert_Element_MaxHeap(heap *mio_heap, heap_element *da_inserire) {
Replace_Element_End(mio_heap, da_inserire);
// assert(mio_heap->dimensione_attuale < mio_heap->capacita);
Rebalance_Heap_MaxHeap(mio_heap);
mio_heap->dimensione_attuale++;
}
static inline void Add_To_Heap(heap *mio_heap, u_int64_t id, u_int64_t peso) {
// printf("Add Heap %lu %lu\n", id, peso);
heap_element temp = {
.peso = peso,
.id = id,
};
Insert_Element_MinHeap(mio_heap, &temp);
}
static inline void Copia_Elemento_Heap(heap *mio_heap, u_int64_t from, u_int64_t to) {
_Copia_Elemento_Heap_(&(mio_heap->data[from]), &(mio_heap->data[to]));
}
static inline u_int64_t Heap_Left(u_int64_t index) {
return ((index << 1) + 1);
}
void Crea_Heap(heap *mio_heap, u_int64_t dimensione, bool big) {
mio_heap->capacita = dimensione * (big?50:1);
mio_heap->data = malloc(mio_heap->capacita * sizeof(heap_element));
mio_heap->dimensione_attuale = 0;
mio_heap->volume = dimensione;
}
inline static void inizializza_elemento_classifica(heap_element *ptr, u_int64_t id, u_int64_t peso) {
ptr->id = id;
ptr->txt_len = convert(id, &(ptr->txt));
ptr->peso = peso * 16777216 + id;
}