Kohi Game Engine
hashtable.h File Reference

This file contains the hashtable implementation. More...

#include "defines.h"

Go to the source code of this file.

Data Structures

struct  hashtable
 Represents a simple hashtable. Members of this structure should not be modified outside the functions associated with it. More...
 

Typedefs

typedef struct hashtable hashtable
 Represents a simple hashtable. Members of this structure should not be modified outside the functions associated with it. More...
 

Functions

KAPI void hashtable_create (u64 element_size, u32 element_count, void *memory, b8 is_pointer_type, hashtable *out_hashtable)
 Creates a hashtable and stores it in out_hashtable. More...
 
KAPI void hashtable_destroy (hashtable *table)
 Destroys the provided hashtable. Does not release memory for pointer types. More...
 
KAPI b8 hashtable_set (hashtable *table, const char *name, void *value)
 Stores a copy of the data in value in the provided hashtable. Only use for tables which were NOT created with is_pointer_type = true. More...
 
KAPI b8 hashtable_set_ptr (hashtable *table, const char *name, void **value)
 Stores a pointer as provided in value in the hashtable. Only use for tables which were created with is_pointer_type = true. More...
 
KAPI b8 hashtable_get (hashtable *table, const char *name, void *out_value)
 Obtains a copy of data present in the hashtable. Only use for tables which were NOT created with is_pointer_type = true. More...
 
KAPI b8 hashtable_get_ptr (hashtable *table, const char *name, void **out_value)
 Obtains a pointer to data present in the hashtable. Only use for tables which were created with is_pointer_type = true. More...
 
KAPI b8 hashtable_fill (hashtable *table, void *value)
 Fills all entries in the hashtable with the given value. Useful when non-existent names should return some default value. Should not be used with pointer table types. More...
 

Detailed Description

This file contains the hashtable implementation.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)
Version
1.0
Date
2022-01-10

Typedef Documentation

◆ hashtable

typedef struct hashtable hashtable

Represents a simple hashtable. Members of this structure should not be modified outside the functions associated with it.

For non-pointer types, table retains a copy of the value.For pointer types, make sure to use the _ptr setter and getter. Table does not take ownership of pointers or associated memory allocations, and should be managed externally.

Function Documentation

◆ hashtable_create()

KAPI void hashtable_create ( u64  element_size,
u32  element_count,
void *  memory,
b8  is_pointer_type,
hashtable out_hashtable 
)

Creates a hashtable and stores it in out_hashtable.

Parameters
element_sizeThe size of each element in bytes.
element_countThe maximum number of elements. Cannot be resized.
memoryA pointer to hold a block of memory to be used. Internally allocated. Will be equal in size to element_size * element_count.
is_pointer_typeIndicates if this hashtable will hold pointer types.
out_hashtableA pointer to a hashtable in which to hold relevant data.

◆ hashtable_destroy()

KAPI void hashtable_destroy ( hashtable table)

Destroys the provided hashtable. Does not release memory for pointer types.

Parameters
tableA pointer to the table to be destroyed.

◆ hashtable_fill()

KAPI b8 hashtable_fill ( hashtable table,
void *  value 
)

Fills all entries in the hashtable with the given value. Useful when non-existent names should return some default value. Should not be used with pointer table types.

Parameters
tableA pointer to the table filled. Required.
valueThe value to be filled with. Required.
Returns
True if successful; otherwise false.

◆ hashtable_get()

KAPI b8 hashtable_get ( hashtable table,
const char *  name,
void *  out_value 
)

Obtains a copy of data present in the hashtable. Only use for tables which were NOT created with is_pointer_type = true.

Parameters
tableA pointer to the table to retrieved from. Required.
nameThe name of the entry to retrieved. Required.
valueA pointer to store the retrieved value. Required.
Returns
True; or false if a null pointer is passed.

◆ hashtable_get_ptr()

KAPI b8 hashtable_get_ptr ( hashtable table,
const char *  name,
void **  out_value 
)

Obtains a pointer to data present in the hashtable. Only use for tables which were created with is_pointer_type = true.

Parameters
tableA pointer to the table to retrieved from. Required.
nameThe name of the entry to retrieved. Required.
valueA pointer to store the retrieved value. Required.
Returns
True if retrieved successfully; false if a null pointer is passed or is the retrieved value is 0.

◆ hashtable_set()

KAPI b8 hashtable_set ( hashtable table,
const char *  name,
void *  value 
)

Stores a copy of the data in value in the provided hashtable. Only use for tables which were NOT created with is_pointer_type = true.

Parameters
tableA pointer to the table to get from. Required.
nameThe name of the entry to set. Required.
valueThe value to be set. Required.
Returns
True, or false if a null pointer is passed.

◆ hashtable_set_ptr()

KAPI b8 hashtable_set_ptr ( hashtable table,
const char *  name,
void **  value 
)

Stores a pointer as provided in value in the hashtable. Only use for tables which were created with is_pointer_type = true.

Parameters
tableA pointer to the table to get from. Required.
nameThe name of the entry to set. Required.
valueA pointer value to be set. Can pass 0 to 'unset' an entry.
Returns
True; or false if a null pointer is passed or if the entry is 0.