https://github.com/barrust/hashmap
https://github.com/barrust/hashmap
c hash-map hash-table hashmap hashtable map
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/barrust/hashmap
- Owner: barrust
- License: mit
- Created: 2015-09-17T02:19:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-06-07T14:13:51.000Z (over 4 years ago)
- Last Synced: 2025-03-31T11:02:13.745Z (11 months ago)
- Topics: c, hash-map, hash-table, hashmap, hashtable, map
- Language: C
- Size: 82 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# hashmap
[](https://opensource.org/licenses/MIT)
[](https://github.com/barrust/hashmap/releases)
[](https://github.com/barrust/hashmap/actions)
[](https://codecov.io/gh/barrust/hashmap)
Hashmap implementation written in **C**
Hashmaps are a key value store with quick look up times. The hashmap_stats()
function will provide a summary of the current lookup times including average,
worst case key not found, and worst case key found.
This hashmap implementation is a simple and, generally, quick method to include
a hashmap in C programs. It was developed to provide a basis for testing and
benchmarking performance along with providing a purposeful, low overhead
library.
To use the library, copy the `src/hashmap.h` and `src/hashmap.c` files into your
project, compile and link the library, and include it where needed.
**NOTE:** The key is of type `char*` as in a `c-string`
## License
MIT 2016
## Usage:
``` c
#include "hashmap.h"
HashMap h;
hashmap_init(&h);
hashmap_set_string(&h, "google", "search engine, android, web ads");
hashmap_set_string(&h, "facebook", "social media site");
hashmap_set_string(&h, "twitter", "the sound of little birds");
char* tmp = (char*)hashmap_get(&h, "google");
if (tmp == NULL) {
printf("'google' was not in the hashmap\n");
} else {
printf("key: test\tvalue: %s\n", tmp);
hashmap_set_string(&h, "google", "search engine, android, web ads, and automobiles");
}
hashmap_stats(&h);
hashmap_destroy(&h);
```
## Thread safety
Due to the the overhead of enforcing thread safety, it is up to the user to
ensure that each thread has controlled access to the hashmap. For **OpenMP**
code, the following should suffice.
``` c
#include "set.h"
#include
int main(int argc, char** argv) {
HashMap h;
hashmap_init(&h);
#pragma omp parallel for
for (int i = 0; i < 500000; i++) {
char key[KEY_LEN] = {0};
sprintf(key, "%d", i);
#pragma omp critical (hashmap_set_lock)
{
hashmap_add_int(&h, key, i);
}
}
hashmap_destroy(&h);
}
```
Guards must be used when inserting and removing elements as the layout of the
nodes may change. When there are only retrievals, `hashmap_get`, then there is
no need for guards. If the retrievals are simultaneous to the insertions and
deletions then guards must be placed around `hashmap_get` to ensure that the
node location doesn't change.
## Required Compile Flags:
None
### Future Enhancements:
* Allow for sorting from the `hashmap_keys` function
* Prove if relaying out nodes needs to do more than 1 loop