2022-07-28 15:15:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
2022-07-28 16:00:52 +00:00
|
|
|
* included in all copies or portions of the Software.
|
2022-07-28 15:15:04 +00:00
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2022-07-29 16:32:52 +00:00
|
|
|
|
2022-07-23 00:19:12 +00:00
|
|
|
/*
|
|
|
|
* HashMap.h: The public interface for Telodendria's hash map
|
|
|
|
* implementation. This hash map is designed to be simple, well
|
|
|
|
* documented, and generally readable and understandable, yet also
|
|
|
|
* performant enough to be useful, because it is used extensively in
|
|
|
|
* Telodendria.
|
2022-07-25 19:25:06 +00:00
|
|
|
*
|
2022-07-23 00:19:12 +00:00
|
|
|
* Fundamentally, this is an entirely generic map implementation. It
|
|
|
|
* can be used for many general purposes, but it is designed to only
|
|
|
|
* implement the features that Telodendria needs to function well.
|
|
|
|
*/
|
|
|
|
#ifndef TELODENDRIA_HASHMAP_H
|
|
|
|
#define TELODENDRIA_HASHMAP_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HashMap: The primary data structure used by this hash map algorithm.
|
|
|
|
* All data necessary for the algorithm to function is stored inside
|
|
|
|
* it. This is an opaque structure; use the methods defined by this
|
|
|
|
* interface to manipulate it.
|
|
|
|
*/
|
|
|
|
typedef struct HashMap HashMap;
|
|
|
|
|
|
|
|
/*
|
2022-07-29 16:32:52 +00:00
|
|
|
* Create a new HashMap object.
|
2022-07-25 19:25:06 +00:00
|
|
|
*
|
2022-07-29 16:32:52 +00:00
|
|
|
* Return: A HashMap object that is ready to be used by the rest of
|
2022-07-23 00:19:12 +00:00
|
|
|
* the HashMap functions, or NULL if memory could not be allocated on
|
|
|
|
* the heap.
|
|
|
|
*/
|
|
|
|
extern HashMap *
|
2022-07-25 19:25:06 +00:00
|
|
|
HashMapCreate(void);
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2022-07-29 16:32:52 +00:00
|
|
|
/*
|
|
|
|
* Set the maximum load of the hash map before it is expanded. When the
|
|
|
|
* hash map reaches the given capacity, it is grown. You don't want
|
|
|
|
* to only grow hash maps when their full, because that makes them
|
|
|
|
* perform very poorly.
|
|
|
|
*
|
|
|
|
* The default max load on new HashMap objects is 0.75, which should be
|
|
|
|
* good enough for most purposes, but if you need finer tuning, feel
|
|
|
|
* free to play with the max load with this function. The changes take
|
|
|
|
* effect on the next insert.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
*
|
|
|
|
* (HashMap *) The HashMap to modify the maximum load for.
|
|
|
|
* (float) The new maximum load. This should be a value
|
|
|
|
* between 0 and 1, which specifies the percentange
|
|
|
|
* of "fullness" at which the HashMap will be
|
|
|
|
* expanded. If the new max load is out of bounds,
|
|
|
|
* this function does nothing.
|
|
|
|
*/
|
2022-07-23 00:19:12 +00:00
|
|
|
extern void
|
2022-07-29 16:32:52 +00:00
|
|
|
HashMapMaxLoadSet(HashMap *, float);
|
2022-07-23 00:19:12 +00:00
|
|
|
|
|
|
|
/*
|
2022-07-29 16:32:52 +00:00
|
|
|
* Set the given key in the HashMap to the given value. Note that the
|
|
|
|
* key nor the value is copied into the HashMap's own memory space;
|
|
|
|
* only pointers is stored. It is the caller's job to ensure that the
|
|
|
|
* key and value memory remains valid for the life of the HashMap, and
|
|
|
|
* are freed when they're no longer needed.
|
|
|
|
*
|
|
|
|
* Params:
|
2022-07-25 19:25:06 +00:00
|
|
|
*
|
2022-07-29 16:32:52 +00:00
|
|
|
* (HashMap *) The hash map to set a key in.
|
|
|
|
* (char *) The key to set.
|
|
|
|
* (void *) The value to set at the given key.
|
|
|
|
*
|
|
|
|
* Return: The previous value at the given key, or NULL if the key did
|
2022-07-23 00:19:12 +00:00
|
|
|
* not previously exist or any of the parameters provided are NULL. All
|
|
|
|
* keys must have values; you can't set a key to NULL. To delete a key,
|
2022-07-29 16:32:52 +00:00
|
|
|
* use HashMapDelete().
|
2022-07-23 00:19:12 +00:00
|
|
|
*/
|
|
|
|
extern void *
|
2022-07-29 16:32:52 +00:00
|
|
|
HashMapSet(HashMap *, char *, void *);
|
2022-07-23 00:19:12 +00:00
|
|
|
|
|
|
|
/*
|
2022-07-29 16:32:52 +00:00
|
|
|
* Get the value for the given key.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
*
|
|
|
|
* (HashMap *) The hash map to check.
|
|
|
|
* (char *) The key to get the value for.
|
2022-07-25 19:25:06 +00:00
|
|
|
*
|
2022-07-29 16:32:52 +00:00
|
|
|
* Return: The value at the given key, or NULL if the key does not
|
2022-07-23 00:19:12 +00:00
|
|
|
* exist, no map was provided, or no key was provided.
|
|
|
|
*/
|
|
|
|
extern void *
|
2022-07-29 16:32:52 +00:00
|
|
|
HashMapGet(HashMap *, const char *);
|
2022-07-23 00:19:12 +00:00
|
|
|
|
|
|
|
/*
|
2022-07-29 16:32:52 +00:00
|
|
|
* Delete the value for the given key.
|
2022-07-25 19:25:06 +00:00
|
|
|
*
|
2022-07-29 16:32:52 +00:00
|
|
|
* Params:
|
|
|
|
*
|
|
|
|
* (HashMap *) The map to delete the given key from.
|
|
|
|
* (const char *) The key to delete.
|
|
|
|
*
|
|
|
|
* Return: The value at the given key, or NULL if the key does not
|
2022-07-23 00:19:12 +00:00
|
|
|
* exist or the map or key was not provided.
|
|
|
|
*/
|
|
|
|
extern void *
|
2022-07-29 16:32:52 +00:00
|
|
|
HashMapDelete(HashMap *, const char *);
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2022-07-29 16:32:52 +00:00
|
|
|
/*
|
|
|
|
* Iterate over all the keys and values of a hash map. This function
|
|
|
|
* works similarly to the POSIX getopt(), where calls are repeatedly
|
|
|
|
* made in a "while" loop until there are no more items to go over.
|
|
|
|
*
|
|
|
|
* The difference is that this function does not rely on globals. This
|
|
|
|
* function takes pointer pointers, and stores necessary state inside
|
|
|
|
* the hash map structure itself.
|
|
|
|
*
|
|
|
|
* This function can be tricky to use in some scenarios, as it
|
|
|
|
* continues where it left off on each call, until there are no more
|
|
|
|
* elements. Then it returns 0 and resets the iterator, so that it can
|
|
|
|
* start over for the next iteration. This means that if you are not
|
|
|
|
* iterating over the entire map at one, and break the loop, the next
|
|
|
|
* time you try to iterate the HashMap, you'll start somewhere in the
|
|
|
|
* middle. Thus, it's recommended to iterate over the entire map. For
|
|
|
|
* scenarios in which the entire map needs to be iterated, such as
|
|
|
|
* when freeing all the keys and values, this function does well.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
*
|
|
|
|
* (HashMap *) The hash map to iterate over.
|
|
|
|
* (char **) A character pointer that will be set to the current
|
|
|
|
* key.
|
|
|
|
* (void **) A void pointer that will be set to the current value.
|
|
|
|
*
|
|
|
|
* Return: 1 if there are still elements left in this iteration of the
|
|
|
|
* hash map, or 0 if no valid hash map was provided, or there are no
|
|
|
|
* more elements in it for this iteration. Note that after this
|
|
|
|
* function returns 0 on a hash map, subsequent iterations will start
|
|
|
|
* from the beginning.
|
|
|
|
*/
|
2022-07-26 01:31:09 +00:00
|
|
|
extern int
|
2022-07-29 16:32:52 +00:00
|
|
|
HashMapIterate(HashMap *, char **, void **);
|
2022-07-23 00:19:12 +00:00
|
|
|
|
|
|
|
/*
|
2022-07-29 16:32:52 +00:00
|
|
|
* Free the hash map, returning its memory to the operating system.
|
|
|
|
* Note that this function does not free the keys or values stored in
|
2022-07-23 00:19:12 +00:00
|
|
|
* the map since this hash map implementation has no way of knowing
|
2022-07-29 16:32:52 +00:00
|
|
|
* what actually is stored in it. You should use HashMapIterate() to
|
2022-07-23 00:19:12 +00:00
|
|
|
* free the values using your own algorithm.
|
2022-07-29 16:32:52 +00:00
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
*
|
|
|
|
* (HashMap *) The hash map to free. The pointer can be safely
|
|
|
|
* discarded when this function returns. In fact,
|
|
|
|
* accessing it after this function returns is undefined
|
|
|
|
* behavior.
|
2022-07-23 00:19:12 +00:00
|
|
|
*/
|
|
|
|
extern void
|
2022-07-26 01:31:09 +00:00
|
|
|
HashMapFree(HashMap *);
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2022-07-29 16:32:52 +00:00
|
|
|
#endif
|