-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
# Hash | ||
|
||
C pointer hash. | ||
|
||
## Example | ||
|
||
```c | ||
hash_t *hash = hash_new(); | ||
hash_set(hash, "name", "tobi"); | ||
hash_set(hash, "species", "ferret"); | ||
hash_set(hash, "age", "2"); | ||
|
||
hash_each(hash, { | ||
printf("%s: %s\n", key, (char *) val); | ||
}); | ||
|
||
hash_free(hash); | ||
``` | ||
yields: | ||
``` | ||
species: ferret | ||
age: 2 | ||
name: tobi | ||
``` | ||
## API | ||
```c | ||
/* | ||
* Hash type. | ||
*/ | ||
typedef khash_t(ptr) hash_t; | ||
/* | ||
* Allocate a new hash. | ||
*/ | ||
#define hash_new() kh_init(ptr) | ||
/* | ||
* Destroy the hash. | ||
*/ | ||
#define hash_free(self) kh_destroy(ptr, self) | ||
/* | ||
* Hash size. | ||
*/ | ||
#define hash_size kh_size | ||
/* | ||
* Iterate hash keys and ptrs, populating | ||
* `key` and `val`. | ||
*/ | ||
#define hash_each(self, block); | ||
/* | ||
* Iterate hash keys, populating `key`. | ||
*/ | ||
#define hash_each_key(self, block); | ||
/* | ||
* Iterate hash ptrs, populating `val`. | ||
*/ | ||
#define hash_each_val(self, block); | ||
// protos | ||
void | ||
hash_set(hash_t *self, char *key, void *val); | ||
void * | ||
hash_get(hash_t *self, char *key); | ||
int | ||
hash_has(hash_t *self, char *key); | ||
void | ||
hash_remove(hash_t *self, char *key); | ||
```` |