diff --git a/modules/src/data/pmap.h b/modules/src/data/pmap.h index a7980a3a3..dd17f1c24 100644 --- a/modules/src/data/pmap.h +++ b/modules/src/data/pmap.h @@ -1,6 +1,9 @@ #ifndef PMAP_H #define PMAP_H +/* A bidirectional multimap, storing (left, right) tuples. + */ + struct pmap_node { void* left; @@ -23,10 +26,33 @@ struct pmap int max; \ } +/** Adds a new relation `(left -> right)` to the map + * if `left` is not present. Otherwise replaces the + * `right` associated with the first `left`. + * + * Use this in normal map mode. + */ extern void pmap_put(void* map, void* left, void* right); + +/** Adds a new relation to the map if that relation does + * not already exist. + * + * Use this in multimap mode. + */ extern void pmap_add(void* map, void* left, void* right); + +/** Removes a relation from the map. + * + * Use this in multimap mode. + */ extern void pmap_remove(void* map, void* left, void* right); + +/** Given a `left`, find the first matching `right`. + */ extern void* pmap_findleft(void* map, void* left); + +/** Given a `right`, find the first matching `left`. + */ extern void* pmap_findright(void* map, void* right); #endif diff --git a/modules/src/data/smap.c b/modules/src/data/smap.c index 35e594997..abc8d180e 100644 --- a/modules/src/data/smap.c +++ b/modules/src/data/smap.c @@ -26,7 +26,7 @@ static void append(void* mapp, const char* key, void* value) } -void smap_put(struct smap *mapp, const char* key, void* value) +void smap_put(void *mapp, const char* key, void* value) { struct smap* map = mapp; int i; @@ -44,34 +44,36 @@ void smap_put(struct smap *mapp, const char* key, void* value) append(map, key, value); } -void smap_init(struct smap *mapp) +void smap_init(void *mapp) { - mapp->count = 0; - mapp->item = NULL; - mapp->max = 0; + struct smap* map = mapp; + map->count = 0; + map->item = NULL; + map->max = 0; } -void smap_free(struct smap *mapp, int free_key, int free_value) +void smap_free(void *mapp, int free_key, int free_value) { + struct smap* map = mapp; int i; - for (i=0; icount; i++) + for (i=0; icount; i++) { - struct smap_node* node = &mapp->item[i]; + struct smap_node* node = &map->item[i]; if (free_key) { - free(node->left); + free((void*) node->left); } if (free_value) { free(node->right); } } - mapp->count = 0; - free(mapp->item); + map->count = 0; + free(map->item); } -void smap_add(struct smap *mapp, const char* key, void* value) +void smap_add(void *mapp, const char* key, void* value) { struct smap* map = mapp; int i; @@ -86,7 +88,7 @@ void smap_add(struct smap *mapp, const char* key, void* value) append(map, key, value); } -void* smap_get(struct smap *mapp, const char* left) +void* smap_get(void *mapp, const char* left) { struct smap* map = mapp; int i; diff --git a/modules/src/data/smap.h b/modules/src/data/smap.h index 184c2b176..8e82639f7 100644 --- a/modules/src/data/smap.h +++ b/modules/src/data/smap.h @@ -6,9 +6,11 @@ #ifndef SMAP_H #define SMAP_H +/* A string multimap (each key can have multiple values). */ + struct smap_node { - char* left; + const char* left; void* right; }; @@ -23,36 +25,45 @@ struct smap #define SMAPOF(RIGHT) \ struct { \ - struct { char* left; RIGHT* right; }* item; \ + struct { const char* left; RIGHT* right; }* item; \ int count; \ int max; \ } -/** Initializes a string map and returns the initialized - * handle in `mapp`. +/** Initializes a string map. Optional; a zero-initialised map + * structure is a valid empty map. */ -extern void smap_init(struct smap *mapp); -/** Adds a new item with name `key` in the string map if - * it does not already exist, otherwise replaces the - * value `value` associated with the existing `key`. - */ -extern void smap_put(struct smap *mapp, const char* key, void* value); -/** Adds a new item in a string map only if `key` does - * not already exist in the string map. - */ -extern void smap_add(struct smap *mapp, const char* key, void* value); -/** Returns the value associated with the specified `key`, returns - * NULL if `key` is not present in the string map. +extern void smap_init(void *mapp); + +/** Adds a new item with name `key` in the string map if it does not already + * exist. Otherwise replaces the value `value` associated with the existing + * `key`. * + * Use this for normal map mode. */ -extern void* smap_get(struct smap* mapp, const char* key); +extern void smap_put(void *mapp, const char* key, void* value); + +/** Adds a new item with name `key` and value `value`. If that relation already + * exists, nothing happens. + * + * Use this for multimap mode. + */ +extern void smap_add(void *mapp, const char* key, void* value); + +/** Returns the first value associated with the specified `key`. Returns NULL + * if `key` is not present in the string map. + * + * Use this in normal map mode. + */ +extern void* smap_get(void* mapp, const char* key); + /** Frees the data structure associated with the string map. * Also frees the memory associated with the key if * `free_key` is non-zero, and free the memory associated * with the value if `free_value` is non-zero. * */ -extern void smap_free(struct smap *mapp, int free_key, int free_value); +extern void smap_free(void *mapp, int free_key, int free_value); #endif diff --git a/modules/src/data/stringlist.c b/modules/src/data/stringlist.c index 39f125697..e947ea58f 100644 --- a/modules/src/data/stringlist.c +++ b/modules/src/data/stringlist.c @@ -39,7 +39,7 @@ void stringlist_free(struct stringlist* list, int freedata) struct stringfragment* next = f->next; if (freedata) { - free(f->data); + free((void*) f->data); f->data = NULL; } free(f); @@ -54,7 +54,7 @@ void stringlist_init(struct stringlist* list) } -char* stringlist_get(struct stringlist *list, int index) +const char* stringlist_get(struct stringlist *list, int index) { struct stringfragment* f = list->first; int i = 0; diff --git a/modules/src/data/stringlist.h b/modules/src/data/stringlist.h index b3c6ae8e6..b104314d8 100644 --- a/modules/src/data/stringlist.h +++ b/modules/src/data/stringlist.h @@ -3,7 +3,7 @@ struct stringfragment { - char* data; + const char* data; struct stringfragment* next; }; @@ -18,7 +18,7 @@ extern void stringlist_addall(struct stringlist* list, struct stringlist* src); extern void stringlist_free(struct stringlist* list, int freedata); extern void stringlist_init(struct stringlist* list); extern int stringlist_count(struct stringlist *list); -extern char* stringlist_get(struct stringlist *list, int index); +extern const char* stringlist_get(struct stringlist *list, int index); #endif