Commit 7f02707a authored by Matthias Braun's avatar Matthias Braun
Browse files

pmap: Cleanup

parent 41057476
......@@ -29,9 +29,9 @@ typedef struct pmap pmap;
/**
* A key, value pair.
*/
typedef struct pmap_entry {
const void *key; /**< The key. */
void *value; /**< The value. */
typedef struct {
void const *key; /**< The key. */
void *value; /**< The value. */
} pmap_entry;
......@@ -42,22 +42,22 @@ FIRM_API pmap *pmap_create(void);
FIRM_API pmap *pmap_create_ex(size_t slots);
/** Deletes a map. */
FIRM_API void pmap_destroy(pmap *);
FIRM_API void pmap_destroy(pmap *map);
/**
* Inserts a pair (key,value) into the map. If an entry with key
* "key" already exists, its "value" is overwritten.
*/
FIRM_API void pmap_insert(pmap *map, const void * key, void * value);
FIRM_API void pmap_insert(pmap *map, void const *key, void *value);
/** Checks if an entry with key "key" exists. */
FIRM_API int pmap_contains(pmap *map, const void * key);
FIRM_API int pmap_contains(pmap const *map, void const *key);
/** Returns the key, value pair of "key". */
FIRM_API pmap_entry *pmap_find(pmap *map, const void * key);
FIRM_API pmap_entry *pmap_find(pmap const *map, void const *key);
/** Returns the value of "key". */
FIRM_API void * pmap_get(pmap *map, const void * key);
FIRM_API void *pmap_get(pmap const *map, void const *key);
/**
* Returns the value of "key".
......@@ -67,7 +67,7 @@ FIRM_API void * pmap_get(pmap *map, const void * key);
#define pmap_get(type, map, key) ((type*)pmap_get(map, key))
/** Return number of elements in the map */
FIRM_API size_t pmap_count(pmap *map);
FIRM_API size_t pmap_count(pmap const *map);
/**
* Returns the first entry of a map if the map is not empty.
......@@ -77,7 +77,7 @@ FIRM_API pmap_entry *pmap_first(pmap *map);
/**
* Returns the next entry of a map or NULL if all entries were visited.
*/
FIRM_API pmap_entry *pmap_next(pmap *);
FIRM_API pmap_entry *pmap_next(pmap *map);
/**
* Iterate over all elements in the map setting curr to the current element.
......@@ -86,8 +86,7 @@ FIRM_API pmap_entry *pmap_next(pmap *);
for (pmap_entry *curr = pmap_first(pmap); curr; curr = pmap_next(pmap))
/** Breaks an iteration.
* Must be called, if a iteration ends before pmap_next() returns NULL.
*/
* Must be called, if a iteration ends before pmap_next() returns NULL. */
FIRM_API void pmap_break(pmap *map);
/**
......
......@@ -10,33 +10,27 @@
* @date 09.06.2002
*/
#include "pmap.h"
#include "set.h"
#include "hashptr.h"
struct pmap {
int dummy; /* dummy entry */
};
#define INITIAL_SLOTS 64
/** map a pmap into a set */
#define M2S(map) (set *)(map)
#define M2S(map) (set*)(map)
/**
* compare the keys of two entry pairs
*/
static int pmap_entry_cmp(const void *p1, const void *p2, size_t size)
static int pmap_entry_cmp(void const *p1, void const *p2, size_t size)
{
const pmap_entry *entry1 = (const pmap_entry*) p1;
const pmap_entry *entry2 = (const pmap_entry*) p2;
(void) size;
const pmap_entry *entry1 = (const pmap_entry*)p1;
const pmap_entry *entry2 = (const pmap_entry*)p2;
(void)size;
return entry1->key != entry2->key;
}
/* Creates a new empty map with an initial number of slots. */
pmap *pmap_create_ex(size_t slots)
{
return (pmap *)new_set(pmap_entry_cmp, slots);
......@@ -61,25 +55,25 @@ void pmap_insert(pmap *map, const void *key, void *value)
p->value = value;
}
int pmap_contains(pmap *map, const void *key)
int pmap_contains(pmap const *map, const void *key)
{
return pmap_find(map, key) != NULL;
}
pmap_entry * pmap_find(pmap *map, const void *key)
pmap_entry *pmap_find(pmap const *map, const void *key)
{
pmap_entry const entry = { key, 0 };
return set_find(pmap_entry, M2S(map), &entry, sizeof(entry), hash_ptr(key));
}
void * (pmap_get)(pmap *map, const void *key)
void *(pmap_get)(pmap const *map, const void *key)
{
pmap_entry * entry = pmap_find(map, key);
return entry == NULL ? NULL : entry->value;
}
size_t pmap_count(pmap *map)
size_t pmap_count(pmap const *map)
{
return set_count(M2S(map));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment