]>
git.wh0rd.org - ICEs.git/blob - 154175/dictionary.c
1 #define STARTOFHASHSOURCE 97
2 #define AANTAL ((*d).aantal[hashfunctie])
3 #define INHOUD ((*d).inhoud[hashfunctie])
4 /* Ik definieer AANTAL hier als het aantal keywords, beginnend met de
5 * eerste letter van het opgegeven woord.
6 * INHOUD verwijst naar de tabel met pointers die de <K, V> elementen bevat.
8 #include "dictionary.h"
12 dictionary
* dictionary_create(void) {
13 dictionary
* dic
= calloc(1, sizeof(dictionary
));
17 void dictionary_add(dictionary
*d
,char* word1
,char* word2
) {
18 int hashfunctie
= hash(word1
);
19 /* vanaf hier zijn de INHOUD en AANTAL macro's gedefinieerd; ze
20 * worden heel veel gebruikt om de leesbaarheid te vergroten */
21 element
** backup
= INHOUD
;
23 INHOUD
= (element
**) realloc(INHOUD
, AANTAL
* sizeof(element
*));
25 /* Als de realloc mislukt, zet alles dan terug zoals het
26 * was, en print een foutmelding */
29 printf("memory error. element not added \n");
32 INHOUD
[AANTAL
- 1] = (element
*) calloc(1, sizeof(element
));
33 printf("element %s stored at %p \n", word1
, INHOUD
[AANTAL
-1]);
34 (*INHOUD
)[AANTAL
-1].keyword
= word1
;
35 (*INHOUD
)[AANTAL
-1].alternative
= word2
;
39 void dictionary_remove(dictionary
*d
,char* word1
) {
41 int hashfunctie
= hash(word1
);
42 for(index
= 0;(index
< AANTAL
) && (strcmp(((*INHOUD
)[index
]).keyword
, word1
) != 0); index
++);
44 /* verwissel het gevonden element met het laatste, en
45 * laat realloc de tabel met 1 verkleinen */
46 printf("deleting element at %p \n", INHOUD
[index
]);
47 INHOUD
[index
] = (element
*) INHOUD
[index
];
49 INHOUD
[index
] = INHOUD
[AANTAL
-1];
51 printf("element freed");
52 INHOUD
= realloc(INHOUD
, AANTAL
);
56 void dictionary_print(dictionary
*d
) {
59 while(hashfunctie
!= 26) {
60 for(woord_index
=0; woord_index
< AANTAL
; woord_index
++) {
61 printf("Keyword: %s \n", ((*INHOUD
)[woord_index
]).keyword
);
62 printf("Synoniem: %s \n", ((*INHOUD
)[woord_index
]).alternative
);
68 char* dictionary_find(dictionary
*d
,char* word1
) {
69 int hashfunctie
= hash(word1
);
71 for(index
= 0; index
< AANTAL
; index
++) {
72 if(strcmp((*INHOUD
[index
]).keyword
, word1
) == 0) return (((*INHOUD
)[index
]).alternative
);
77 void dictionary_findall(dictionary
*d
,char* word1
,char** syn
) {
78 int aantal_gevonden_woorden
= 0;
80 int hashfunctie
= hash(word1
);
81 // syn = realloc(syn, AANTAL * sizeof(char*));
82 /* Dit vraagt misschien wat uitleg:
83 * De syn pointer wijst naar de tabel met strings. Er moet dus zo'n tabel gedeclareerd / geinitialiseerd worden.
84 * Hiervoor is geheugen nodig. We weten niet op voorhand hoeveel, maar beter te veel dan te weinig uiteraard.
85 * Er kunnen maximum evenveel synoniemen gevonden worden als dat er keywords met dezelfde letter in de tabel zitten.
86 * We vragen dus een blok om AL die alternatives in te kunnen opslaan, de geheugenruimte die niet nodig is geven we op het einde weer vrij
88 for(index
= 0; index
< AANTAL
; index
++) {
89 // printf("now looking at: %x \n", INHOUD[index]);
90 if(strcmp((*INHOUD
)[index
].keyword
, word1
) == 0) {
91 // printf("adding %s \n", ((*INHOUD)[index].alternative));
92 syn
[aantal_gevonden_woorden
] = ((*INHOUD
)[index
]).alternative
;
93 // printf("%d : %d \n", syn[aantal_gevonden_woorden], aantal_gevonden_woorden);
94 aantal_gevonden_woorden
++;
97 /* if(aantal_gevonden_woorden <= AANTAL) {
98 syn = realloc(syn, aantal_gevonden_woorden * sizeof(char*));
100 /* Dit vraagt wederom om wat uitleg:
101 * We hebben een blok geheugen gealloceerd om de maximaal mogelijke hoeveelheid strings in op te slaan.
102 * We hebben ondertussen gevonden hoeveel strings er zijn, die steken al in de array.
103 * Nu moet de overbodige ruimte terug vrijgegeven worden.
107 void dictionary_destroy(dictionary
*d
) {
109 register int index_lijst
;
110 while(hashfunctie
< 26) {
112 while(index_lijst
< AANTAL
) {
113 free((&INHOUD
)[index_lijst
]);
121 int hash(char* word
) {
122 return (int) ((*word
) - STARTOFHASHSOURCE
);
126 dictionary
* d
= dictionary_create();
127 char** syn
= malloc(2 * sizeof(char*));
128 int hashfunctie
= hash("test");
129 dictionary_add(d
, "test", "tettn");
130 dictionary_add(d
, "test2", "ooktettn");
131 dictionary_add(d
, "test", "nogmeertettn");
132 dictionary_add(d
, "vierendelen", "doodgaan");
133 dictionary_add(d
, "proberen", "test");
134 printf("%s \n", dictionary_find(d
, "test"));
135 dictionary_findall(d
, "test", syn
);
136 printf("findall woord1: %s \n", syn
[0]);
137 printf("findall woord2: %s \n", syn
[1]);
138 printf("removing memory ... \n");
139 dictionary_remove(d
, "test2");
141 dictionary_destroy(d
);