|
| 1 | +// |
| 2 | +// Created by Seppe Degryse on 15/04/2025. |
| 3 | +// |
| 4 | +#include "hash_table.h" |
| 5 | + |
| 6 | +#include <assert.h> |
| 7 | + |
| 8 | + |
| 9 | +uint32_t hash(const uint32_t key, const size_t table_size) |
| 10 | +{ |
| 11 | + return key % table_size; |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +hash_table_t create_table(const size_t num_elements) |
| 16 | +{ |
| 17 | + const size_t size = num_elements * 2; // double for lower load factor |
| 18 | + hash_table_t ht; |
| 19 | + ht.size = size; |
| 20 | + |
| 21 | + // Allocate array of buckets |
| 22 | + ht.table = (bucket_t *) calloc(size, sizeof(bucket_t)); |
| 23 | + if (ht.table == NULL) { |
| 24 | + fprintf(stderr, "Failed to allocate hash table\n"); |
| 25 | + exit(EXIT_FAILURE); |
| 26 | + } |
| 27 | + |
| 28 | + return ht; |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +void free_table(const hash_table_t *ht) |
| 33 | +{ |
| 34 | + for (size_t i = 0; i < ht->size; i++) { |
| 35 | + free(ht->table[i].tuples); |
| 36 | + } |
| 37 | + free(ht->table); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +bool bucket_insert(bucket_t *bucket, triple_t *el) { |
| 42 | + if (bucket->count == bucket->capacity) { |
| 43 | + size_t new_capacity = bucket->capacity == 0 ? 4 : bucket->capacity * 2; |
| 44 | + triple_t **new_tuples = realloc(bucket->tuples, new_capacity * sizeof(triple_t *)); |
| 45 | + if (new_tuples == NULL) return false; |
| 46 | + |
| 47 | + bucket->tuples = new_tuples; |
| 48 | + bucket->capacity = new_capacity; |
| 49 | + } |
| 50 | + |
| 51 | + bucket->tuples[bucket->count++] = el; |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +bool triple_offset_equal(const uint32_t offset, triple_t *el, uint32_t key) |
| 57 | +{ |
| 58 | + return *((uint32_t *) el + offset) == key; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +bool insert(hash_table_t *ht, const uint32_t offset, triple_t *el) { |
| 63 | + uint32_t key = *((uint32_t *)el + offset); |
| 64 | + size_t idx = hash(key, ht->size); |
| 65 | + |
| 66 | + for (size_t i = 0; i < ht->size; i++) { |
| 67 | + size_t probe_idx = (idx + i) % ht->size; |
| 68 | + bucket_t *bucket = &ht->table[probe_idx]; |
| 69 | + |
| 70 | + // Empty bucket: initialize it |
| 71 | + if (bucket->tuples == NULL) { |
| 72 | + bucket->tuples = malloc(4 * sizeof(triple_t *)); |
| 73 | + if (bucket->tuples == NULL) return false; |
| 74 | + bucket->capacity = 4; |
| 75 | + bucket->count = 0; |
| 76 | + } |
| 77 | + |
| 78 | + // Check if the key in this bucket matches or it's the first insert |
| 79 | + if (bucket->count == 0 || triple_offset_equal(offset, bucket->tuples[0], key)) { |
| 80 | + return bucket_insert(bucket, el); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return false; // Table full |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +bucket_t *contains(const hash_table_t *ht, const uint32_t offset_in1, const uint32_t offset_in2, triple_t *el) { |
| 89 | + uint32_t key = *((uint32_t *)el + offset_in2); |
| 90 | + size_t idx = hash(key, ht->size); |
| 91 | + |
| 92 | + for (size_t i = 0; i < ht->size; i++) { |
| 93 | + size_t probe_idx = (idx + i) % ht->size; |
| 94 | + bucket_t *bucket = &ht->table[probe_idx]; |
| 95 | + |
| 96 | + if (bucket->tuples == NULL) |
| 97 | + return NULL; |
| 98 | + |
| 99 | + if (bucket->count > 0 && triple_offset_equal(offset_in1, bucket->tuples[0], key)) |
| 100 | + return bucket; |
| 101 | + } |
| 102 | + |
| 103 | + return NULL; |
| 104 | +} |
0 commit comments