Skip to content
Snippets Groups Projects
Verified Commit 02abe1ac authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added hm

parent 12241484
No related branches found
No related tags found
No related merge requests found
CC:=clang
CFLAGS:=-Wall -Wextra -pedantic -g -fsanitize=address,undefined
LDFLAGS:=-fsanitize=address,undefined
main: main.o hm.o
$(CC) main.o hm.o -o main $(LDFLAGS)
main.o: main.c hm.h
$(CC) -c main.c -o main.o $(CFLAGS)
hm.o: hm.c hm.h
$(CC) -c hm.c -o hm.o $(CFLAGS)
clean:
rm -f *.o main
\ No newline at end of file
#include <string.h>
#include "hm.h"
static size_t hash(char *key, size_t table_capacity) {
size_t h = 0;
for (size_t i = 0; i < strlen(key); ++i) {
h = (h + key[i] * 43) % table_capacity;
}
return h;
}
void hm_init(hm_t *hm, size_t table_capacity) {
hm->table = malloc(sizeof(*(hm->table)) * table_capacity);
hm->table_capacity = table_capacity;
hm->table_length = 0;
for (size_t i = 0; i < hm->table_capacity; ++i) {
hm->table[i].state = EMPTY;
}
}
void hm_destroy(hm_t *hm) {
free(hm->table);
hm->table = NULL;
hm->table_capacity = 0;
hm->table_length = 0;
}
void hm_insert(hm_t *hm, char *key, char *value) {
if (hm->table_capacity == hm->table_length) {
return;
}
size_t index = hash(key, hm->table_capacity);
while(hm->table[index].state == OCCUPIED && (0 != strcmp(key, hm->table[index].key))) {
index = (index + 1) % hm->table_capacity;
}
if (hm->table[index].state != OCCUPIED) {
hm->table_length += 1;
strcpy(hm->table[index].key, key);
hm->table[index].state = OCCUPIED;
}
strcpy(hm->table[index].value, value);
}
void hm_remove(hm_t *hm, char *key) {
if (0 == hm->table_length) {
return;
}
size_t index = hash(key, hm->table_capacity);
while (hm->table[index].state == EMPTY &&
(0 != strcmp(key, hm->table[index].key))) {
index = (index + 1) % hm->table_capacity;
}
if (0 == strcmp(key, hm->table[index].key)) {
hm->table[index].state = DELETED;
hm->table[index].key[0] = '\0';
hm->table_length -= 1;
}
}
#ifndef HM_H
#define HM_H
#include <stdlib.h>
#define MAX_SIZE 80
typedef enum _state_t { EMPTY, OCCUPIED, DELETED } state_t;
typedef struct _entry_t {
char key[MAX_SIZE];
char value[MAX_SIZE];
state_t state;
} entry_t;
typedef struct _hm_t {
entry_t *table;
size_t table_capacity;
size_t table_length;
} hm_t;
void hm_init(hm_t *hm, size_t table_capacity);
void hm_destroy(hm_t *hm);
void hm_insert(hm_t *hm, char *key, char *value);
void hm_remove(hm_t *hm, char *key);
#endif
#include <stdlib.h>
#include <stdio.h>
#include "hm.h"
int main(int argc, char *argv[]) {
hm_t hm;
hm_init(&hm, 50);
printf("capacity = %zu, length = %zu\n", hm.table_capacity, hm.table_length);
char key1[] = "Orestis";
char value1[] = "1234567";
hm_insert(&hm, key1, value1);
hm_remove(&hm, key1);
char value2[] = "2345678";
hm_insert(&hm, key1, value2);
for (int i = 0; i < 50; ++i) {
if (hm.table[i].state == OCCUPIED) {
printf("key: %s, value %s\n", hm.table[i].key, hm.table[i].value);
}
}
for (int i = 0; i < 50; ++i) {
if (hm.table[i].state == OCCUPIED) {
printf("key: %s, value %s\n", hm.table[i].key, hm.table[i].value);
}
}
printf("capacity = %zu, length = %zu\n", hm.table_capacity, hm.table_length);
hm_destroy(&hm);
printf("capacity = %zu, length = %zu\n", hm.table_capacity, hm.table_length);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment