This repository has been archived on 2026-04-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
bhlib/include/BH/Hashmap.h

225 lines
4.9 KiB
C
Raw Normal View History

2025-01-18 17:24:36 +03:00
#ifndef BH_HASHMAP_H
#define BH_HASHMAP_H
#include "Common.h"
2025-01-18 17:24:36 +03:00
typedef struct BH_Hashmap BH_Hashmap;
2025-01-18 17:24:36 +03:00
/**
* Creates the new hashmap with specified \a equal and \a hash callbacks.
2025-01-18 17:24:36 +03:00
*
* \param equal Comparision function
* \param hash Key hash function
*
* \return On success, returns hashmap pointer.
2025-01-18 17:24:36 +03:00
* \return On failure, returns a null pointer.
*/
BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal,
BH_HashCallback hash);
2025-01-18 17:24:36 +03:00
/**
* Destroys the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
*/
void BH_HashmapFree(BH_Hashmap *hashmap);
2025-01-18 17:24:36 +03:00
/**
* Clears the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
*/
void BH_HashmapClear(BH_Hashmap *hashmap);
2025-01-18 17:24:36 +03:00
/**
* Reserves space for \a size amount of elements in the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* This function can both expand and shrink the available space in hashmap.
* This function takes into account current hashmap load factor.
*
* \param hashmap Hahsmap pointer
2025-01-18 17:24:36 +03:00
* \param size Capacity
*
* \note Calling this function will invalidate iterators.
* \note Actual hashmap capacity can be bigger then requested.
*
* \return On success, returns zero.
2025-01-18 17:24:36 +03:00
* \return On failure, returns error code.
*/
int BH_HashmapReserve(BH_Hashmap *hashmap,
size_t size);
2025-01-18 17:24:36 +03:00
/**
* Inserts the pair \a key \a value pair into the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
* \param key Key
* \param value Value
*
* \note This function allows duplicates to be inserted.
* \note Calling this function will invalidate iterators.
*
* \return On success, returns zero.
2025-01-18 17:24:36 +03:00
* \return On failure, returns error code.
*/
int BH_HashmapInsert(BH_Hashmap *hashmap,
void *key,
void *value);
2025-01-18 17:24:36 +03:00
/**
* Removes value from the \a hashmap by \a key.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
* \param key Key
*
* \note Calling this function will invalidate iterators.
* \note If hashmap contains several elements with the same key, this function
* will remove only one key-value pair.
*/
void BH_HashmapRemove(BH_Hashmap *hashmap,
void *key);
2025-01-18 17:24:36 +03:00
/**
* Returns \a value from the \a hashmap by \a key.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
* \param key Key
* \param value Value (optional)
*
* \return On success, returns zero.
2025-01-18 17:24:36 +03:00
* \return On failure, returns error code.
*/
int BH_HashmapAt(BH_Hashmap *hashmap,
void *key,
void **value);
2025-01-18 17:24:36 +03:00
/**
* Checks if the \a hashmap is empty.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
*
* \return If hashmap is empty, returns non-zero value
* \return If hashmap is not empty, returns zero
2025-01-18 17:24:36 +03:00
*/
int BH_HashmapEmpty(BH_Hashmap *hashmap);
2025-01-18 17:24:36 +03:00
/**
* Returns the size of the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
*
* \return Returns the size of the hashmap.
*/
size_t BH_HashmapSize(BH_Hashmap *hashmap);
2025-01-18 17:24:36 +03:00
/**
* Returns the capacity of the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
*
* \return Returns the capacity of the hashmap.
*/
size_t BH_HashmapCapacity(BH_Hashmap *hashmap);
2025-01-18 17:24:36 +03:00
/**
* Returns the load factor of the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
*
* \return Returns the load factor of the hashmap.
*/
float BH_HashmapFactor(BH_Hashmap *hashmap);
2025-01-18 17:24:36 +03:00
/**
* Sets the load \a factor of the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
* \param factor Load factor
*
* \note New load factor will be applied on the next reserve/insert operation.
*/
void BH_HashmapSetFactor(BH_Hashmap *hashmap,
float factor);
2025-01-18 17:24:36 +03:00
/**
* Returns the iterator to the element in the \a hashmap by \a key.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
* \param iter Iterator
*
* \return On success, returns iterator value.
* \return On failure, returns NULL pointer.
*
* \note If hashmap contains several elements with the same key, this function
* will return iterator to one of them
*/
void *BH_HashmapIterAt(BH_Hashmap *hashmap,
void *key);
2025-01-18 17:24:36 +03:00
/**
* Returns next element \a iter in the \a hashmap.
2025-01-18 17:24:36 +03:00
*
* If \a iter is NULL pointer, then this function will return iter to the
2025-01-18 17:24:36 +03:00
* first element of the hashmap.
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
* \param iter Iterator
*
* \return On success, returns new iterator value for the next element.
* \return On failure, returns NULL pointer.
*/
void *BH_HashmapIterNext(BH_Hashmap *hashmap,
void *iter);
2025-01-18 17:24:36 +03:00
/**
* Removes key-value from the \a hashmap pointed by \a iter.
2025-01-18 17:24:36 +03:00
*
* \param hashmap Hashmap pointer
2025-01-18 17:24:36 +03:00
* \param iter Iterator
*
* \note Calling this function will invalidate iterators.
*/
void BH_HashmapIterRemove(BH_Hashmap *hashmap,
void *iter);
2025-01-18 17:24:36 +03:00
/**
* Returns key, pointed by the \a iter.
2025-01-18 17:24:36 +03:00
*
* \param iter Iterator
*
* \return Returns key.
*/
void *BH_HashmapIterKey(void *iter);
2025-01-18 17:24:36 +03:00
/**
* Returns value, pointed by the \a iter.
2025-01-18 17:24:36 +03:00
*
* \param iter Iterator
*
* \return Returns value.
*/
void *BH_HashmapIterValue(void *iter);
2025-01-18 17:24:36 +03:00
#endif /* BH_HASHMAP_H */