-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTable.c
More file actions
159 lines (126 loc) · 5.07 KB
/
hashTable.c
File metadata and controls
159 lines (126 loc) · 5.07 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
/* ____________________________________________________________________________
Module hashTable.c
This module contains functions which work with hashTable struct made with
arrayLists. It can be used only by graphNode or graphEdge structs, because
they have similar structure. HashTable saves only pointers to structs.
____________________________________________________________________________
*/
#include <malloc.h>
#include <stdio.h>
#include "hashTable.h"
#include "structs.h"
/* ____________________________________________________________________________
hashTable *createHashtable(int size, int itemSize)
Creates new hashTable with which has size same as parameter size and
itemSize. HashTable is made with usage of arrayLists.
____________________________________________________________________________
*/
hashTable *createHashTable(int size, int itemSize) {
int i;
hashTable *table = malloc(sizeof(hashTable));
table->size = size;
table->itemSize = itemSize;
table->filledItems = 0;
table->array = malloc(sizeof(arrayList *) * size);
for (i = 0; i < table->size; i++) {
table->array[i] = createArrayList(1, table->itemSize);
}
return table;
}
/* ____________________________________________________________________________
int hashTableAddElement(void *element, int id, hashTable *table)
Checks if element with key id is present in the hashTable, if it is, zero
is returned and element is not added, in the other case element is added
and 1 is returned
____________________________________________________________________________
*/
int hashTableAddElement(void *element, int id, hashTable *table) {
int i;
int index;
void *hashElement;
index = ABS(id % table->size);
for (i = 0; i < table->array[index]->filledItems; i++) {
hashElement = arrayListGetPointer(table->array[index],0);
if (((graphNode *)hashElement)->id == id) return 0;
}
arrayListAdd(table->array[index], element);
table->filledItems++;
return 1;
}
/* ____________________________________________________________________________
int hashTableAddUncheckedElement(void *element, int index, hashTable *table)
Adds the element without checking if the index is unique in the hashTable,
this function is used when new edges are added into graph on index which
means the source node of the edge
____________________________________________________________________________
*/
int hashTableAddUncheckedElement(void *element, int index, hashTable *table) {
int updatedIndex;
if (!element || !table) return 0;
updatedIndex = ABS(index % table->size);
arrayListAdd(table->array[updatedIndex], element);
table->filledItems++;
return 1;
}
/* ____________________________________________________________________________
void *hashTableGetElement(int id, hashTable *table)
Returns element with specified id, or NULL if the element is not present.
____________________________________________________________________________
*/
void *hashTableGetElement(int id, hashTable *table) {
int i;
int index;
void *element;
index = ABS(id % table->size);
for (i = 0; i < table->array[index]->filledItems; i++) {
element = arrayListGetPointer(table->array[index], i);
if (id == ((graphNode *)element)->id) {
return element;
}
}
return NULL;
}
/* ____________________________________________________________________________
int hashTableContains(int id, hashTable *table)
Returns whether the table contains element with key id.
____________________________________________________________________________
*/
int hashTableContains(int id, hashTable *table) {
int i;
int index;
if (!table) return 0;
index = ABS(id % table->size);
for (i = 0; i < table->array[index]->filledItems; i++) {
if (((graphNode *)arrayListGetPointer(table->array[index],i))->id == id) {
return 1;
}
}
return 0;
}
/* ____________________________________________________________________________
void freeHashTable(hashTable **table)
Deallocates memory used by hashTable, arrayLists which it uses and elements
saved in arrayLists.
____________________________________________________________________________
*/
void freeHashTable(hashTable **table) {
int i;
int j;
graphEdge *pointer;
if (!table || !*table) return;
for (i = 0; i < (*table)->size; i++) {
if ((*table)->array[i]) {
for (j = 0; j < (*table)->array[i]->filledItems; j++) {
pointer = arrayListGetPointer((*table)->array[i], j);
if (pointer) {
if (pointer->wkt) free(pointer->wkt);
free(pointer);
}
}
freeArrayList(&(*table)->array[i]);
}
}
free((*table)->array);
free(*table);
*table = NULL;
}