-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·1572 lines (1450 loc) · 43.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
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by Gianluca Regis on 29/05/2019.
// Copyright © 2019 Gianluca Regis. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define INPUT 1000
#define HASH_DIMENSION 26
#define NAME 333
//********************************************************************************************* Strutture
// Iterations list (one for each entity)
struct listaInter
{
short rel;
bool dest;
struct treeEnt *altraEntNode;
struct listaCont *cont;
struct listaInter *otherInter;
struct listaInter *prevPtr;
struct listaInter *nextPtr;
};
// Create some aliases
typedef struct listaInter ListaInter;
typedef ListaInter *ListaInterPtr;
// Entities tree structure (each one linked to Hash arrays)
struct treeEnt
{
unsigned short id;
char idEnt[NAME];
ListaInterPtr interazioniOrig[HASH_DIMENSION];
ListaInterPtr interazioniDest[HASH_DIMENSION];
struct treeEnt *pPtr;
struct treeEnt *leftPtr;
struct treeEnt *rightPtr;
};
// Create some aliases
typedef struct treeEnt TreeEnt;
typedef TreeEnt *TreeEntPtr;
// Counters list (one for each relation)
struct listaCont
{
unsigned short contatore_rel;
TreeEntPtr destNode;
struct listaCont *prevPtr;
struct listaCont *nextPtr;
};
// Create some aliases
typedef struct listaCont ListaCont;
typedef ListaCont *ListaContPtr;
// Relations list
struct listaRel
{
unsigned short idRel;
char relazione[NAME];
bool delent;
ListaContPtr contatori;
struct listaRel *prevPtr;
struct listaRel *nextPtr;
};
// Create some aliases
typedef struct listaRel ListaRel;
typedef ListaRel *ListaRelPtr;
//********************************************************************************************* Variabili globali
unsigned short cont = 0;
unsigned short contRel = 0;
TreeEntPtr Hash[HASH_DIMENSION];
ListaRelPtr relRoot;
//********************************************************************************************* Prototipi funzioni
void addent(char string[]);
void delent(char string[]);
void addrel(char string[]);
void delrel(char string[]);
void report(void);
void hashInsert(char idEnt[]);
void hashDelete(TreeEntPtr entNode);
TreeEntPtr hashSearch(unsigned int key, char idEnt[]);
void treeEntInsert(TreeEntPtr EntRoot, unsigned short key, char nome[]);
void treeEntDelete(TreeEntPtr *EntRoot, TreeEntPtr treeNode);
void sistemaInterazioni(ListaInterPtr interazioniOrig[], ListaInterPtr interazioniDest[], TreeEntPtr entNode);
TreeEntPtr treeEntSuccessor(TreeEntPtr treeNode);
TreeEntPtr treeEntMinimum(TreeEntPtr treeNode);
TreeEntPtr treeEntSearch(TreeEntPtr node, char idEnt[]);
void listaInterInsert(TreeEntPtr destNode, ListaContPtr *contRoot, TreeEntPtr altraEnt, unsigned short rel);
ListaInterPtr listaInterAdd(ListaInterPtr *interRoot, ListaInterPtr altraInter, ListaContPtr contNode, TreeEntPtr altraEnt, unsigned short rel);
void listaInterCrea(TreeEntPtr destNode, ListaContPtr *contNode, TreeEntPtr origNode, unsigned short rel);
void listaInterDelete(TreeEntPtr destNode, TreeEntPtr origNode, ListaRelPtr relNode);
void altraInterDelete(ListaInterPtr *interRoot, ListaInterPtr interNode);
void interDelent(TreeEntPtr entNode);
ListaInterPtr altraEntInterDelete(ListaInterPtr interNode);
void ripristinaSnap(void);
ListaRelPtr listaRelInsert(char rel[]);
ListaContPtr listaContInsert(ListaContPtr *contRoot, TreeEntPtr entNode);
void listaContDelete(ListaContPtr *contRoot, unsigned short dest);
void listaContDecrementa(ListaContPtr *contRoot, ListaContPtr contNode);
void listaContSnap(ListaContPtr *contRoot, ListaContPtr contNode);
ListaRelPtr listaRelSearch(char rel[]);
bool listaRelDelete(ListaRelPtr relNode);
ListaContPtr swap(ListaContPtr primoPtr, ListaContPtr secondoPtr);
//************************************************************************************************* MAIN
int main()
{
// Initialize to NULL the relations list
relRoot = NULL;
// Initialize to NULL the hash array
unsigned int i;
for (i = 0; i < HASH_DIMENSION; i++)
{
Hash[i] = NULL;
}
// Read input
char input[INPUT];
char s;
unsigned long contRiga = 0;
while (i != 1)
{
s = *fgets(input, INPUT, stdin);
s++;
contRiga++;
if (input[0] == 'a')
{
if (input[3] == 'e')
{
addent(input);
}
else
{
addrel(input);
}
}
else if (input[0] == 'd')
{
if (input[3] == 'e')
{
delent(input);
}
else
{
delrel(input);
}
}
else if (input[0] == 'r')
{
report();
}
else if (input[0] == 'e')
{
i = 1;
}
else
{
printf("Errore\n");
}
}
return 0;
}
//************************************************ FUNCTIONS
// Start monitoring an entity (adding entity to entities hash structure)
void addent(char string[])
{
unsigned int i = 8, j = 0;
char idEnt[NAME];
while (string[i] != '"')
{
idEnt[j] = string[i];
i++;
j++;
}
idEnt[j] = '\0';
hashInsert(idEnt);
}
// Stop monitoring an entity (delete entity from entities hash structure)
void delent(char string[])
{
unsigned int i = 8, j = 0;
char idEnt[NAME];
while (string[i] != '"')
{
idEnt[j] = string[i];
i++;
j++;
}
idEnt[j] = '\0';
TreeEntPtr entNode;
entNode = hashSearch(idEnt[0] % HASH_DIMENSION, idEnt);
if (entNode != NULL)
{
interDelent(entNode);
hashDelete(entNode);
ripristinaSnap();
}
}
// Adds a new relationship between two entities
void addrel(char string[])
{
unsigned int i = 8, j = 0;
char idOrig[NAME];
char idDest[NAME];
char rel[NAME];
TreeEntPtr origNode;
TreeEntPtr destNode;
while (string[i] != '"')
{
idOrig[j] = string[i];
i++;
j++;
}
idOrig[j] = '\0';
i = i + 3;
j = 0;
while (string[i] != '"')
{
idDest[j] = string[i];
i++;
j++;
}
idDest[j] = '\0';
i = i + 3;
j = 0;
while (string[i] != '"')
{
rel[j] = string[i];
i++;
j++;
}
rel[j] = '\0';
origNode = hashSearch(idOrig[0] % HASH_DIMENSION, idOrig);
if (origNode != NULL)
{
destNode = hashSearch(idDest[0] % HASH_DIMENSION, idDest);
if (destNode != NULL)
{
ListaRelPtr relNode;
relNode = listaRelSearch(rel);
if (relNode != NULL)
{
listaInterInsert(destNode, &relNode->contatori, origNode, relNode->idRel);
}
else
{
relNode = listaRelInsert(rel);
listaInterCrea(destNode, &relNode->contatori, origNode, relNode->idRel);
}
}
}
}
// Delete a relationship between two entities
void delrel(char string[])
{
unsigned int i = 8, j = 0;
char idOrig[NAME];
char idDest[NAME];
char rel[NAME];
TreeEntPtr origNode;
TreeEntPtr destNode;
while (string[i] != '"')
{
idOrig[j] = string[i];
i++;
j++;
}
idOrig[j] = '\0';
i = i + 3;
j = 0;
while (string[i] != '"')
{
idDest[j] = string[i];
i++;
j++;
}
idDest[j] = '\0';
i = i + 3;
j = 0;
while (string[i] != '"')
{
rel[j] = string[i];
i++;
j++;
}
rel[j] = '\0';
origNode = hashSearch(idOrig[0] % HASH_DIMENSION, idOrig);
if (origNode != NULL)
{
destNode = hashSearch(idDest[0] % HASH_DIMENSION, idDest);
if (destNode != NULL)
{
ListaRelPtr relNode;
relNode = listaRelSearch(rel);
if (relNode != NULL)
{
// Check if a relationship between two entities already exists. If so, delete it
listaInterDelete(destNode, origNode, relNode);
}
}
}
}
void report(void)
{
if (relRoot == NULL)
{
printf("none");
}
else
{
ListaRelPtr relNode;
ListaContPtr contNode;
relNode = relRoot;
while (relNode != NULL)
{
fputs("\"", stdout);
fputs(relNode->relazione, stdout);
fputs("\" ", stdout);
contNode = relNode->contatori;
fputs("\"", stdout);
fputs((contNode->destNode)->idEnt, stdout);
;
fputs("\" ", stdout);
while (contNode->nextPtr != NULL)
{
if ((contNode->nextPtr)->contatore_rel == contNode->contatore_rel)
{
contNode = contNode->nextPtr;
fputs("\"", stdout);
fputs((contNode->destNode)->idEnt, stdout);
fputs("\" ", stdout);
}
else
{
break;
}
}
if (relNode->nextPtr != NULL)
{
printf("%d; ", contNode->contatore_rel);
}
else
{
printf("%d;", contNode->contatore_rel);
}
relNode = relNode->nextPtr;
}
}
printf("\n");
}
void hashInsert(char idEnt[])
{
short i;
TreeEntPtr node;
node = hashSearch(idEnt[0] % HASH_DIMENSION, idEnt);
if (node == NULL)
{
unsigned int key = cont;
cont++;
if (Hash[idEnt[0] % HASH_DIMENSION] == NULL)
{
TreeEntPtr newNode;
newNode = malloc(sizeof(TreeEnt)); // Create the node to insert
if (newNode != NULL)
{ // If the new node as been created...
newNode->id = key;
strcpy(newNode->idEnt, idEnt);
for (i = 0; i < HASH_DIMENSION; i++)
{
newNode->interazioniDest[i] = NULL;
newNode->interazioniOrig[i] = NULL;
}
newNode->pPtr = NULL;
newNode->rightPtr = NULL;
newNode->leftPtr = NULL;
Hash[idEnt[0] % HASH_DIMENSION] = newNode;
}
}
else
{
treeEntInsert(Hash[idEnt[0] % HASH_DIMENSION], key, idEnt);
}
}
}
void hashDelete(TreeEntPtr entNode)
{
unsigned int init;
init = (entNode->idEnt[0]) % HASH_DIMENSION;
if (entNode != NULL)
{
if ((Hash[init])->pPtr == NULL && (Hash[init])->leftPtr == NULL && (Hash[init])->rightPtr == NULL)
{
free(Hash[init]);
Hash[init] = NULL;
}
else
{
treeEntDelete(&Hash[init], entNode);
}
}
}
// Return searched entity's node giving the name
TreeEntPtr hashSearch(unsigned int key, char idEnt[])
{
if (Hash[key] == NULL)
{
return NULL;
}
else
{
return (treeEntSearch(Hash[key], idEnt));
}
}
// Insert a new node in the tree ordering it by name
void treeEntInsert(TreeEntPtr EntRoot, unsigned short key, char nome[])
{
TreeEntPtr newNode; // Point to next node
TreeEntPtr prevNode; // Point to previous node
TreeEntPtr currNode; // Point to current node
int res;
newNode = malloc(sizeof(TreeEnt)); // Create the node to insert
if (newNode != NULL)
{ // If the new node has been created...
newNode->id = key; // Assign key to new node
strcpy(newNode->idEnt, nome); // Assign entity's name to new node
// Initialize new node's pointers
for (res = 0; res < HASH_DIMENSION; res++)
{
newNode->interazioniDest[res] = NULL;
newNode->interazioniOrig[res] = NULL;
}
newNode->pPtr = NULL;
newNode->leftPtr = NULL;
newNode->rightPtr = NULL;
prevNode = NULL; // Initialize prevNode
currNode = EntRoot; // Initialize currNode with radix address
while (currNode != NULL)
{
// Go to next node through correct tree side
prevNode = currNode;
res = strcmp(newNode->idEnt, currNode->idEnt);
if (res < 0)
{
currNode = currNode->leftPtr;
}
else
{
currNode = currNode->rightPtr;
}
}
newNode->pPtr = prevNode; // Save reference to new node's father
if (prevNode == NULL)
{ // If tree is empty...
EntRoot = newNode; //... new node becomes the new radix...
}
else
{
res = strcmp(newNode->idEnt, prevNode->idEnt);
if (res < 0)
{ // otherwise choose correct tree side
prevNode->leftPtr = newNode;
}
else
{
prevNode->rightPtr = newNode;
}
}
}
}
// Delete a node from the tree using helping functions treeSuccessor and treeMinimum
void treeEntDelete(TreeEntPtr *EntRoot, TreeEntPtr treeNode)
{
TreeEntPtr eliminaNodo;
TreeEntPtr sostiituisciNodo;
if ((treeNode->leftPtr == NULL) || (treeNode->rightPtr == NULL))
{
eliminaNodo = treeNode;
}
else
{
eliminaNodo = treeEntSuccessor(treeNode);
}
if (eliminaNodo->leftPtr != NULL)
{
sostiituisciNodo = eliminaNodo->leftPtr;
}
else
{
sostiituisciNodo = eliminaNodo->rightPtr;
}
if (sostiituisciNodo != NULL)
{
sostiituisciNodo->pPtr = eliminaNodo->pPtr;
}
if (eliminaNodo->pPtr == NULL)
{
*EntRoot = sostiituisciNodo;
}
else if (eliminaNodo == (eliminaNodo->pPtr)->leftPtr)
{
(eliminaNodo->pPtr)->leftPtr = sostiituisciNodo;
}
else
{
(eliminaNodo->pPtr)->rightPtr = sostiituisciNodo;
}
if (eliminaNodo != treeNode)
{
treeNode->id = eliminaNodo->id;
strcpy(treeNode->idEnt, eliminaNodo->idEnt);
for (int i = 0; i < HASH_DIMENSION; i++)
{
treeNode->interazioniOrig[i] = eliminaNodo->interazioniOrig[i];
treeNode->interazioniDest[i] = eliminaNodo->interazioniDest[i];
}
sistemaInterazioni(treeNode->interazioniOrig, treeNode->interazioniDest, treeNode);
}
free(eliminaNodo);
}
void sistemaInterazioni(ListaInterPtr interazioniOrig[], ListaInterPtr interazioniDest[], TreeEntPtr entNode)
{
ListaInterPtr interOrigNode;
ListaInterPtr interDestNode;
for (int i = 0; i < HASH_DIMENSION; i++)
{
// Fix otigin iterations
interOrigNode = interazioniOrig[i];
while (interOrigNode != NULL)
{
(interOrigNode->otherInter)->altraEntNode = entNode;
if (interOrigNode->dest)
{
(interOrigNode->cont)->destNode = entNode;
}
interOrigNode = interOrigNode->nextPtr;
}
// Fix destination iterations
interDestNode = interazioniDest[i];
while (interDestNode != NULL)
{
(interDestNode->otherInter)->altraEntNode = entNode;
if (interDestNode->dest)
{
(interDestNode->cont)->destNode = entNode;
}
interDestNode = interDestNode->nextPtr;
}
}
}
// Find node successor
TreeEntPtr treeEntSuccessor(TreeEntPtr treeNode)
{
TreeEntPtr yNode;
if (treeNode->rightPtr != NULL)
{
return treeEntMinimum(treeNode->rightPtr);
}
yNode = treeNode->pPtr;
while ((yNode != NULL) && (treeNode == yNode->rightPtr))
{
treeNode = yNode;
yNode = yNode->pPtr;
}
return yNode;
}
// Find minimum in the tree that have the given node as radix
TreeEnt *treeEntMinimum(TreeEntPtr treeNode)
{
while (treeNode->leftPtr != NULL)
{
treeNode = treeNode->leftPtr;
}
return treeNode;
}
// Find and return a node knowing idEnt
TreeEntPtr treeEntSearch(TreeEntPtr node, char idEnt[])
{
if (node == NULL)
{
return NULL;
}
else
{
int res;
res = strcmp(idEnt, node->idEnt);
if (res == 0)
{
return node;
}
else if (res < 0)
{
return treeEntSearch(node->leftPtr, idEnt);
}
else
{
return treeEntSearch(node->rightPtr, idEnt);
}
}
}
// Check if the relationship already exists. If it exists return 1, otherwise return 0 and add it
void listaInterInsert(TreeEntPtr destNode, ListaContPtr *contRoot, TreeEntPtr altraEnt, unsigned short rel)
{
ListaInterPtr prevPtr;
ListaInterPtr currPtr;
prevPtr = NULL;
short init = (altraEnt->idEnt[0]) % HASH_DIMENSION;
currPtr = destNode->interazioniDest[init];
while ((currPtr != NULL))
{
if ((currPtr->rel != rel) || (currPtr->dest != true) || ((currPtr->altraEntNode)->id) != (altraEnt)->id)
{
prevPtr = currPtr;
currPtr = currPtr->nextPtr;
}
else
{
break;
}
}
if (currPtr == NULL)
{
ListaInterPtr newNode;
newNode = malloc(sizeof(ListaInter));
if (newNode != NULL)
{
ListaContPtr contNode;
ListaInterPtr altraInter;
// Initialize new node
newNode->rel = rel;
newNode->dest = true;
newNode->prevPtr = NULL;
newNode->nextPtr = NULL;
newNode->altraEntNode = altraEnt;
if (prevPtr == NULL)
{
destNode->interazioniDest[init] = newNode;
}
else
{
newNode->prevPtr = prevPtr;
prevPtr->nextPtr = newNode;
}
contNode = listaContInsert(contRoot, destNode);
newNode->cont = contNode;
altraInter = listaInterAdd(&altraEnt->interazioniOrig[(destNode->idEnt[0]) % HASH_DIMENSION], newNode, contNode, destNode, rel);
newNode->otherInter = altraInter;
}
}
}
// Add an istance of the relationship
ListaInterPtr listaInterAdd(ListaInterPtr *interRoot, ListaInterPtr altraInter, ListaContPtr contNode, TreeEntPtr altraEnt, unsigned short rel)
{
ListaInterPtr newNode;
newNode = malloc(sizeof(ListaInter));
if (newNode != NULL)
{
// Initialize new node
newNode->rel = rel;
newNode->dest = false;
newNode->prevPtr = NULL;
newNode->otherInter = altraInter;
newNode->altraEntNode = altraEnt;
newNode->cont = contNode;
if ((*interRoot) != NULL)
{
newNode->nextPtr = *interRoot;
(*interRoot)->prevPtr = newNode;
}
else
{
newNode->nextPtr = NULL;
}
*interRoot = newNode;
return newNode;
}
else
{
return NULL;
}
}
void listaInterCrea(TreeEntPtr destNode, ListaContPtr *contNode, TreeEntPtr origNode, unsigned short rel)
{
ListaContPtr newCont;
newCont = malloc(sizeof(ListaCont));
if (newCont != NULL)
{
ListaInterPtr newInterDest;
newInterDest = malloc(sizeof(ListaInter));
if (newInterDest != NULL)
{
ListaInterPtr newInterOrig;
newInterOrig = malloc(sizeof(ListaInter));
if (newInterOrig != NULL)
{
// Initialize and attach counter
newCont->contatore_rel = 1;
newCont->destNode = destNode;
newCont->prevPtr = NULL;
if ((*contNode) != NULL)
{
(*contNode)->prevPtr = newCont;
newCont->nextPtr = (*contNode);
}
else
{
newCont->nextPtr = NULL;
}
*contNode = newCont;
// Initialize and attach the the relationship for addressee
short init = (origNode->idEnt[0]) % HASH_DIMENSION;
newInterDest->altraEntNode = origNode;
newInterDest->cont = newCont;
newInterDest->dest = true;
newInterDest->prevPtr = NULL;
if (destNode->interazioniDest[init] != NULL)
{
newInterDest->nextPtr = destNode->interazioniDest[init];
(destNode->interazioniDest[init])->prevPtr = newInterDest;
}
else
{
newInterDest->nextPtr = NULL;
}
newInterDest->rel = rel;
newInterDest->otherInter = newInterOrig;
destNode->interazioniDest[init] = newInterDest;
// Initialize and attach the relationship for the origin
init = (destNode->idEnt[0]) % HASH_DIMENSION;
newInterOrig->altraEntNode = destNode;
newInterOrig->cont = newCont;
newInterOrig->dest = false;
newInterOrig->prevPtr = NULL;
if (origNode->interazioniOrig[init] != NULL)
{
newInterOrig->nextPtr = origNode->interazioniOrig[init];
(origNode->interazioniOrig[init])->prevPtr = newInterOrig;
}
else
{
newInterOrig->nextPtr = NULL;
}
newInterOrig->rel = rel;
newInterOrig->otherInter = newInterDest;
origNode->interazioniOrig[init] = newInterOrig;
}
}
}
}
// Check if the relationship already exists. If it exists, delete it, decrement (or delete) the counter and delete the iteration for the other entity
void listaInterDelete(TreeEntPtr destNode, TreeEntPtr origNode, ListaRelPtr relNode)
{
ListaInterPtr prevPtr;
ListaInterPtr currPtr;
prevPtr = NULL;
currPtr = destNode->interazioniDest[(origNode->idEnt[0]) % HASH_DIMENSION];
while ((currPtr != NULL))
{
if ((currPtr->rel != relNode->idRel) || ((currPtr->altraEntNode)->id != origNode->id) || !(currPtr->dest))
{
prevPtr = currPtr;
currPtr = currPtr->nextPtr;
}
else
{
break;
}
}
if (currPtr != NULL)
{
// Delete the block
if ((prevPtr == NULL) && (currPtr->nextPtr == NULL))
{ // Radix and final NULL
destNode->interazioniDest[(origNode->idEnt[0]) % HASH_DIMENSION] = NULL;
}
else if ((prevPtr == NULL) && (currPtr->nextPtr != NULL))
{ // Radix
destNode->interazioniDest[(origNode->idEnt[0]) % HASH_DIMENSION] = currPtr->nextPtr;
(currPtr->nextPtr)->prevPtr = NULL;
}
else if ((prevPtr != NULL) && (currPtr->nextPtr == NULL))
{ // Final NULL
prevPtr->nextPtr = NULL;
}
else
{ // Center
ListaInterPtr currNextPtr;
currNextPtr = currPtr->nextPtr;
currNextPtr->prevPtr = prevPtr;
prevPtr->nextPtr = currNextPtr;
}
listaContDecrementa(&(relNode->contatori), currPtr->cont);
listaRelDelete(relNode);
altraInterDelete(&(origNode->interazioniOrig[(destNode->idEnt[0]) % HASH_DIMENSION]), currPtr->otherInter);
free(currPtr);
}
}
void altraInterDelete(ListaInterPtr *interRoot, ListaInterPtr interNode)
{
ListaInterPtr prevPtr;
prevPtr = interNode->prevPtr;
// Delete the block
if ((prevPtr == NULL) && (interNode->nextPtr == NULL))
{ // Radix and fianl NULL
*interRoot = NULL;
}
else if ((prevPtr == NULL) && (interNode->nextPtr != NULL))
{ // Radix
*interRoot = interNode->nextPtr;
(interNode->nextPtr)->prevPtr = NULL;
}
else if ((prevPtr != NULL) && (interNode->nextPtr == NULL))
{ // Final NULL
prevPtr->nextPtr = NULL;
}
else
{ // Center
ListaInterPtr currNextPtr;
currNextPtr = interNode->nextPtr;
currNextPtr->prevPtr = prevPtr;
prevPtr->nextPtr = currNextPtr;
}
free(interNode);
}
// Pass entity's relationship and delete its iterations with this entity. Each time delete relative counters.
void interDelent(TreeEntPtr entNode)
{
ListaRelPtr relNode;
ListaInterPtr interNode;
for (short i = 0; i < HASH_DIMENSION; i++)
{
interNode = entNode->interazioniDest[i];
// Pass destination entity's iterations and call relative helping functions
while (interNode != NULL)
{
relNode = relRoot;
while (relNode != NULL)
{
if (relNode->idRel != interNode->rel)
{
relNode = relNode->nextPtr;
}
else
{
break;
}
}
short init = (entNode->idEnt[0]) % HASH_DIMENSION;
if (relNode != NULL)
{
if (!(relNode->delent))
{
listaContSnap(&(relNode->contatori), interNode->cont);
bool del;
del = listaRelDelete(relNode);
if (!del)
{
relNode->delent = true;
}
}
ListaInterPtr tmpPtr;
if (((interNode->altraEntNode)->interazioniOrig[init] == NULL) || (((interNode->altraEntNode)->interazioniOrig[init])->nextPtr == NULL))
{
(interNode->altraEntNode)->interazioniOrig[init] = NULL;
}
else
{
tmpPtr = altraEntInterDelete(interNode->otherInter);
if (tmpPtr != NULL)
{
(interNode->altraEntNode)->interazioniOrig[init] = tmpPtr;
}
}
tmpPtr = interNode;
interNode = interNode->nextPtr;
free(tmpPtr);
}
else
{
ListaInterPtr tmpPtr;
if (((interNode->altraEntNode)->interazioniOrig[(entNode->idEnt[0]) % HASH_DIMENSION] == NULL) || (((interNode->altraEntNode)->interazioniOrig[(entNode->idEnt[0]) % HASH_DIMENSION])->nextPtr == NULL))
{
(interNode->altraEntNode)->interazioniOrig[(entNode->idEnt[0]) % HASH_DIMENSION] = NULL;
}
else
{
tmpPtr = altraEntInterDelete(interNode->otherInter);
if (tmpPtr != NULL)
{
(interNode->altraEntNode)->interazioniOrig[(entNode->idEnt[0]) % HASH_DIMENSION] = tmpPtr;
}
}
tmpPtr = interNode;
interNode = interNode->nextPtr;
free(tmpPtr);
}
}
}
for (short i = 0; i < HASH_DIMENSION; i++)
{
interNode = entNode->interazioniOrig[i];
// Pass origin entity's relationship and call relative helpimg functions
while (interNode != NULL)
{
relNode = relRoot;
while (relNode != NULL)
{
if (relNode->idRel != interNode->rel)
{
relNode = relNode->nextPtr;
}