]> git.wh0rd.org - ICEs.git/blame - 154175/dictionary.c
add bfin ice
[ICEs.git] / 154175 / dictionary.c
CommitLineData
45516216 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.
7 */
8#include "dictionary.h"
9
10int hash(char* word);
11
12dictionary* dictionary_create(void) {
13 dictionary* dic = calloc(1, sizeof(dictionary));
14 return dic;
15}
16
17void 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;
22 AANTAL++;
23 INHOUD = (element**) realloc(INHOUD, AANTAL * sizeof(element*));
24 if(INHOUD == NULL) {
25 /* Als de realloc mislukt, zet alles dan terug zoals het
26 * was, en print een foutmelding */
27 INHOUD = backup;
28 AANTAL--;
29 printf("memory error. element not added \n");
30 }
31 else {
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;
36 }
37}
38
39void dictionary_remove(dictionary *d,char* word1) {
40 register int index;
41 int hashfunctie = hash(word1);
42 for(index = 0;(index < AANTAL) && (strcmp(((*INHOUD)[index]).keyword, word1) != 0); index++);
43 if(index < AANTAL) {
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];
48 free(INHOUD[index]);
49 INHOUD[index] = INHOUD[AANTAL -1];
50 AANTAL--;
51 printf("element freed");
52 INHOUD = realloc(INHOUD, AANTAL);
53 }
54}
55
56void dictionary_print(dictionary *d) {
57 int hashfunctie = 0;
58 int woord_index;
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);
63 }
64 hashfunctie++;
65 }
66}
67
68char* dictionary_find(dictionary *d,char* word1) {
69 int hashfunctie = hash(word1);
70 register int index;
71 for(index = 0; index < AANTAL; index++) {
72 if(strcmp((*INHOUD[index]).keyword, word1) == 0) return (((*INHOUD)[index]).alternative);
73 }
74 return NULL;
75}
76
77void dictionary_findall(dictionary *d,char* word1,char** syn) {
78 int aantal_gevonden_woorden = 0;
79 register int index;
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
87 */
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++;
95 }
96 }
97/* if(aantal_gevonden_woorden <= AANTAL) {
98 syn = realloc(syn, aantal_gevonden_woorden * sizeof(char*));
99 }*/
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.
104 */
105}
106
107void dictionary_destroy(dictionary *d) {
108 int hashfunctie = 0;
109 register int index_lijst;
110 while(hashfunctie < 26) {
111 index_lijst = 0;
112 while(index_lijst < AANTAL) {
113 free((&INHOUD)[index_lijst]);
114 index_lijst++;
115 }
116 hashfunctie++;
117 }
118 free(d);
119}
120
121int hash(char* word) {
122 return (int) ((*word) - STARTOFHASHSOURCE);
123}
124
125int main(void) {
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");
140 dictionary_print(d);
141 dictionary_destroy(d);
142 return 0;
143}