-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTEP_LinkedHashMap.c
More file actions
274 lines (230 loc) · 6.18 KB
/
Copy pathTEP_LinkedHashMap.c
File metadata and controls
274 lines (230 loc) · 6.18 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
//Topicos especiais em programacao - DCC - UFRJ
//Assignment 5 - linked hash map
//Allan Monteiro David
//My own linked hash map.
//A hash map used when you want to have a fixed sized hash map.
//When it's full and you want to add another element, it will remove the
//less recently accessed element from the hash map.
#include <stdio.h>
#include <stdlib.h>
int LINKEDHASHMAP_SIZE = 0;
int LINKEDHASHMAP_MAXNUMOFELEMENTS = 10;
float LINKEDHASHMAP_LOADFACTOR = 2;
struct HashElement
{
struct HashElement* nextInHash;
struct HashElement* previousInHash;
struct HashElement* nextInOrder;
struct HashElement* previousInOrder;
int key;
int info;
};
struct LinkedHashMap
{
struct HashElement** list;
int numberOfElements;
struct HashElement* firstInOrder; //more recently accessed
struct HashElement* lastInOrder; //less recently accessed
} linkedHashMap;
void initLinkedHashMap()
{
LINKEDHASHMAP_SIZE = (int) LINKEDHASHMAP_MAXNUMOFELEMENTS / LINKEDHASHMAP_LOADFACTOR;
linkedHashMap.list = (struct HashElement**) malloc (sizeof(struct HashElement*) * LINKEDHASHMAP_SIZE);
linkedHashMap.numberOfElements = 0;
linkedHashMap.firstInOrder = NULL;
linkedHashMap.lastInOrder = NULL;
}
int hashFunction(int key)
{
return key % LINKEDHASHMAP_SIZE;
}
struct HashElement* findInHash(int key)
{
int hashedKey = hashFunction(key);
if(linkedHashMap.list[hashedKey] == NULL)
{
printf("FindInHash: There was no element with key: %d!\n", key);
return NULL;
}
else
{
struct HashElement* actualHashElement = linkedHashMap.list[hashedKey];
while(actualHashElement->key != key)
{
if(actualHashElement->nextInHash == NULL)
{
printf("FindInHash: There was no element with key: %d!\n", key);
return NULL;
}
else
{
actualHashElement = actualHashElement->nextInHash;
}
}
return actualHashElement;
}
}
void putElementAsFirstInOrder(struct HashElement* element)
{
if(linkedHashMap.firstInOrder == element)
return;
if(linkedHashMap.lastInOrder == NULL)
linkedHashMap.lastInOrder = element;
if(linkedHashMap.firstInOrder != NULL)
linkedHashMap.firstInOrder->nextInOrder = element;
if(element->previousInOrder != NULL)
element->previousInOrder->nextInOrder = element->nextInOrder;
if(element->nextInOrder != NULL)
element->nextInOrder->previousInOrder = element->previousInOrder;
element->nextInOrder = NULL;
element->previousInOrder = linkedHashMap.firstInOrder;
linkedHashMap.firstInOrder = element;
}
void removeElementInOrder(struct HashElement* element)
{
if(element->previousInOrder != NULL)
element->previousInOrder->nextInOrder = element->nextInOrder;
if(element->nextInOrder != NULL)
element->nextInOrder->previousInOrder = element->previousInOrder;
if(linkedHashMap.firstInOrder == element)
{
linkedHashMap.firstInOrder = element->previousInOrder;
linkedHashMap.firstInOrder->nextInOrder = NULL;
}
if(linkedHashMap.lastInOrder == element)
{
linkedHashMap.lastInOrder = element->nextInOrder;
linkedHashMap.lastInOrder->previousInOrder = NULL;
}
}
void deleteInHash(int key)
{
struct HashElement* auxElement = findInHash(key);
if(auxElement == NULL)
{
printf("DeleteInHash: did nothing.\n");
}
else
{
removeElementInOrder(auxElement);
int hashedKey = hashFunction(key);
if(linkedHashMap.list[hashedKey] == auxElement)
linkedHashMap.list[hashedKey] = auxElement->nextInHash;
if(auxElement->previousInHash != NULL)
auxElement->previousInHash->nextInHash = auxElement->nextInHash;
if(auxElement->nextInHash != NULL)
auxElement->nextInHash->previousInHash = auxElement->previousInHash;
free(auxElement);
auxElement = NULL;
linkedHashMap.numberOfElements -= 1;
}
}
void removeLastElementInOrder()
{
if(linkedHashMap.lastInOrder != NULL)
{
deleteInHash(linkedHashMap.lastInOrder->key);
}
}
void insertInHash(int key, int info)
{
struct HashElement* newHashElement = (struct HashElement*) malloc(sizeof(struct HashElement));
newHashElement->info = info;
newHashElement->key = key;
int hashedKey = hashFunction(key);
if(linkedHashMap.list[hashedKey] == NULL)
{
linkedHashMap.numberOfElements += 1;
if(linkedHashMap.numberOfElements > LINKEDHASHMAP_MAXNUMOFELEMENTS)
{
removeLastElementInOrder();
}
linkedHashMap.list[hashedKey] = newHashElement;
putElementAsFirstInOrder(newHashElement);
}
else
{
linkedHashMap.numberOfElements += 1;
if(linkedHashMap.numberOfElements > LINKEDHASHMAP_MAXNUMOFELEMENTS)
{
removeLastElementInOrder();
}
struct HashElement* actualHashElement = linkedHashMap.list[hashedKey];
while(actualHashElement->nextInHash != NULL)
{
actualHashElement = actualHashElement->nextInHash;
}
actualHashElement->nextInHash = newHashElement;
newHashElement->previousInHash = actualHashElement;
putElementAsFirstInOrder(newHashElement);
}
}
int accessInHash(int key)
{
struct HashElement* auxElement = findInHash(key);
if(auxElement == NULL)
{
printf("AccessInHash: returned zero.\n");
return 0;
}
else
{
putElementAsFirstInOrder(auxElement);
return auxElement->info;
}
}
void printOrder()
{
printf("=======ORDER======\n");
struct HashElement* auxElement = linkedHashMap.lastInOrder;
while(auxElement != NULL)
{
printf("-> %d", auxElement->info);
auxElement = auxElement->nextInOrder;
}
printf("\n=======ORDER======\n");
}
void printReverseOrder()
{
printf("=======REVERSEORDER======\n");
struct HashElement* auxElement = linkedHashMap.firstInOrder;
while(auxElement != NULL)
{
printf("-> %d", auxElement->info);
auxElement = auxElement->previousInOrder;
}
printf("\n=======REVERSEORDER======\n");
}
void printHashMap()
{
printf("=======HASH======\n");
int i = 0;
struct HashElement* pointer = NULL;
for(i = 0; i < LINKEDHASHMAP_SIZE; i++)
{
pointer = linkedHashMap.list[i];
printf("line %d: ", i);
while(pointer != NULL)
{
printf("%d ", pointer->key);
pointer = pointer->nextInHash;
}
printf("\n");
}
printf("=======HASH======\n");
}
main()
{
initLinkedHashMap();
//test of behaviour
int i = 0;
for(i = 0; i < 13; i++)
{
insertInHash(i, i*10);
printf("Num of elements: %d\n", linkedHashMap.numberOfElements);
printOrder();
printReverseOrder();
printHashMap();
printf("\n");
}
}