-
Notifications
You must be signed in to change notification settings - Fork 0
/
carlist.h
196 lines (166 loc) · 4.95 KB
/
carlist.h
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
#ifndef PLATES_HEADERS
#define PLATES_HEADERS
#include <inttypes.h> // for portable integer declarations
#include <stdbool.h> // for bool type
#include <stdio.h> // for printf()
#include <stdlib.h> // for malloc(), free(), NULL
#include <string.h> // for strcmp()
#include <pthread.h>
#include <sys/time.h>
#define PLATE_LENGTH 6
#endif
// An car inserted into a hash table.
// As hash collisions can occur, multiple cars can exist in one bucket.
// Therefore, each bucket is a linked list of cars that hashes to that bucket.
typedef struct car car_t;
struct car
{
char plate[PLATE_LENGTH];
int current_level;
bool in_carpark;
struct timeval entry_time;
car_t *next;
};
// A hash table mapping a string to an integer.
typedef struct htab htab_t;
struct htab
{
car_t **buckets;
size_t size;
};
// HASHTABLE FUNCTIONS
//Inserts plates from plates.txt into hashtable
//Preconditions: The hashtable has been initialised
//Postconditions: the hashtable has been filled with plates from the plates.txt file
bool htab_insert_plates(htab_t *h);
//Initialise hashtable
//Preconditions: None
//Postconditions: Initialises the hashtable in memory
bool htab_init(htab_t *h, size_t n);
//Calculate the offset for the bucket for key in hash table.
//Preconditions: The hashtable has been initialised
//Postconditions: The plate in the hashtable has been indexed
size_t htab_index(htab_t *h, char *key);
//Find pointer to head of list for key in hash table.
//Preconditions: The hashtable has been initialised
//Postconditions: returns the pointer to the head of the link list with given plate.
car_t *htab_bucket(htab_t *h, char *key);
//hashes each entry into hashtable
//Preconditions: The hashtable has been initialised
//Postconditions: hashes the plate in the hashtable
size_t djb_hash(char *c);
//Adds a character array into hashtable and hashes it
//Preconditions: The hashtable has been initialised
//Postconditions: The hashtable is updated with the plate
bool htab_add(htab_t *h,char *plate);
//Searches the hashtable for the plate given
//Preconditions: The hashtable has been initialised
//Postconditions: true if plate is in hashtable, false if plate is not in hashtable
bool htab_search_plate(htab_t *h, char *search);
//Finds the corrosponding key to access a plate in the hashtable
//Preconditions: The hashtable has been initialised
//Postconditions: The key to a plate in the hashtable
car_t *htab_find(htab_t *h, char *key);
//Frees the memory taken by the hashtable
//Preconditions: The hashtable has been initialised
//Postconditions: The memory has been freed
void htab_destroy(htab_t *h);
bool htab_init(htab_t *h, size_t n)
{
h->size = n;
h->buckets = calloc(n, sizeof(car_t*));
// array of pointers, therefore no matter how large table grows, same amount of memory
// calloc guarantess all that memory is initialised to zero
return h->buckets != NULL;
}
bool htab_insert_plates(htab_t *h)
{
FILE* input_file = fopen("plates.txt", "r");
if (input_file == NULL)
{
fprintf(stderr, "Error: failed to open file %s", "plates.txt");
exit(1);
}
char scan[7];
while( fscanf(input_file, "%s", scan) != EOF )
{
char plate[6];
string2charr(scan,plate);
htab_add(h, plate);
}
fclose(input_file);
return true;
}
bool htab_add(htab_t *h, char *plate)
{
size_t index = htab_index(h, plate);
car_t *new_car = malloc(sizeof(car_t));
if (new_car == NULL){
printf("Failed to allocate memory to new car\n");
return false;
}
string2charr(plate, new_car->plate);
new_car->in_carpark = false;
new_car->next = h->buckets[index];
h->buckets[index] = new_car;
return true;
}
size_t djb_hash(char *s)
{
size_t hash = 5381;
for(int i = 0; i < PLATE_LENGTH; i++)
{
int c = (int)s[i];
hash = ((hash << 5) + hash) + c;
}
return hash;
}
size_t htab_index(htab_t *h, char *key)
{
return djb_hash(key) % h->size;
}
car_t *htab_bucket(htab_t *h, char *key)
{
return h->buckets[htab_index(h, key)];
}
car_t *htab_find(htab_t *h, char *key)
{
for (car_t *i = htab_bucket(h, key); i != NULL; i = i->next)
{
if (strncmp(i->plate, key, 6) == 0)
{ // found the key
return i;
}
}
return NULL;
}
bool htab_search_plate(htab_t *h, char *search)
{
bool retVal = false;
for (size_t i = 0; i < h->size; ++i)
{
for (car_t *bucket = h->buckets[i]; bucket != NULL; bucket = bucket->next) {
if (strncmp(bucket->plate, search, 6) == 0)
{
retVal = true;
}
}
}
return retVal;
}
void htab_destroy(htab_t *h)
{
for (size_t i = 0; i < h->size; ++i)
{
car_t *bucket = h->buckets[i];
while (bucket != NULL)
{
car_t *next = bucket->next;
free(bucket);
bucket = next;
}
}
free(h->buckets);
h->buckets = NULL;
h->size = 0;
}