initial version of hashmap works

This commit is contained in:
2024-11-09 20:43:12 +01:00
parent 51a309e55f
commit 5e5d25cd75
9 changed files with 51 additions and 28 deletions

View File

@@ -1,9 +1,24 @@
#include "hashmap.h"
int hash(int sockfd) { return sockfd % HASHMAP_MAX_ITEMS; }
int hash(int sockfd) { return sockfd % HASHMAP_MAX_CLIENTS; }
void hm_init(struct hashmap *map) {
memset(map, 0, sizeof(struct hashmap) * HASHMAP_MAX_CLIENTS);
for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) {
map[i].sockfd = -1;
}
}
void hm_insert(struct hashmap *map, int sockfd, struct sockaddr_storage *sas) {
int index = hash(sockfd);
map[index].sockfd = sockfd;
map[index].sas = *sas;
// I should check the collisions
}
struct hashmap *hm_lookup(struct hashmap *map, int sockfd) {
int index = hash(sockfd);
return &map[index];
}