Loading...
Searching...
No Matches
hash_map.c File Reference
`#include <stdbool.h>`
`#include <stdint.h>`
`#include <stdlib.h>`
`#include "hash_map.h"`

Go to the source code of this file.

## Macros

#define | INITIAL_SIZE 1024
#define | LOAD_FACTOR 0.75
#define | LARGE_MAP_THRESHOLD (2ULL * 1024 * 1024 * 1024 / sizeof(kv_pair_t))
#define | LARGE_GROWTH_FACTOR 1.25

## Functions

hash_map_t * | create_map (size_t size)
| Creates a new hash map with the specified initial size.
void | free_map (hash_map_t *map)
| Frees all memory associated with a hash map.
static size_t | calculate_new_size (size_t current_size)
| Calculates the new size for hash map resizing.
static void | resize_map (hash_map_t *map, size_t new_size)
| Resizes the hash map to a new size and rehashes all entries.
bool | insert_map (hash_map_t *map, uint64_t key, uint64_t value)
| Inserts a key-value pair into the hash map.
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.

## Macro Definition Documentation

## INITIAL_SIZE

#define INITIAL_SIZE 1024

Definition at line 25 of file hash_map.c.

Referenced by create_map().

## LARGE_GROWTH_FACTOR

#define LARGE_GROWTH_FACTOR 1.25

Definition at line 28 of file hash_map.c.

Referenced by calculate_new_size().

## LARGE_MAP_THRESHOLD

#define LARGE_MAP_THRESHOLD (2ULL * 1024 * 1024 * 1024 / sizeof(kv_pair_t))

Definition at line 27 of file hash_map.c.

Referenced by calculate_new_size().

## LOAD_FACTOR

#define LOAD_FACTOR 0.75

Definition at line 26 of file hash_map.c.

Referenced by insert_map().

## Function Documentation

## calculate_new_size()

size_t calculate_new_size | ( | size_t | current_size | ) |

static

Calculates the new size for hash map resizing.

This function implements intelligent size calculation based on the current map size. For small to medium maps (below ~2GB), it doubles the size for fast growth. For large maps (at or above ~2GB), it uses a smaller growth factor (25%) to avoid excessive memory allocation.

Parameters

  current_size | The current size of the hash table.

Returns
  The calculated new size for the hash table.

Note
  For maps below LARGE_MAP_THRESHOLD, size doubles (2x growth).
  For maps at or above LARGE_MAP_THRESHOLD, size grows by 25% (1.25x growth).
  This is a static (internal) function and should not be called directly.

See also
  resize_map()
  insert_map()

Definition at line 103 of file hash_map.c.

References LARGE_GROWTH_FACTOR, and LARGE_MAP_THRESHOLD.

Referenced by insert_map().

## create_map()

hash_map_t * create_map | ( | size_t | size | ) |

Creates a new hash map with the specified initial size.

Allocates and initializes a new hash map structure with the given size. The hash map uses open addressing with linear probing for collision resolution. The table is zero-initialized, making empty slots identifiable by having a key value of 0.

Parameters

  size | Initial size of the hash table. Must be greater than 0.

Returns
  Returns a pointer to the newly created hash map, or NULL if allocation fails.

Return values

  hash_map_t*

  Successfully created hash map with:
  - Allocated and zero-initialized table of specified size
  - Size set to the requested value
  - Count initialized to 0 (empty map)

  NULL | Memory allocation failed

Note
  The caller is responsible for freeing the returned hash map using free_map().
  A key value of 0 is reserved to indicate empty slots and cannot be used as a valid key.

See also
  free_map()

Definition at line 51 of file hash_map.c.

References hash_map_t::count, INITIAL_SIZE, hash_map_t::size, and hash_map_t::table.

Referenced by aaruf_create(), and aaruf_open().

## free_map()

void free_map | ( | hash_map_t * | map | ) |

Frees all memory associated with a hash map.

Deallocates the hash table and the hash map structure itself. After calling this function, the hash map pointer becomes invalid and should not be used.

Parameters

  map | Pointer to the hash map to free. Can be NULL (no operation performed).

Note
  This function does not free any memory pointed to by the values stored in the map. If the values are dynamically allocated, they must be freed separately before calling this function.

See also
  create_map()

Definition at line 78 of file hash_map.c.

References hash_map_t::table.

Referenced by aaruf_close(), and cleanup_failed_create().

## insert_map()

bool insert_map | ( | hash_map_t * | map,
|  | uint64_t | key,
|  | uint64_t | value )

Inserts a key-value pair into the hash map.

Adds a new key-value pair to the hash map using open addressing with linear probing for collision resolution. If the key already exists, the insertion fails and returns false. The function automatically resizes the hash table when the load factor exceeds the threshold (0.75) to maintain optimal performance.

Parameters

  map | Pointer to the hash map. Must not be NULL.
  key | The key to insert. Must not be 0 as this value is reserved for empty slots.
  value | The value to associate with the key.

Returns
  Returns the result of the insertion operation.

Return values

  true | Successfully inserted the key-value pair. The map count is incremented.
  false | Key already exists in the map. No changes made to the map.

Note
  If insertion would exceed the load factor threshold, the hash table is automatically resized to twice its current size before insertion.
  Time complexity: O(1) average case, O(n) worst case with poor hash distribution.
  Space complexity: O(1) unless resizing occurs, in which case it's O(n).

Warning
  Using 0 as a key value will result in undefined behavior as 0 is reserved for marking empty slots.
  If memory allocation fails during automatic resizing, the program may terminate.

See also
  lookup_map()
  resize_map()

Definition at line 185 of file hash_map.c.

References calculate_new_size(), hash_map_t::count, kv_pair_t::key, LOAD_FACTOR, resize_map(), hash_map_t::size, hash_map_t::table, and kv_pair_t::value.

Referenced by aaruf_write_sector().

## lookup_map()

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.

Searches for the specified key in the hash map and retrieves its associated value. Uses linear probing to handle collisions during the search. The function does not modify the hash map in any way.

Parameters

  map | Pointer to the hash map to search. Must not be NULL.
  key | The key to search for. Must not be 0.
  out_value | Pointer to store the found value. Must not be NULL. Only modified if the key is found.

Returns
  Returns whether the key was found in the map.

Return values

  true | Key found. The associated value is written to *out_value.
  false | Key not found. *out_value is not modified.

Note
  Time complexity: O(1) average case, O(n) worst case with poor hash distribution or high load factor.
  The function is read-only and does not modify the hash map structure.
  Searching for key value 0 will always return false as 0 indicates empty slots.

Warning
  The out_value parameter must point to valid memory location. Passing NULL will result in undefined behavior.

See also
  insert_map()

Definition at line 228 of file hash_map.c.

References kv_pair_t::key, hash_map_t::size, hash_map_t::table, and kv_pair_t::value.

Referenced by aaruf_write_sector().

## resize_map()

void resize_map | ( | hash_map_t * | map,
|  | size_t | new_size )

static

Resizes the hash map to a new size and rehashes all entries.

This is an internal function that creates a new hash table with the specified size, rehashes all existing key-value pairs from the old table, and replaces the old table with the new one. This operation is automatically triggered when the load factor exceeds the threshold during insertion.

Parameters

  map | Pointer to the hash map to resize. Must not be NULL.
  new_size | New size for the hash table. Should be larger than the current size for optimal performance.

Note
  This is a static (internal) function and should not be called directly.
  The function preserves all existing key-value pairs during the resize operation.
  After resizing, the physical positions of entries in the table will change, but the logical key-value mappings remain intact.
  The old table is automatically freed after successful migration.

Warning
  If memory allocation for the new table fails, the program may terminate.

See also
  insert_map()
  calculate_new_size()

Definition at line 133 of file hash_map.c.

References hash_map_t::count, kv_pair_t::key, hash_map_t::size, and hash_map_t::table.

Referenced by insert_map().