2024-03-29 23:34:26 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2024-03-30 04:39:02 +01:00
|
|
|
#include <wchar.h>
|
2024-03-29 23:34:26 +01:00
|
|
|
|
2024-03-30 04:39:02 +01:00
|
|
|
#include "icons.h"
|
|
|
|
#include "util.h"
|
2024-03-29 23:34:26 +01:00
|
|
|
|
|
|
|
icon *hash_table[TABLE_SIZE];
|
|
|
|
|
|
|
|
/* Hashes every name with: name and TABLE_SIZE */
|
|
|
|
unsigned int hash(char *name)
|
|
|
|
{
|
|
|
|
int length = strnlen(name, MAX_NAME), i = 0;
|
|
|
|
unsigned int hash_value = 0;
|
|
|
|
|
|
|
|
for (; i < length; i++) {
|
|
|
|
hash_value += name[i];
|
|
|
|
hash_value = (hash_value * name[i]) % TABLE_SIZE;
|
|
|
|
}
|
|
|
|
|
2024-03-30 04:39:02 +01:00
|
|
|
printf("Name: %s | Hash Value: %d\n", name, hash_value);
|
2024-03-29 23:34:26 +01:00
|
|
|
return hash_value;
|
|
|
|
}
|
|
|
|
|
2024-03-30 04:39:02 +01:00
|
|
|
void hashtable_init()
|
2024-03-29 23:34:26 +01:00
|
|
|
{
|
2024-03-30 04:39:02 +01:00
|
|
|
for (int i = 0; i < TABLE_SIZE; i++)
|
2024-03-29 23:34:26 +01:00
|
|
|
hash_table[i] = NULL;
|
2024-03-30 04:39:02 +01:00
|
|
|
|
|
|
|
icon *c = memalloc(sizeof(icon));
|
|
|
|
strcpy(c->name, "c");
|
|
|
|
c->icon = L"";
|
|
|
|
|
|
|
|
icon *h = memalloc(sizeof(icon));
|
|
|
|
strcpy(h->name, "h");
|
|
|
|
h->icon = L"";
|
|
|
|
|
|
|
|
icon *cpp = memalloc(sizeof(icon));
|
|
|
|
strcpy(cpp->name, "cpp");
|
|
|
|
cpp->icon = L"";
|
|
|
|
|
|
|
|
icon *hpp = memalloc(sizeof(icon));
|
|
|
|
strcpy(hpp->name, "hpp");
|
|
|
|
hpp->icon = L"";
|
|
|
|
|
|
|
|
icon *md = memalloc(sizeof(icon));
|
|
|
|
strcpy(md->name, "md");
|
|
|
|
md->icon = L"";
|
|
|
|
|
|
|
|
hashtable_add(c);
|
|
|
|
hashtable_add(h);
|
|
|
|
hashtable_add(cpp);
|
|
|
|
hashtable_add(hpp);
|
|
|
|
hashtable_add(md);
|
2024-03-29 23:34:26 +01:00
|
|
|
}
|
|
|
|
|
2024-03-30 04:39:02 +01:00
|
|
|
void hashtable_print()
|
2024-03-29 23:34:26 +01:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (; i < TABLE_SIZE; i++) {
|
|
|
|
if (hash_table[i] == NULL) {
|
|
|
|
printf("%i. ---\n", i);
|
|
|
|
} else {
|
2024-03-30 04:39:02 +01:00
|
|
|
printf("%i. | Name %s | Icon %ls\n", i, hash_table[i]->name, hash_table[i]->icon);
|
2024-03-29 23:34:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets hashed name and tries to store the icon struct in that place */
|
2024-03-30 04:39:02 +01:00
|
|
|
bool hashtable_add(icon *p)
|
2024-03-29 23:34:26 +01:00
|
|
|
{
|
|
|
|
if (p == NULL) return false;
|
|
|
|
|
|
|
|
int index = hash(p->name);
|
2024-03-30 04:39:02 +01:00
|
|
|
int initial_index = index;
|
|
|
|
/* linear probing until an empty slot is found */
|
|
|
|
while (hash_table[index] != NULL) {
|
|
|
|
index = (index + 1) % TABLE_SIZE; /* move to next item */
|
|
|
|
/* the hash table is full as no available index back to initial index, cannot fit new item */
|
|
|
|
if (index == initial_index) return false;
|
2024-03-29 23:34:26 +01:00
|
|
|
}
|
|
|
|
|
2024-03-30 04:39:02 +01:00
|
|
|
hash_table[index] = p;
|
|
|
|
return true;
|
2024-03-29 23:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Rehashes the name and then looks in this spot, if found returns icon */
|
2024-03-30 04:39:02 +01:00
|
|
|
icon *hashtable_search(char *name)
|
2024-03-29 23:34:26 +01:00
|
|
|
{
|
2024-03-30 04:39:02 +01:00
|
|
|
int index = hash(name);
|
|
|
|
int initial_index = index;
|
|
|
|
|
|
|
|
/* Linear probing until an empty slot or the desired item is found */
|
|
|
|
while (hash_table[index] != NULL) {
|
|
|
|
if (strncmp(hash_table[index]->name, name, MAX_NAME) == 0)
|
|
|
|
return hash_table[index];
|
|
|
|
|
|
|
|
index = (index + 1) % TABLE_SIZE; /* Move to the next slot */
|
|
|
|
/* back to same item */
|
|
|
|
if (index == initial_index) break;
|
|
|
|
}
|
|
|
|
|
2024-03-29 23:34:26 +01:00
|
|
|
return NULL;
|
|
|
|
}
|