https://github.com/barrust/set
Simple Set implementation in C
https://github.com/barrust/set
c set
Last synced: about 1 year ago
JSON representation
Simple Set implementation in C
- Host: GitHub
- URL: https://github.com/barrust/set
- Owner: barrust
- License: mit
- Created: 2016-05-06T14:46:11.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2021-06-07T14:24:19.000Z (about 5 years ago)
- Last Synced: 2025-04-19T01:31:45.929Z (about 1 year ago)
- Topics: c, set
- Language: C
- Homepage:
- Size: 60.5 KB
- Stars: 70
- Watchers: 5
- Forks: 22
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# set
[](https://opensource.org/licenses/MIT)
[](https://github.com/barrust/set/releases)
[](https://github.com/barrust/set/actions)
[](https://codecov.io/gh/barrust/set)
A simple set implementation in **C**
Sets allow for quick checks for inclusion and exclusion
This implementation provides a simple and generally quick method to get set functionality into a C program quickly. It was developed to provide a basis for testing and benchmarking performance along with providing a purposeful, low overhead library. Currently only supports strings.
To use the library, copy the `src/set.h` and `src/set.c` files into your project and include it where needed.
## License
MIT 2016
# Main Features
* Union, intersection, difference, and semantic difference
* Standard and Strict subset and superset checks
* Simple method to change the hashing function if desired
* Add, check, and remove elements in a the set
## Future Enhancements
* In place union - add to an already created Set
* Print statistics about the set
## Usage:
``` c
#include "set.h"
#include
int main(int argc, char** argv) {
SimpleSet set;
set_init(&set);
set_add(&set, "orange");
set_add(&set, "blue");
set_add(&set, "red");
set_add(&set, "green");
set_add(&set, "yellow");
if (set_contains(&set, "yellow") == SET_TRUE) {
printf("Set contains 'yellow'!\n");
} else {
printf("Set does not contains 'yellow'!\n");
}
if (set_contains(&set, "purple") == SET_TRUE) {
printf("Set contains 'purple'!\n");
} else {
printf("Set does not contains 'purple'!\n");
}
set_destroy(&set);
}
```
## 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 set. For **OpenMP** code,
the following should suffice.
``` c
#include "set.h"
#include
int main(int argc, char** argv) {
SimpleSet set;
set_init(&set);
int i;
#pragma omp parallel for private(i)
for (i = 0; i < 500000; i++) {
char key[KEY_LEN] = {0};
sprintf(key, "%d", i);
#pragma omp critical (set_lock)
{
set_add(&set, key);
}
}
set_destroy(&set);
}
```
All but `set_contains` needs to be guarded against race conditions as the set
will grow as needed. Set comparison functions (union, intersect, etc.) should
be done on non-changing sets.
## Required Compile Flags:
None