OAuth2Lib/util/sorted_str_set.h

70 lines
2.5 KiB
C

#ifndef _OAUTH2LIB_UTIL_SORTED_SET_
#define _OAUTH2LIB_UTIL_SORTED_SET_
#include <stddef.h>
struct sorted_str_set_pair {
char* key;
void* element;
};
typedef struct {
size_t element_size;
size_t size;
size_t capacity;
struct sorted_str_set_pair* data;
} sorted_str_set;
/**
* @name sorted_str_set_new
* @description Allocates and returns a new instance of sorted_str_set. Must be freed with @sorted_str_set_free
*
* @param element_size the size of each element (not the key)
* @return the newly allocated set or NULL if allocation fails
*/
sorted_str_set* sorted_str_set_new(size_t element_size);
/**
* @name sorted_str_set_free
* @description Frees all resources of an instance of sorted_str_set. Has to be called before the end of the lifetime of the variable.
* Does not free any memory possibly pointed to by element.
*
* @param set the set to be deallocated. Must not be NULL.
*/
void sorted_str_set_free(sorted_str_set* set);
/**
* @name sorted_str_set_free
* @description Returns a pointer to the element associated with key within set
*
* @param set the set to be accessed. Must not be NULL.
* @param key the key whose value should be retrieved. Must be null-terminated.
* @return a pointer to the element associated with key or NULL if key is not in the set
*/
void* sorted_str_set_get(const sorted_str_set* set, const char* key);
/**
* @name sorted_str_set_insert
* @description Inserts a new element associated with key into set and returns a pointer to the element.
* If key is already present in set, nothing is modified and a pointer to the existing element is returned.
*
* @param set the set to be accessed. Must not be NULL.
* @param key the key for the element. Must not be NULL. Must be null-terminated.
* @param element the element to be inserted. A copy of element is made. Is expected to be the size of set->element_size. Must not be NULL.
* @return a pointer to the newly inserted element or if the key is already existing, the element associated with key.
*/
void* sorted_str_set_insert(sorted_str_set* set, const char* key, const void* element);
/**
* @name sorted_str_set_remove
* @description Removes the key and element associated by key from set. If the key is not present, does nothing.
*
* @param set the set to be accessed. Must not be NULL.
* @aram key the key whose element should be removed. Must not be NULL.
*/
void sorted_str_set_remove(sorted_str_set* set, const char* key);
#endif //_OAUTH2LIB_UTIL_SORTED_SET_