-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiValueHashTable.c
More file actions
149 lines (129 loc) · 4.88 KB
/
MultiValueHashTable.c
File metadata and controls
149 lines (129 loc) · 4.88 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MultiValueHashTable.h"
#include "HashTable.h"
#include "KeyValuePair.h"
#include "LinkedList.h"
struct MultiValueHashTable_s {
hashTable table;
CopyFunction copyVal;
FreeFunction freeVal;
PrintFunction printVal;
PrintFunction printKey;
EqualFunction equalValue;
};
Element copyLinkedList(Element element) {
return element;
}
//Wrappers
status destroyLinkedList(Element element) {
return destroyList(element);
}
status displayLinkedList(Element element) {
LinkedList list = (LinkedList)element;
return displayList(list);
}
//The createMultiValueHashTable function initializes and allocates memory for a multi-value hash table, where each key can have multiple associated values.
//This hash table relies on a base hash table structure to store linked lists of values for each key.
MultiValueHashTable createMultiValueHashTable(CopyFunction copyKey, CopyFunction copyValue, FreeFunction freeKey, FreeFunction freeValue,
PrintFunction printKey, PrintFunction printValue, EqualFunction equalKey, EqualFunction equalValue, TransformIntoNumberFunction hashFunc, int hashSize)
{
//Input validation
if (hashSize <= 0) {
return NULL;
}
//Allocate memory for the structure
MultiValueHashTable multiValueTbl = (MultiValueHashTable)malloc(sizeof(struct MultiValueHashTable_s));
if (multiValueTbl == NULL) {
return NULL;
}
//
multiValueTbl->table = createHashTable(copyKey, freeKey, printKey, copyLinkedList, destroyLinkedList, displayLinkedList, equalKey, hashFunc, hashSize);
if (multiValueTbl->table == NULL) {
free(multiValueTbl);
return NULL;
}
multiValueTbl->copyVal = copyValue;
multiValueTbl->freeVal = freeValue;
multiValueTbl->printVal = printValue;
multiValueTbl->printKey = printKey;
multiValueTbl->equalValue = equalValue;
return multiValueTbl;
}
status destroyMultiValueHashTable(MultiValueHashTable multiValueHashTable) {
//Input validation
if (multiValueHashTable == NULL) {
return argumentFailure;
}
if (destroyHashTable(multiValueHashTable->table) != success) {
return failure;
}
free(multiValueHashTable);
return success;
}
status addToMultiValueHashTable(MultiValueHashTable multiValueHashTable, Element key, Element value) {
//Input validation
if (multiValueHashTable == NULL || key == NULL || value == NULL) {
return argumentFailure;
}
//search if there is a list of values already
LinkedList valuesList = lookupInHashTable(multiValueHashTable->table, key);
if (valuesList == NULL) {
//first element for this key
valuesList = createLinkedList(multiValueHashTable->copyVal, multiValueHashTable->freeVal, multiValueHashTable->equalValue, multiValueHashTable->equalValue, multiValueHashTable->printVal);
if (valuesList == NULL) {
return memoryFailure;
}
if (appendNode(valuesList, value) != success) {
return failure;
}
if (addToHashTable(multiValueHashTable->table, key, valuesList) != success) {
destroyList(valuesList);
return failure;
}
return success;
}
//if it's not the first value for this key , check if this value already exist
if (searchByKeyInList(valuesList, key) != NULL) {
return failure;
}
return appendNode(valuesList, value);
}
LinkedList lookupInMultiValueHashTable(MultiValueHashTable multiValueHashTable, Element key) {
//Input validation
if (multiValueHashTable == NULL || key == NULL) {
return NULL;
}
return lookupInHashTable(multiValueHashTable->table, key);
}
status removeFromMultiValueHashTable(MultiValueHashTable multiValueHashTable, Element key, Element value) {
//Input validation
if (multiValueHashTable == NULL || key == NULL || value == NULL) {
return argumentFailure;
}
LinkedList valuesList = lookupInMultiValueHashTable(multiValueHashTable, key);
if (valuesList == NULL) {
return failure;
}
if (deleteNode(valuesList, value) != success) {
return failure;
}
if (getLengthList(valuesList) == 0) {
return removeFromHashTable(multiValueHashTable->table, key);
}
return success;
}
status displayMultiValueHashTable(MultiValueHashTable multiValueHashTable, Element key) {
//Input validation
if (multiValueHashTable == NULL || key == NULL) {
return argumentFailure;
}
Element valuesList = lookupInMultiValueHashTable(multiValueHashTable, key);
if (valuesList == NULL) {
return failure;
}
multiValueHashTable->printKey(key);
displayList(valuesList);
return success;
}