BeRTOS
Functions
hashtable.c File Reference

Portable hash table implementation. More...

#include "hashtable.h"
#include "cfg/cfg_hashtable.h"
#include <cfg/debug.h>
#include <cfg/compiler.h>
#include <cfg/macros.h>
#include <string.h>

Go to the source code of this file.

Functions

uint8_t * key_internal_get_ptr (struct HashTable *ht, HashNodePtr node)
 For hash tables with internal keys, compute the pointer to the internal key for a given node.
void ht_init (struct HashTable *ht)
 Initialize (and clear) a hash table in a memory buffer.
bool ht_insert_with_key (struct HashTable *ht, const void *key, uint8_t key_length, const void *data)
 Insert an element into the hash table.
bool ht_insert (struct HashTable *ht, const void *data)
 Insert an element into the hash table.
const void * ht_find (struct HashTable *ht, const void *key, uint8_t key_length)
 Find an element in the hash table.

Detailed Description

Portable hash table implementation.

Some rationales of our choices in implementation:

Author:
Giovanni Bajo <rasky@develer.com>

Definition in file hashtable.c.


Function Documentation

const void* ht_find ( struct HashTable ht,
const void *  key,
uint8_t  key_length 
)

Find an element in the hash table.

Parameters:
htHandle of the hash table
keyKey of the element
key_lengthLength of the key in characters
Returns:
Data of the element, or NULL if no element was found for the given key.

Definition at line 274 of file hashtable.c.

void ht_init ( struct HashTable ht)

Initialize (and clear) a hash table in a memory buffer.

Parameters:
htHash table declared with DECLARE_HASHTABLE
Note:
This function must be called before using the hash table. Optionally, it can be called later in the program to clear the hash table, removing all its elements.

Definition at line 203 of file hashtable.c.

bool ht_insert ( struct HashTable ht,
const void *  data 
)

Insert an element into the hash table.

Parameters:
htHandle of the hash table
dataData to be inserted into the table
Returns:
true if insertion was successful, false otherwise (table is full)
Note:
The key for the element to insert is extract from the data with the hook. This means that this function cannot be called for hashtables with internal keys.
If an element with the same key already exists in the table, it will be overwritten.
It is not allowed to store NULL in the table. If you pass NULL as data, the function call will fail.

Definition at line 254 of file hashtable.c.

bool ht_insert_with_key ( struct HashTable ht,
const void *  key,
uint8_t  key_length,
const void *  data 
)

Insert an element into the hash table.

Parameters:
htHandle of the hash table
keyKey of the element
key_lengthLength of the key in characters
dataData to be inserted into the table
Returns:
true if insertion was successful, false otherwise (table is full)
Note:
If this function is called for hash table with external keys, the key provided must be match the key that would be extracted with the hook, otherwise the function will fail.
If an element with the same key already exists in the table, it will be overwritten.
It is not allowed to store NULL in the table. If you pass NULL as data, the function call will fail.

Definition at line 235 of file hashtable.c.

uint8_t* key_internal_get_ptr ( struct HashTable ht,
HashNodePtr  node 
) [inline]

For hash tables with internal keys, compute the pointer to the internal key for a given node.

Definition at line 99 of file hashtable.c.