hashmap.c File Reference
Implementation of the hashmap.
More...
#include "hashmap.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
Go to the source code of this file.
|
Functions |
static void | rehash (hashmap *map) |
| Doubles the size of the hashmap and re-inserts all old elements.
|
static int | insert (hashmap *map, void *data, char *key) |
| Ensures that a given element is present in the hashmap without copying the key.
|
static hashmapEntry * | find (const hashmap *map, const char *key) |
| Searches for the hashmap entry using the key and returns the first free index if the entry could not be found.
|
static int | compare (const hashmapEntry *lhs, const hashmapEntry *rhs) |
| Compares two hashmap entries lexicographically according to their keys, if they exist.
|
static unsigned long | hash1 (const char *rawKey) |
| Computes the SDBM 32-Bit hashcode. (Sleepycats Berkeley DataBase).
|
static unsigned long | hash2 (const char *rawKey) |
| Computes the FNV 32-Bit hashcode. (Fowler/Noll/Vo).
|
hashmap * | newHashmap (unsigned int hint) |
| Creates a new hashmap.
|
void | deleteHashmap (hashmap *map) |
| Destroys the hashmap without touching saved data.
|
int | hashmapSet (hashmap *map, void *data, const char *key) |
| Inserts a new element into the map or updates an existing one.
|
void * | hashmapGet (const hashmap *map, const char *key) |
| Returns the element associated with a given key.
|
void * | hashmapRemove (hashmap *map, const char *key) |
| Removes the element associated with a given key from the map and returns it.
|
void | hashmapProcess (const hashmap *map, fHashmapProc proc) |
| Processes all elements of the map in lexicographical order of their keys.
|
Detailed Description
Implementation of the hashmap.
- Author:
- Dorian Weber
Definition in file hashmap.c.
Function Documentation
static int compare |
( |
const hashmapEntry * |
lhs, |
|
|
const hashmapEntry * |
rhs | |
|
) |
| | [static] |
Compares two hashmap entries lexicographically according to their keys, if they exist.
Definition at line 198 of file hashmap.c.
void deleteHashmap |
( |
hashmap * |
map |
) |
|
Destroys the hashmap without touching saved data.
- Parameters:
-
Definition at line 234 of file hashmap.c.
static hashmapEntry * find |
( |
const hashmap * |
map, |
|
|
const char * |
key | |
|
) |
| | [static] |
Searches for the hashmap entry using the key and returns the first free index if the entry could not be found.
Definition at line 115 of file hashmap.c.
static unsigned long hash1 |
( |
const char * |
rawKey |
) |
[static] |
Computes the SDBM 32-Bit hashcode. (Sleepycats Berkeley DataBase).
Definition at line 59 of file hashmap.c.
static unsigned long hash2 |
( |
const char * |
rawKey |
) |
[static] |
Computes the FNV 32-Bit hashcode. (Fowler/Noll/Vo).
Definition at line 75 of file hashmap.c.
void* hashmapGet |
( |
const hashmap * |
map, |
|
|
const char * |
key | |
|
) |
| | |
Returns the element associated with a given key.
- Parameters:
-
[in] | map | target hashmap |
[in] | key | the key |
- Returns:
- the element associated with a
key
, if existing,
NULL
otherwise
Definition at line 257 of file hashmap.c.
void hashmapProcess |
( |
const hashmap * |
map, |
|
|
fHashmapProc |
proc | |
|
) |
| | |
Processes all elements of the map in lexicographical order of their keys.
- Precondition:
proc
is not NULL
- Parameters:
-
[in] | map | target hashmap |
[in] | proc | function that processes an item |
Definition at line 294 of file hashmap.c.
void* hashmapRemove |
( |
hashmap * |
map, |
|
|
const char * |
key | |
|
) |
| | |
Removes the element associated with a given key from the map and returns it.
- Parameters:
-
[in] | map | target hashmap |
[in] | key | the key |
- Returns:
- the element associated with a
key
, if existing,
NULL
otherwise
Definition at line 270 of file hashmap.c.
int hashmapSet |
( |
hashmap * |
map, |
|
|
void * |
data, |
|
|
const char * |
key | |
|
) |
| | |
Inserts a new element into the map or updates an existing one.
- Precondition:
key
is not NULL
or empty.
- Parameters:
-
[in] | map | target hashmap |
[in] | data | pointer to the data |
[in] | key | the key |
- Returns:
HASHMAP_ILLEGAL
, if the key is invalid (NULL
or empty)
HASHMAP_INSERT
, if the element was inserted
HASHMAP_UPDATE
, if the element got updated (overwritten)
Definition at line 251 of file hashmap.c.
static int insert |
( |
hashmap * |
map, |
|
|
void * |
data, |
|
|
char * |
key | |
|
) |
| | [static] |
Ensures that a given element is present in the hashmap without copying the key.
- Returns:
HASHMAP_INSERT
, if the hashmaps count changed
HASHMAP_UPDATE
, if the element got updated
Definition at line 163 of file hashmap.c.
hashmap* newHashmap |
( |
unsigned int |
hint |
) |
|
Creates a new hashmap.
- Note:
- The hashmap resizes itself automatically.
- Parameters:
-
[in] | hint | indicates the estimated number of entries |
- Returns:
- pointer to a hashmap
Definition at line 206 of file hashmap.c.
static void rehash |
( |
hashmap * |
map |
) |
[static] |
Doubles the size of the hashmap and re-inserts all old elements.
Definition at line 90 of file hashmap.c.