libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
hash_map.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2026 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdbool.h>
20#include <stdint.h>
21#include <stdlib.h>
22
23#include "hash_map.h"
24
25#define INITIAL_SIZE 1024
26#define LOAD_FACTOR 0.75
27
50{
51 // Enforce minimum size to prevent division by zero in modulo operations
52 if(size < INITIAL_SIZE) size = INITIAL_SIZE;
53
54 hash_map_t *map = malloc(sizeof(hash_map_t));
55 map->table = calloc(size, sizeof(kv_pair_t));
56 map->size = size;
57 map->count = 0;
58
59 return map;
60}
61
77{
78 free(map->table);
79 free(map);
80}
81
104static void resize_map(hash_map_t *map, size_t new_size)
105{
106 kv_pair_t *old_table = map->table;
107 size_t old_size = map->size;
108
109 map->table = calloc(new_size, sizeof(kv_pair_t));
110 map->size = new_size;
111 map->count = 0;
112
113 for(size_t i = 0; i < old_size; i++)
114 if(old_table[i].key != 0)
115 {
116 // Re-insert
117 size_t idx = old_table[i].key % new_size;
118
119 while(map->table[idx].key != 0) idx = (idx + 1) % new_size;
120
121 map->table[idx] = old_table[i];
122 map->count++;
123 }
124
125 free(old_table);
126}
127
156bool insert_map(hash_map_t *map, uint64_t key, uint64_t value)
157{
158 if((double)map->count / map->size > LOAD_FACTOR) resize_map(map, map->size * 2);
159
160 size_t idx = key % map->size;
161
162 while(map->table[idx].key != 0 && map->table[idx].key != key) idx = (idx + 1) % map->size;
163
164 if(map->table[idx].key == key) return false; // Already present
165
166 map->table[idx].key = key;
167 map->table[idx].value = value;
168 map->count++;
169
170 return true;
171}
172
199bool lookup_map(const hash_map_t *map, uint64_t key, uint64_t *out_value)
200{
201 size_t idx = key % map->size;
202
203 while(map->table[idx].key != 0)
204 {
205 if(map->table[idx].key == key)
206 {
207 *out_value = map->table[idx].value;
208 return true;
209 }
210
211 idx = (idx + 1) % map->size;
212 }
213
214 return false;
215}
bool lookup_map(const hash_map_t *map, uint64_t key, uint64_t *out_value)
Looks up a value by key in the hash map.
Definition hash_map.c:199
#define INITIAL_SIZE
Definition hash_map.c:25
hash_map_t * create_map(size_t size)
Creates a new hash map with the specified initial size.
Definition hash_map.c:49
bool insert_map(hash_map_t *map, uint64_t key, uint64_t value)
Inserts a key-value pair into the hash map.
Definition hash_map.c:156
#define LOAD_FACTOR
Definition hash_map.c:26
void free_map(hash_map_t *map)
Frees all memory associated with a hash map.
Definition hash_map.c:76
static void resize_map(hash_map_t *map, size_t new_size)
Resizes the hash map to a new size and rehashes all entries.
Definition hash_map.c:104
Minimal open-addressing hash map for 64-bit key/value pairs used in deduplication lookup.
Definition hash_map.h:50
size_t count
Number of active (filled) entries.
Definition hash_map.h:53
size_t size
Allocated slot capacity of table.
Definition hash_map.h:52
kv_pair_t * table
Array of key/value slots of length == size.
Definition hash_map.h:51
Single key/value slot used internally by the open-addressing hash map.
Definition hash_map.h:33
uint64_t value
Associated value payload (64-bit) stored alongside the key.
Definition hash_map.h:35
uint64_t key
Stored key (64-bit). May use a reserved sentinel to denote an empty slot.
Definition hash_map.h:34