CHashTable 1
Loading...
Searching...
No Matches
hash_table_resize.c File Reference

Internal methods for creating and resizing a HashTable. More...

#include <stdlib.h>
#include "hash_table.h"
#include "hash_table_internal.h"
#include "../debugmalloc/debugmalloc.h"
Include dependency graph for hash_table_resize.c:

Functions

Entry ** create_buckets (size_t size)
 Creates a dynamically allocated bucket array with a set size.
 
HashTable * hash_table_create_with_size (size_t size)
 Creates a new dynamically allocated HashTable object with a set size.
 
void clear_buckets (Entry **buckets, size_t size)
 Frees all entries inside a bucket array.
 
size_t calc_load_threshold_count (size_t size)
 Precalculates the load threshold count.
 
void hash_table_resize (HashTable *table)
 Resizes the HashTable.
 

Detailed Description

Internal methods for creating and resizing a HashTable.

Function Documentation

◆ calc_load_threshold_count()

size_t calc_load_threshold_count ( size_t  size)

Precalculates the load threshold count.

To reduce calculations, this method can be used to precalculate the threshold count. This way the threshold is only calculated if the table size changes.

Parameters
sizeTable size
Returns
Threshold count

◆ clear_buckets()

void clear_buckets ( Entry **  buckets,
size_t  size 
)

Frees all entries inside a bucket array.

Parameters
bucketsBucket array
sizeSize of the bucket array

◆ create_buckets()

Entry ** create_buckets ( size_t  size)

Creates a dynamically allocated bucket array with a set size.

Parameters
sizeTable size
Returns
Pointer to the bucket array

◆ hash_table_create_with_size()

HashTable * hash_table_create_with_size ( size_t  size)

Creates a new dynamically allocated HashTable object with a set size.

Parameters
sizeTable size
Returns
Pointer to a dynamically allocated HashTable object

◆ hash_table_resize()

void hash_table_resize ( HashTable *  table)

Resizes the HashTable.

Steps:

  1. Calculate new bucket array size. It is the next prime after the double of the current size, found using next_prime()
  2. Create new buckets and swap out the old one
  3. Rehash all entries into new buckets using hash_table_insert()
  4. Free old bucket
Parameters
tablePointer to HashTable object